Home Web Front-end CSS Tutorial The most comprehensive css clearfix method to clear floats

The most comprehensive css clearfix method to clear floats

Mar 17, 2017 pm 04:25 PM
css float

Clearfix is ​​often used to clear floats in CSS. The author lists several different methods of using clearfix for comparison. Friends in need can refer to the css clearfix method to clear floats.(css Why should we clear float? What is the principle of clearing float? If you search "css clear float" on Baidu, you will find that many websites mention ".clearfix can be used when clearing internal floats in a box".

.clearfix:after {
  content: " ";
  display: block;
  clear: both;
  height: 0;
}
.clearfix {
  zoom: 1;
}<p class="clearfix">
  <p class="floated"></p>
</p>
Copy after login

The above code is the definition and application of .clearfix. Let’s briefly talk about the principle of .clearfix:

1. In IE6 and 7, zoom: 1 will trigger hasLayout, thus making the element Close the inner float.
2. Under standard browsers, the .clearfix:after pseudo-class will insert a clear: both block-level element after the element applied to .clearfix, thereby clearing the float.

<p>
  <p class="floated"></p>
</p>
<p style="clear: both"></p>
Copy after login

2. The disadvantages of .clearfix

As you can see in the above code, leaving aside IE6 and 7, .clearfix is ​​inserted under a standard browser An element with clear: both is added, which is likely to clear unnecessary floats. To illustrate with an example:

<!DOCTYPE html>
<html>
<head>
  <title>Demo</title>
  <style type="text/css">
    html, body { padding: 0; margin: 0; }
    ul { margin: 0; padding: 0; } 
  
    .clearfix:after {
      content: " ";
      display: block;
      clear: both;
      height: 0;
    }
    .clearfix {
      zoom: 1;
    }
  
    .left-col {
      background: red;      float: left;
      width: 100px;
      height: 300px;
    }
    .right-col {
      margin-left: 100px;
    }
    .menu {
      border: 1px solid #000;
    }
    .menu li {      float: left;
      display: block;
      padding: 0 1em;
      margin: 0 1em 0 0;
      background: #ccc;
    }
    .placeholder {
      background: yellow;
      height: 400px;
    }  </style>
</head>
<body>
  <p class="left-col">
  </p>
  <p class="right-col">
    <ul class="menu">
      <li>Menu Item</li>
      <li>Menu Item</li>
      <li>Menu Item</li>
      <li>Menu Item</li>
      <li>Menu Item</li>
      <li>Menu Item</li>
    </ul>
    <p class="placeholder"></p>
  </p>
</body>
</html>
Copy after login

The above code constitutes a two-column layout page. Note that the .menu menu has a border, but because the li element of the .menu floats left, the .menu has no height, so you can use .clearfix to clear the floating content inside the .menu. The code is as follows:

<ul class="menu clearfix">
  <li>Menu Item</li>
  <li>Menu Item</li>
  <li>Menu Item</li>
  <li>Menu Item</li>
  <li>Menu Item</li>
  <li>Menu Item</li>
</ul>
Copy after login

But after applying .clearfix, the page becomes very messy under the standard browser. This is because .clearfix:after also clears the float of .left-col.

3. Reconstruct .clearfix

After encountering the above error, analyze whether there is any other method besides .clearfix:after. Clear the float inside the element. The answer is yes. In the article Block Formatting Contexts in vernacular, it is mentioned that the elements that constitute the Block Formatting Context can clear the floating of internal elements. Then just make .clearfix form a Block Formatting Context. There are several methods to construct Block Formatting Context:

The value of float is not none.

The value of overflow is not visible.

  • The value of display is any one of table-cell, table-caption, and inline-block.

  • The value of position is not relative or static.

  • Obviously, float and position are not suitable for our needs. Then you can only choose one from overflow or display. Because the menu that applies .clearfix and .menu is most likely to be multi-level, overflow: hidden or overflow: auto does not meet the needs (it will hide the drop-down menu or display the scroll bar), so you can only use display Take action.

  • We can set the display value of .clearfix to any one of table-cell, table-caption, and inline-block, but display: inline-block will produce redundant blanks, so it is also excluded. The only ones left are table-cell and table-caption. In order to ensure compatibility, you can use display: table to make .clearfix form a Block Formatting Context, because display: table will generate some anonymous boxes. One of these anonymous boxes (the display value is table-cell) will form a Block Formatting Context. In this way, our new .clearfix will close the float of the inner element. Below is the .clearfix after refactoring.
  • .clearfix {
      zoom: 1;
      display: table;
      width: 100%;
    }
    Copy after login

    Four, summary

    Under IE6 and 7, as long as the element that triggers hasLayout can clear the internal float. There are many ways to clear internal floats of elements under standard browsers. Except for .clearfix:after, the other methods are nothing more than generating a new Block Formatting Context to achieve the purpose. If you can use which method under what conditions, I think this is enough...

    For more related articles about the css clearfix method of clearing floats, please pay attention to php Chinese website

    Related articles:

    In-depth analysis of clearfix to clear floats

    In-depth understanding of the usage of clearfix in css

    A brief discussion on the usage of clearfix and clear in css

    CSS about clearfix clearing floating method

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 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

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)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles