A detailed introduction to several ways to clear floats in CSS
What is float?
Features: 1--Floating elements will not occupy the space of the standard flow, but will affect the layout of the text in the standard flow. Floating only floats left and right.
2--The position of floating element A is related to the previous element. The top of A is aligned with the top of the previous element if the previous element has a float, and with its bottom if there is no float.
3--For a child box in a parent box, if one of the children is floated, the other children need to be floated in order to display one line.
4--After the element is floated, if the width and height are not set, the element will set the size of the content according to the amount of content and will have the attributes of the inline block element.
5--After the element is floated, if the parent element does not set a height. Will cause the height of the parent element to collapse.
solve this problem?
-----Add attributes to the parent element: overflow:hidden; hide the excess part
overflow attribute:
When the elements in the box exceed the size of the sunspot itself, the content Display mode
visible: the content will not be trimmed and will be presented outside the element box (default)
hidden: will be trimmed and hidden, you can use this attribute to clear the float
auto: adaptive display scroll bar
scroll: The content will be trimmed and the scroll bar will be displayed
bfc:
Overflow can trigger the bfc of the element, allowing the element to have the space and permissions for typesetting. All elements within the bfc are based on The parent element performs typesetting and layout, and all parent elements have wrapping properties. This is the principle that solves the problem of height collapse.
Floating, positioning, overflow, display, table, table-cell can all trigger bfc.
Clear floats:
--The element will be displayed in the standard stream only when there are no floating elements on the left and right sides of the current element.
--Four ways to clear floats:
1. Use empty markers to clear floats and put them on the wall. Add tags.
Add a
2. Use the overflow attribute to clear the float. Will cause accidental injury.
overflow:hidden;
3. Use the after pseudo-object to clear floats
4. Use the before after pseudo-object to clear floats
clearfix (a combination of methods 2, 3, and 4, commonly used).
.clearfix 父盒子要把子盒子包裹住,触发bfc同时清除前后浮动。 .clearfix{ display:table;<!-- 触发dfc --> } .clearfix:before,.clearfix:after{ content:""; display:block; clear:both; height:0; } 在ie6中display:table;不能触发dfc,所以用以下方式进行触发 .clearfix{ _zoom:1; }
Usage:
<p class="top">top</p> <p class="clearfix"> <p class="left">left</p> <p class="right">right</p> </p> <p class="bottom">bottom</p>
.clearfix{ display: table;/* 触发nfc */ } .clearfix:before,.clearfix:after{ /* 前后加一个空的标签清除浮动 */ content: ""; display: block; height: 0; clear: both; } .clearfix{ _zoom:1;/* 为了兼容ie6 */ }
The above is the detailed content of A detailed introduction to several ways to clear floats in CSS. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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

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.

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.

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.

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.

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-

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