This time I will bring you the layout method of the web pageClear floating, what are theprecautionsfor clearing floating, the following is a practical case, let's take a look.
The height problem of the box
1. The height of the box in the standard flow can be supported by the content height;
2. The floating content in the floating flow cannot support the height of the box;
Why do we need to be clear about floating?
Between adjacent boxes, if the front box has no height, then the floating element in the back box will look for the floating element in the front box. This will It leads to confusion in the interface, so it is necessary to clear the float;
Clear the float method one:
Solution:
Set the height of the previous parent element
Notes :
In enterprise development, we can write height without writing height, so this method is rarely used;
CSS:
body:
我是文字1
我是文字1
我是文字1
我是文字2
我是文字2
我是文字2
Clear floating method two:
Solution:
Add the clear:both; attribute to the back box
clear attribute value:
none: Default value, sort according to the sorting rules of floating elements (left floating elements look for left floating elements, right floating elements look for right floating elements)
left: Do not look for the previous left floating element (that is: do not compare it with the previous left floating element) Floating elements are displayed in one line)
right: Do not look for the previous right floating element
both: Do not look for the previous left floating element and right floating element
Note:
When After we add the clear attribute to an element, the margin attribute of this attribute will become invalid; so it is not recommended to use
CSS:
Clear floating method three:
Solution:
Exterior wall method: add an additional block-level element between two boxes with floating child elements; and set the clear: both; attribute;
Notes:
Exterior wall method allows the second box to use the margin-top attribute,
Exterior wall method does not allow the first box to use themargin-bottomattribute,
However The margin effect can be achieved by setting the height of additional tags;
This technique is widely used in Sohu, but due to the need to add a large number of meaningless tags, it is not recommended;
CSS:
//外墙法:在两个有浮动子元素的盒子之间添加一个额外的块级元素;我是文字1
我是文字1
我是文字1
我是文字2
我是文字2
我是文字2
Clear floating method four:
Solution:
Inner wall method:
1Add an additional block-level element at the end of all child elements in the first box,
2Set the clear: both; attribute to this additional block-level element
Note:
The inner wall method allows the second box to use the margin-top attribute
The inner wall method allows the first box to use the margin-bottom attribute
内墙法会自动撑起盒子的高度,所以可以直接设置margin属性
The difference between the exterior wall method and the interior wall method?
The exterior wall method cannot support the height of the first box. The inner wall method can support the height of the first box
In enterprise developmentThe partition methodis not commonly used to clear floating (partition wall method: external wall method and internal wall method) Wall method)
CSS:
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
The above is the detailed content of Web page layout method: clear floating. For more information, please follow other related articles on the PHP Chinese website!