The clearfix, as the name suggests, is used to clear floats. It is generally used in float layouts. The clearfix is considered a hack to clear floats.
Let us see the problem first before moving towards the solution. We have an image here, floated to the right. It overflows outside its container since it is way taller than the element belonging to it −
<!DOCTYPE html> <html> <head> <style> div { border: 2px solid blue; padding: 5px; } .myimg { float: right; } </style> </head> <body> <h2>Demo Heading</h2> <p>We haven't used clearfix below:</p> <div> @@##@@ Etiam accumsan metus sapien, rutrum sagittis nunc posuere eu. Ut facilisis tortor eget justo scelerisque, quis porta nisl sagittis. </div> </body> </html>
The output displays the overflow issue −
Let us now fix the issue with clearfix −
<!DOCTYPE html> <html> <head> <style> div { border: 2px solid blue; padding: 5px; } .myimg { float: right; } .clearfix { overflow: auto; } </style> </head> <body> <h2 style="clear:right">Demo Heading</h2> <p>We have used clearfix below:</p> <div class="clearfix"> @@##@@ Etiam accumsan metus sapien, rutrum sagittis nunc posuere eu. Ut facilisis tortor eget justo scelerisque, quis porta nisl sagittis. </div> </body> </html>
The ::after选择器在这里用于修复clearfix −
<!DOCTYPE html> <html> <head> <style> div { border: 2px solid blue; padding: 5px; } .myimg { float: right; } .clearfix::after { content: ""; clear: both; display: table; } </style> </head> <body> <h2 style="clear:right">Demo Heading</h2> <p>We have used clearfix below:</p> <div class="clearfix"> @@##@@ Etiam accumsan metus sapien, rutrum sagittis nunc posuere eu. Ut facilisis tortor eget justo scelerisque, quis porta nisl sagittis. </div> </body> </html>
以上就是什么是clearfix?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号