Examples introduce six ways to clear floats in HTML for reference.
What will happen when display: inline-block is used:
1. Make the block element display in one line
2. Make the inline support width and height
3. Line breaks are parsed
4. When not set, the width is stretched by the content
5. Block tags are supported in IE6 and 7.
Because the inline-block attribute is parsed when the line breaks (there is a gap) The solution is to use floating float:left/right
Situations that occur when using float:
1. Make the block element display in one line
2. Make the inline element support width and height
3. If not set, no When the width and height are high, the width is stretched by the content
4. Line breaks are not parsed (so when using inline elements, you can use floats to clear gaps)
5. Adding floats to elements will break away from the document flow and follow the specified Move in one direction until it hits the boundary of the parent or another floating element stops (the document flow is the position occupied by displayable objects in the document when arranged)
Methods to clear floats:
1. Add a float to the parent (in this case, the parent margin: 0 auto; will not be centered)
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> .box{ width:300px;margin:0 auto;border:10px solid #000; float:left;} .p{ width:200px;height:200px;background:red;float:left;} /* 清浮动 1.给父级也加浮动(不居中了) */ </style> </head> <body> <p class="box"> <p class="p"></p> </p> </body> </html>
2. Add display:inline-block; to the parent; (Same as method 1, not centered. Only IE6 and 7 are centered)
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> .box{ width:300px;margin:0 auto;border:10px solid #000; display:inline-block;} .p{ width:200px;height:200px;background:red;float:left;} /* 清浮动 1.给父级也加浮动 2.给父级加display:inline-block */ </style> </head> <body> <p class="box"> <p class="p"></p> </p> </body> </html>
3. Add under the floating element p>
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> .box{ width:300px;margin:0 auto;border:10px solid #000;} .p{ width:200px;height:200px;background:red;float:left;} .clear{ height:0px;font-size:0;clear:both;} /* 清浮动 1.给父级也加浮动 2.给父级加display:inline-block 3.在浮动元素下加<p class="clear"></p> .clear{ height:0px;font-size:0;clear:both;} */ </style> </head> <body> <p class="box"> <p class="p"></p> <p class="clear"></p> </p> </body> </html>
4. Add
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> .box{ width:300px;margin:0 auto;border:10px solid #000;} .p{ width:200px;height:200px;background:red;float:left;} /* 清浮动 1.给父级也加浮动 2.给父级加display:inline-block 3.在浮动元素下加<p class="clear"></p> .clear{ height:0px;font-size:0;clear:both;} 4.在浮动元素下加<br clear="all"/> */ </style> </head> <body> <p class="box"> <p class="p"></p> <br clear="all"/> </p> </body> </html>
##5. Add {zoom:1;} to the parent of the floating element
:after{content:""; display:block;clear:both;}
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> .box{margin:0 auto;border:10px solid #000;} .p{ width:200px;height:200px;background:red;float:left;} .clear{zoom:1;} .clear:after{content:""; display:block;clear:both;} /* 清浮动 1.给父级也加浮动 2.给父级加display:inline-block 3.在浮动元素下加<p class="clear"></p> .clear{ height:0px;font-size:0;clear:both;} 4.在浮动元素下加<br clear="all"/> 5. 给浮动元素的父级加{zoom:1;} :after{content:""; display:block;clear:both;} **在IE6,7下浮动元素的父级有宽度就不用清浮动 haslayout 根据元素内容的大小 或者父级的父级的大小来重新的计算元素的宽高 display: inline-block height: (任何值除了auto) float: (left 或 right) width: (任何值除了auto) zoom: (除 normal 外任意值) */ </style> </head> <body> <p class="box clear"> <p class="p"></p> </p> </body> </html>
6. Add overflow:auto;
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> .box{ width:300px;border:1px solid #000;overflow:auto;} .p1{ width:260px;height:400px;background:Red;float:left;} </style> </head> <body> <p class="box"> <p class="p1"></p> </p> </body> </html>
to the parent of the floating element
The above is the detailed content of Examples introduce six ways to clear floats in HTML for reference.. 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

AI Hentai Generator
Generate AI Hentai for free.

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



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
