search
HomeWeb Front-endCSS TutorialA brief analysis of floating and cleaning in CSS

As a front-end, I have written a lot of page layouts, but floating has always been a blind spot that I don’t understand. On the one hand, floating can be used to achieve many layouts, but on the other hand, the impact of floating will destroy the layout and cause headaches, so Today I will write a blog post to address this blind spot.

This article mainly discusses the following issues:
1. The original purpose of floating
2. Why floating has a text wrapping effect
3. How to clean up the impact of floating

Original uses of float

The following content comes from Zhang Xinxu’s "In-depth Research, Detailed Explanation and Expansion of CSS Float (1)":

Assume that there is no float in CSS now ( float) attribute, what will it look like. We will find that whether it is column layout or list arrangement that is currently popularly implemented using the floating method, we can use some other CSS properties (regardless of table) to implement it. The only thing that cannot be implemented is "text wrapping around pictures". I am I can't think of any way to wrap the text around the image. Well, this irreplaceable role is the real meaning of float.

To sum up in one sentence: Achieve the effect of text surrounding the picture.

Why does floating have a text wrapping effect?

This question mainly comes from past impressions:

Floating elements are separated from the document flow.

Huh? If you are out of the document flow, shouldn't it overlap with the unfloated elements below? Why can the text still wrap around you?

This problem has been bothering me until I saw the book "Mastering CSS". The article writes:

Floating will cause elements to break out of the document flow and will no longer affect non-floating elements. Actually, not entirely. If there is an element in the document flow behind the floated element, then the box of this element will behave as if the float does not exist at all. However, the text content of the box will be affected by the floating element and will move to make space.

In other words, floating elements are indeed separated from the document flow, so the block box in the document flow will ignore the floating elements, but the text will not . This is different from absolute positioning out of the document flow.

Senior Zhang Qiuyi’s answer on Zhihu also confirms this view.

Additional point: The term document flow is actually inaccurate. In the W3C standard, there is only normal flow (normal flow), but many domestic articles and translated books use document flow. Over time, everyone will In terms of document flow.

How to clean up the impact caused by floating

In fact, the main impact caused by floating is

1. The height collapse of the parent element caused by the element breaking away from the ordinary flow
2. Below Non-text elements overlap with floating elements, destroying the original layout

To clean up these effects, many people know to use the clear attribute. Why can clear clear floats?

There are two opinions:
1. BrowserAdd enough margins on top of the clear element so that the upper border of the element drops vertically to the lower edge of the floating element below the distance.
2.Add clear space above the top margin of the clear element, while the margin itself does not change.

The former is the implementation principle of CSS1 and CSS2, and the latter is the implementation principle of CSS2.1. But no matter which one is achieved, vertical space is left for the floating elements, which seems to clear the floating effect. At the same time, the space filled above the set clear element is actually present in the normal flow. Therefore, the height of the parent element is stretched.

There are actually many ways to clear floats. In the article "The Floats We Cleared Together in Those Years", the methods of clearing floats are simply divided into two categories:

1. By adding an empty element at the end of the floating element, set clear: both attributes, the after pseudo-element actually generates a block-level element with a point content behind the element through content;
2. Close the float by setting the overflow or display: table attribute of the parent element.

Almost all methods for clearing floats can be classified into these two categories. Let’s talk about the method of adding pseudo elements in conjunction with the above blog post:

1.display: block , so that the generated elements are displayed as block-level elements and occupy the remaining space.
2. Add a dot in content because this character is very small.
3. Set height to 0, because you don’t want this new content to occupy space and destroy the layout height.
4. Set visibility to hidden to make the generated content invisible, allowing parts that may be covered by the generated content to be clickable and interactive.
5.clear: Both clear the impact of floating

The code is as follows:

.clearfix:after {
    display: block;
    content: ".";
    height: 0;
    visibility: hidden;
    clear: both;
}

This method is effective in most modern browsers and is also recommended. As for other methods, there are many online, so I won’t go into details here.

This article draws on some other blogs and documents, with links attached:

Reference links:

CSS clear attribute

CSS float

Understanding CSS float and clear float

The floats we cleared together in those years

noraml flow

In-depth research, detailed explanation and expansion of CSS float float (1)

Zhang Qiuyi’s answer


##

As a front-end, I have written a lot of page layouts, but floating has always been a blind spot that I don’t understand. On the one hand, floating can be used to achieve many layouts, but on the other hand, the impact of floating will destroy the layout and cause headaches, so Today I will write a blog post to address this blind spot.

