Home Web Front-end CSS Tutorial What are the ways to clear floats in css? Introduction to four methods of css float clearing

What are the ways to clear floats in css? Introduction to four methods of css float clearing

Sep 14, 2018 pm 01:37 PM
css clear float

css clearing floats is often seen in css layout, so how to clear floats in css? This article will introduce to you how to clear floats in css, and briefly introduce why you should use css floats.

Our last articleWhat does css floating mean? Why does css need to clear floats? It has been mentioned in

that the reason for using CSS floating is because some elements in CSS are block-level elements, and they will automatically start a new line. There is also another type of inline elements, which are inline elements, and they will remain with the previous content. Displayed in "one row"; but sometimes we need to change this layout, which requires using CSS floating. But when "height collapse" occurs, floats need to be cleared. So, what are the methods to clear floats in CSS?

Let’s take a look at the four methods of clearing floats in CSS.

Css method to clear floating elements:

Use an empty element with clear attribute

Use an element after the floating element Empty elements such as <div class="clear"></div>, and assign the .clear{clear:both;} attribute in CSS to clear the float. You can also use<br class="clear" /> or <hr class="clear" /> to clean.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

.news {

  background-color: gray;

  border: solid 1px black;

  }

.news img {

  float: left;

  }

.news p {

  float: right;

  }

.clear {

  clear: both;

  }

&lt;div class=&quot;news&quot;&gt;

&lt;img src=&quot;news-pic.jpg&quot; /&gt;

&lt;p&gt;some text&lt;/p&gt;

&lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;

&lt;/div&gt;

Copy after login
Description:

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.

Css method two to clear floating elements:

Use the overflow property of CSS

Add overflow to the container of floating elements: hidden; or overflow:auto; can clear the float. In addition, hasLayout needs to be triggered in IE6, such as setting the container width and height for the parent element or setting zoom:1.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

.news {

  background-color: gray;

  border: solid 1px black;

  overflow: hidden;

  *zoom: 1;

  }

.news img {

  float: left;

  }

.news p {

  float: right;

  }

&lt;div class=&quot;news&quot;&gt;

&lt;img src=&quot;news-pic.jpg&quot; /&gt;

&lt;p&gt;some text&lt;/p&gt;

&lt;/div&gt;

Copy after login
Description:

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.

Advantages: There are no structural and semantic problems, and the amount of code is very small

Disadvantages: When the content increases, it is easy to cause the content to not wrap automatically, causing the content to be hidden, and elements that need to overflow cannot be displayed; In 2004, POPO discovered that overflow:hidden would cause the middle button to fail, which is unacceptable to me as a multi-tab browsing controller. So don’t use it.

Css method three to clear floats:

Use adjacent element processing

Do nothing , add the clear attribute to the element behind the floating element.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

.news {

  background-color: gray;

  border: solid 1px black;

  }

.news img {

  float: left;

  }

.news p {

  float: right;

  }

.content{

  clear:both;

  }

&lt;div class=&quot;news&quot;&gt;

&lt;img src=&quot;news-pic.jpg&quot; /&gt;

&lt;p&gt;some text&lt;/p&gt;

&lt;div class=&quot;content&quot;&gt;&lt;/div&gt;

&lt;/div&gt;

Copy after login
Css clear floating method four:

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 an element) 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 a :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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

.news {

  background-color: gray;

  border: solid 1px black;

  }

.news img {

  float: left;

  }

.news p {

  float: right;

  }

.clearfix:after{

  content: &quot;020&quot;;

  display: block;

  height: 0;

  clear: both;

  visibility: hidden; 

  }

.clearfix {

  /* 触发 hasLayout */

  zoom: 1;

  }

&lt;div class=&quot;news clearfix&quot;&gt;

&lt;img src=&quot;news-pic.jpg&quot; /&gt;

&lt;p&gt;some text&lt;/p&gt;

&lt;/div&gt;

Copy after login

Description: An invisible space "020" or dot "." is added at the end of the internal element of the container through CSS pseudo-element, and the clear attribute is assigned 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.

Finally: For the specific content of some attributes in the above article, please refer to css manual
.

Related recommendations:

Summary of common methods for clearing floats in CSS_Experience exchange

css Don’t forget to clear floats clear:both_Experience exchange

Float (floating) related skills article in CSS_Experience exchange

###

The above is the detailed content of What are the ways to clear floats in css? Introduction to four methods of css float clearing. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article

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

Hot Article

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does placeholder mean in vue What does placeholder mean in vue May 07, 2024 am 09:57 AM

What does placeholder mean in vue

How to write spaces in vue How to write spaces in vue Apr 30, 2024 am 05:42 AM

How to write spaces in vue

How to get dom in vue How to get dom in vue Apr 30, 2024 am 05:36 AM

How to get dom in vue

What does span mean in js What does span mean in js May 06, 2024 am 11:42 AM

What does span mean in js

What does rem mean in js What does rem mean in js May 06, 2024 am 11:30 AM

What does rem mean in js

How to introduce images into vue How to introduce images into vue May 02, 2024 pm 10:48 PM

How to introduce images into vue

What is the function of span tag What is the function of span tag Apr 30, 2024 pm 01:54 PM

What is the function of span tag

How to wrap prompt in js How to wrap prompt in js May 01, 2024 am 06:24 AM

How to wrap prompt in js

See all articles