search
HomeWeb Front-endCSS TutorialDetailed explanation of how to clear floats using CSS

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 use
or
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=&#39;text/css&#39;>   
        #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:
Detailed explanation of how to clear floats using CSS

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: &#39;.&#39;;   
            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!

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
利用CSS怎么创建渐变色边框?5种方法分享利用CSS怎么创建渐变色边框?5种方法分享Oct 13, 2021 am 10:19 AM

利用CSS怎么创建渐变色边框?下面本篇文章给大家分享CSS实现渐变色边框的5种方法,希望对大家有所帮助!

css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

怎么设置rotate在css3的旋转中心点怎么设置rotate在css3的旋转中心点Apr 24, 2022 am 10:50 AM

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.