This article mainly discusses the following issues:
1. The original purpose of floating
2. Why floating has a text wrapping effect
3. How to clean up the impact of floating

Original uses of float

The following content comes from Zhang Xinxu’s "In-depth Research, Detailed Explanation and Expansion of CSS Float (1)":

Assume that there is no float in CSS now ( float) attribute, what will it look like. We will find that whether it is column layout or list arrangement that is currently popularly implemented using the floating method, we can use some other CSS properties (regardless of table) to implement it. The only thing that cannot be implemented is "text wrapping around pictures". I am I can't think of any way to wrap the text around the image. Well, this irreplaceable role is the real meaning of float.

To sum up in one sentence: Achieve the effect of text surrounding the picture.

Why does floating have a text wrapping effect?

This question mainly comes from past impressions:

Floating elements are separated from the document flow.

Huh? If you are out of the document flow, shouldn't it overlap with the unfloated elements below? Why can the text still wrap around you?

This problem has been bothering me until I saw the book "Mastering CSS". The article writes:

Floating will cause elements to break out of the document flow and will no longer affect non-floating elements. Actually, not entirely. If there is an element in the document flow behind the floated element, then the box of this element will behave as if the float does not exist at all. However, the text content of the box will be affected by the floating element and will move to make space.

In other words, floating elements are indeed separated from the document flow, so the block box in the document flow will ignore the floating elements, but the text will not . This is different from absolute positioning out of the document flow.

Senior Zhang Qiuyi’s answer on Zhihu also confirms this view.

Additional point: The term document flow is actually inaccurate. In the W3C standard, there is only normal flow (normal flow), but many domestic articles and translated books use document flow. Over time, everyone will In terms of document flow.

How to clean up the impact caused by floating

In fact, the main impact caused by floating is

1. The height collapse of the parent element caused by the element breaking away from the ordinary flow
2. Below Non-text elements overlap with floating elements, destroying the original layout

To clean up these effects, many people know to use the clear attribute. Why can clear clear floats?

There are two opinions:
1. BrowserAdd enough margins on top of the clear element so that the upper border of the element drops vertically to the lower edge of the floating element distance below.
2.Add clear space above the top margin of the clear element, while the margin itself does not change.

The former is the implementation principle of CSS1 and CSS2, and the latter is the implementation principle of CSS2.1. But no matter which one is achieved, vertical space is left for the floating elements, which seems to clear the floating effect. At the same time, the space filled above the set clear element is actually present in the normal flow. Therefore, the height of the parent element is stretched.

There are actually many ways to clear floats. In the article "The Floats We Cleared Together in Those Years", the methods of clearing floats are simply divided into two categories:

1. By adding an empty element at the end of the floating element, set clear: both attributes, the after pseudo-element actually generates a block-level element with a point content behind the element through content;
2. Close the float by setting the overflow or display: table attribute of the parent element.

Almost all methods for clearing floats can be classified into these two categories. Let’s talk about the method of adding pseudo elements in conjunction with the above blog post:

1.display: block , so that the generated elements are displayed as block-level elements and occupy the remaining space.
2. Add a dot in content because this character is very small.
3. Set height to 0, because you don’t want this new content to occupy space and destroy the layout height.
4. Set visibility to hidden to make the generated content invisible, allowing parts that may be covered by the generated content to be clickable and interactive.
5.clear: Both clear the impact of floating

The code is as follows:

.clearfix:after {
    display: block;
    content: ".";
    height: 0;
    visibility: hidden;
    clear: both;
}

This method is effective in most modern browsers and is also recommended. As for other methods, there are many online, so I won’t go into details here.


The above is the detailed content of A brief analysis of floating and cleaning in CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

How can we make our website responsive using CSS?How can we make our website responsive using CSS?Apr 30, 2025 pm 03:19 PM

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

What does the CSS box-sizing property do?What does the CSS box-sizing property do?Apr 30, 2025 pm 03:18 PM

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

How can we animate using CSS?How can we animate using CSS?Apr 30, 2025 pm 03:17 PM

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Can we add 3D transformations to our project using CSS?Can we add 3D transformations to our project using CSS?Apr 30, 2025 pm 03:16 PM

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

How can we add gradients in CSS?How can we add gradients in CSS?Apr 30, 2025 pm 03:15 PM

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

What are pseudo-elements in CSS?What are pseudo-elements in CSS?Apr 30, 2025 pm 03:14 PM

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use