search
HomeWeb Front-endCSS TutorialDetailed introduction to the usage of CSS clear property to clear floats

What is CSS clear float?

A popular saying on the Internet is: Under non-IE browsers (such as Firefox), when the height of the container (height) is auto, and the content of the container has float (float is left or right) element. In this case, the height of the container cannot automatically extend to adapt to the height of the content, causing the content to overflow outside the container and affect (or even destroy) the layout. This phenomenon is called float overflow, and the CSS processing performed to prevent this phenomenon is called CSS clear float.

Use clear style to clear
Example:

.clear-float {clear:both;}

The clear attribute is provided by CSS 1 to clear the floating style. For an element with the clear attribute set, its top border position will be rendered close to the margin-bottom boundary position of the floating element, ignoring its margin-top setting. In this way, when the height of the parent container is not set (the value is auto), since the defined cleanup floating style element is located below the floating element, the calculated actual height of the container includes the floating element.

Example
Add p4 in the code, and set the width and height to 300px, gray background color, the code is as follows

HTML code:

<p class="p4">
    p4   
</p>

CSS code:

.p4 {   
    width: 300px;   
    height: 300px;   
    background-color: darkgray;   
}

The effect is as follows:
Detailed introduction to the usage of CSS clear property to clear floats

1. The one circled by the red line is p4, everyone You will find a problem, that is, p4 is still arranged from the upper left corner, but the text is not in the upper left corner. This is a problem caused by floating.
2. If we want to achieve the effect of p4 being attached below p2 and to the right of p3, we also need to set float: left attribute description for p4
But we don’t want this effect, but want Let p4 be rearranged below instead of floating with p1, p2, and p3. At this time, we need to clear the floating

.p4 {   
    width: 300px;   
    height: 300px;   
    background-color: darkgray;   
    clear: both;   
}

Just add the clear: both; attribute, You can clear the float.

The effect is as follows:
Detailed introduction to the usage of CSS clear property to clear floats

After clearing the floats, p4 can be arranged from below and will no longer participate in the floating of the above ps. This is called clearing the float.

For more detailed introduction to the usage of CSS clear attribute to clear floats, please pay attention to the PHP Chinese website for related articles!

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft