Home >Web Front-end >CSS Tutorial >CSS Float and related layout modes
float value
Look at a chestnut
The red wireframe represents the parent element
It is out of the document flow. In fact, it is not completely out of the way, and will be blocked by the boundary of the parent element.
float elements are in the same document flow
Look at an example:
The red line box represents the parent element
After the three child elements "float to the left", the parent element has no content, so the parent element does not High.
float elements are semi-detached from the document flow
For elements, they are detached from the document flow; for content, they are in the document flow. The elements overlap, but the content does not.
Give me an example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>float元素半脱离文档流</title> <style type="text/css"> body{ width: 300px;padding: 5px;line-height: 1.6; border: 1px dashed blue; } .sample{ height: 100px;margin-right: 5px; padding: 0 5px; line-height: 100px;background-color: pink; } .sb{ outline: 1px dashed red; } .sample{ float: left; } </style> </head> <body> <div class="sample">float : left </div> <div class="sb"> A float is a box that is shifted to the left or right on the current line . the most interesting characteristic of a float (o "floated" o "floating" box) is that content may flow along its side (or be prohibited from doing so by the 'clear' property). </div> </body> </html>
Give me another example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>float abscure</title> <style type="text/css"> body{ width: 400px;line-height: 1.6; } .sample{ width: 100px;line-height: 100px; margin: 3px;text-align: center; background-color: pink; } .sb{ margin: 10px auto; padding: 5px; border: 1px dashed #0f00fa; } .sample{ float: left; } </style> </head> <body> <div class="sb"> <div class="sample">float: left;</div> 第十二届ChinaJoy 动漫游戏展7月31号在上海新国际博览中心开幕,导出是站台表演的帅哥美女。 </div> <div class="sb"> 有些游戏商为了吸引人气,还请来了著名的演员、模特前来助阵。以下是一批漂亮的Show Girl现场照片。 </div> </body> </html>
Usually we don’t want this result, but the pink div is only surrounded by the first text block. You need to use the clear attribute
clear
To understand clear, you must first know that div is a block-level element. It occupies an exclusive line in the page and is arranged from top to bottom, which is the legendary flow
① Applied to subsequent elements to clear the impact of floating on subsequent elements.
②Apply to block-level elements
Usage:
Add a blank element (less used)
clearfix, this is a general solution. In fact, an unavailable dot is used to clear the float.
For example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>float abscure</title> <style type="text/css"> body{ width: 400px;line-height: 1.6; } .sample{ width: 100px;line-height: 100px; margin: 3px;text-align: center; background-color: pink; } .sb{ margin: 10px auto; padding: 5px; border: 1px dashed #0f00fa; } .sample{ float: left; } .clearfix:after{ content: '.'; /* 在clearfix后面加入一个 . */ display: block; /* 设置 . 块级元素 */ clear: both; /* 清除浮动效果 */ /*隐藏那个 . */ height: 0; overflow: hidden; visibility: hidden; } /* .clearfix{ zoom: 1; 由于在IE 低版本中,不支持after属性,所以需要增加zoom属性 } */ </style> </head> <body> <div class="sb clearfix"> <div class="sample">float: left;</div> 第十二届ChinaJoy 动漫游戏展7月31号在上海新国际博览中心开幕,导出是站台表演的帅哥美女。 </div> <div class="sb"> 有些游戏商为了吸引人气,还请来了著名的演员、模特前来助阵。以下是一批漂亮的Show Girl现场照片。 </div> </body> </html>