Home Web Front-end CSS Tutorial A detailed introduction to several ways to clear floats in CSS

A detailed introduction to several ways to clear floats in CSS

Aug 07, 2017 pm 03:22 PM
css Way

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

after the floating label, do not set the width and height and then set .clearboth{clear:both}, and then put the required label in this space Just after the label
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;
   }
Copy after login

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>
Copy after login
.clearfix{
			 	display: table;/* 触发nfc */
			 }
			 .clearfix:before,.clearfix:after{
				/* 前后加一个空的标签清除浮动 */
				content: "";
				display: block;
				height: 0;
				clear: both;
			 }
			 .clearfix{
			 	_zoom:1;/* 为了兼容ie6 */
			 }
Copy after login

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!

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
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.

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.

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.

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

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

See all articles