This article mainly introduces the detailed method of using CSS to clear floats. It is worth noting that not simply clearing floats can solve related problems every time. The solution of closing floats mentioned at the end of the article is also worth a try. , Friends in need can refer to
Clear floating method
Method 1: Use an empty element with clear attribute
Use an empty element after the floating element, such as
, and assign the .clear{clear:both;} attribute in CSS to clear the float. You can also useor
for cleaning.
.news { background-color: gray; border: solid 1px black; } .news img { float: left; } .news p { float: rightright; } .clear { clear: both; }
<p class="news"> <img src="/static/imghwm/default1.png" data-src="news-pic.jpg" class="lazy" / alt="Detailed explanation of how to clear floats using CSS" > <p>some text</p> <p class="clear"></p> </p>
Advantages: simple, less code, good browser compatibility.
Disadvantages: A large number of unsemantic html elements need to be added, the code is not elegant enough, and it is not easy to maintain later.
Method 2: Use the overflow attribute of CSS
Add overflow:hidden; or overflow:auto; to the container of the floating element to clear the float. In addition, in IE6 HasLayout needs to be triggered, such as setting the container width and height for the parent element or setting zoom:1. After adding the overflow attribute, the floating element returns to the container layer, raising the height of the container, achieving the effect of cleaning up the floating elements.
.news { background-color: gray; border: solid 1px black; overflow: hidden; *zoom: 1; } .news img { float: left; } .news p { float: rightright; }
<p class="news"> <img src="/static/imghwm/default1.png" data-src="news-pic.jpg" class="lazy" / alt="Detailed explanation of how to clear floats using CSS" > <p>some text</p> </p>
Method 3: Add a float to the container of the floating element
Also add a floating attribute to the container of the floating element to clear the interior Float, but this will make it float as a whole and affect the layout, so it is not recommended.
Method 4: Use adjacent element processing
Do nothing and add the clear attribute to the element behind the floating element.
.news { background-color: gray; border: solid 1px black; } .news img { float: left; } .news p { float: rightright; } .content{ clear:both; }
<p class="news"> <img src="/static/imghwm/default1.png" data-src="news-pic.jpg" class="lazy" / alt="Detailed explanation of how to clear floats using CSS" > <p>some text</p> <p class="content">***</p> </p>
Note that p.content here has content.
Method Five: Use CSS:after pseudo-element
Combined with:after pseudo-element (note that this is not a pseudo-class, but a pseudo-element, which represents the nearest element after elements) and IEhack, which are perfectly compatible with all major current mainstream browsers. IEhack here refers to triggering hasLayout.
Add a clearfix class to the container of floating elements, and then add an :after pseudo-element to this class to add an invisible block element (Block element) to the end of the element to clean up the floating elements.
.news { background-color: gray; border: solid 1px black; } .news img { float: left; } .news p { float: rightright; } .clearfix:after{ content: "020"; display: block; height: 0; clear: both; visibility: hidden; } .clearfix { /* 触发 hasLayout */ zoom: 1; }
<p class="news clearfix"> <img src="/static/imghwm/default1.png" data-src="news-pic.jpg" class="lazy" / alt="Detailed explanation of how to clear floats using CSS" > <p>some text</p> </p>
Add an invisible space "020" or dot "." at the end of the internal element of the container through CSS pseudo-elements, and assign the clear attribute to clear the float. It should be noted that for IE6 and IE7 browsers, a zoom:1; must be added to the clearfix class to trigger haslayout.
Summary
Through the above example, we can easily find that the methods of clearing floats can be divided into two categories:
One is to use the clear attribute, included in the floating element Add an empty p with the clear: both attribute at the end to close the element. In fact, the method of using the :after pseudo-element is also achieved by adding an element with the content of a dot and the clear: both attribute at the end of the element.
The second is to trigger the BFC (Block Formatting Contexts, block-level formatting context) of the parent element of the floating element, so that the parent element can contain floating elements. Regarding this point.
Use the :after pseudo-element method in the main layout of the web page and use it as the main way to clean up floats; use overflow:hidden; in small modules such as ul (pay attention to possible hidden overflow element problems); if it is a float itself Elements can automatically clear internal floats without special processing; use adjacent elements in the body to clear previous floats.
Finally, you can use the relatively perfect :after pseudo-element method to clean up the floats, making the document structure clearer.
Postscript: Clear float or close float?
We often use floats in web page layout. With it, we can more easily achieve the effects we want, but floats often leave some hidden dangers. At this time, we usually do one thing to clear the floats, but clearing the floats often leaves hidden dangers. The following code:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <style type='text/css'> #main{ border: 1px solid #000; } #left{ float: left; width: 100px; height: 100px; background-color: #f00; } #right{ float: left; width: 100px; height: 100px; background-color: #0f0; } </style> </head> <body> <p id="main"> <p id="left"></p> <p id="right"></p> </p> </body> </html>
The rendering is as follows:
Although clear:both is used in foot to clear floats, the height of main cannot adapt to the height of sub-elements, resulting in collapse (pointed by the arrow).
The following introduces closed float. As the name suggests, it is to close the floating element and clear the impact of floating. Currently, the most commonly used method to clear floats is clearfix. Specifically, there is no need to add clear:both in the foot, insert the following line of css:
#main:after{ content: '.'; height: 0; visibility: hidden; /*display: block;*/ clear:both; }
The above is the detailed content of Detailed explanation of how to clear floats using CSS. For more information, please follow other related articles on the PHP Chinese website!

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides

With Astro, we can generate most of our site during our build, but have a small bit of server-side code that can handle search functionality using something like Fuse.js. In this demo, we’ll use Fuse to search through a set of personal “bookmarks” th

I wanted to implement a notification message in one of my projects, similar to what you’d see in Google Docs while a document is saving. In other words, a

Some months ago I was on Hacker News (as one does) and I ran across a (now deleted) article about not using if statements. If you’re new to this idea (like I

Since the early days of science fiction, we have fantasized about machines that talk to us. Today it is commonplace. Even so, the technology for making

I remember when Gutenberg was released into core, because I was at WordCamp US that day. A number of months have gone by now, so I imagine more and more of us

The idea behind most of web applications is to fetch data from the database and present it to the user in the best possible way. When we deal with data there

Let's do a little step-by-step of a situation where you can't quite do what seems to make sense, but you can still get it done with CSS trickery. In this


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor