Common property and attribute values for box models
/* Basic Properties */padding:padding-left/right/top/bottomborder:border-left/right/top/bottom Border-radiusmargin: margin-left/right/top/bottom/* positioning */position //Put an element in static, relative relative, absolute absolute, fixed position Top bottom //element offset from the left and right of the original position overflow //Set element overflow what happened to its area clip //Set element display shape Vertical-align // Set how elements are aligned vertically Z-index //Set the stacking order of elements/* Floating */float //floating left right none inheritclear //Remove floating left right both inherit 123456789101112131415161718
CSS Floating
The first thing to know is that Div is a block-level element that has a single line in the page, arranged from top to bottom, which is the legendary stream. Such as
As you can see, even if the width of the div1 is very small, the line in the page can tolerate Div1 and div2,div2 and will not be ranked behind Div1, because the DIV element is exclusive.
Note that these theories refer to the div in the standard flow. No matter how complex the layout, the basic starting point is: "How to display multiple div elements on one line."
It is clear that the standard flow has not been able to meet the demand, it will use floating. Floating can be understood as 让某个div元素脱离标准流,漂浮在标准流之上,和标准流不是一个层次 . For example, assuming that the div2 floats, then it will be out of the standard flow, but Div1, Div3, Div4 are still in the standard flow, so Div3 will automatically move up, occupy the div2 position, and re-form a stream.
As can be seen, because of the div2 set floating, so it no longer belongs to the standard flow, Div3 automatically up the displacement div2 position, Div1, Div3, div4 in order to become a new stream. And because the float is floating on the standard flow, so div2 block a part of the div3,div3 appears to be "short".
The div2 used here is 左浮动(float:left;),可以理解为漂浮起来后靠左排列 that the right float (float:right;) is, of course, arranged on the right. Left and right are the left and right edges of the page.
If we use the div2 to float right, it will be as follows:
At this time div2 by the right edge of the page arrangement, no longer obscure div3, the reader can clearly see the above-mentioned Div1, Div3, div4 composition of the flow.
So far we have only floated a DIV element (div2), if it floats multiple div?
Let's add the Div2 and Div3 to the left floating, the effect
Similarly, because Div2, div3 float, they no longer belong to the standard flow, so div4 will automatically move up, and Div1 form a "new" standard flow, and the float is floating on the standard flow, so div2 block div4.
When the Div2, Div3 set floating, DIV3 will follow the div2, do not know if the reader has found, until now, div2 in each case is floating, but did not follow the DIV1. Therefore, we can draw an important conclusion:
If a DIV element A is floating, if the previous element of a element is also floating, then a element will follow the previous element (if the two elements are not placed in a row, the a element will be squeezed to the next line), and if the previous element of a element is an element in the standard flow, the relative vertical position of a will not change. This means that the top of a is always aligned with the bottom of the previous element. 1
To help the reader understand, give a few more examples.
If we set Div2, Div3, div4 to the left float, the effect is as follows:
According to the above conclusion: starting from DIV4 analysis, it found that the upper element div3 is floating, so div4 will follow Div3, DIV3 found the upper element div2 is also floating, so Div3 will follow the Div2 While Div2 found that the upper element div1 is an element in the standard flow, the relative vertical position of the div2 is unchanged, and the top is still aligned with the bottom of the DIV1 element. Because it is left floating, the left side is near the edge of the page, so the left side is front, so Div2 is on the left.
If the Div2, Div3, Div4 are set to the right floating, the effect is as follows:
The truth and left floating basically the same, just need to pay attention to the corresponding relationship. Because it is floating right, so the right side is near the edge of the page, so the right side is front, so div2 on the far right.
If we only float the div2 and div4 to the left, the following:
Still according to the conclusion, Div2, Div4 floating, out of the standard flow, so Div3 will automatically move up, and div1 constitute a standard flow. Div2 found that the previous element div1 is an element in the standard flow, so the div2 is not changed relative to the vertical position, aligned with the bottom of the div1. Div4 found that the previous element div3 is the element in the standard flow, so the top of the DIV4 is aligned with the bottom of the div3, and is always true, as it can be seen that div3 moves up after the div4, Div4 always guarantees its top and last element Div3 (elements in the standard flow) The bottom of the alignment.
At this point, congratulations to the reader has mastered the addition of floating, but there are clear floating, there is a basic clear floating on the top is very easy to understand.
Through the above study, you can see: 元素浮动之前,也就是在标准流中,是竖向排列的,而浮动之后可以理解为横向排列,清除浮动可以理解为打破横向排列 .
Clear floating: syntax: Clear:none | Left | Right | Both none: Default, allows floating objects on both sides left: no floating object allowed on the right: No floating object is allowed on the both: floating objects are not allowed. 12345678
According to the above basis, if there are only two elements in the page div1, Div2, they are left floating, the scene is as follows:
At this time div1, div2 are floating, according to the rules, DIV2 will follow the div1 behind, but we still hope that div2 can be arranged under the div1, like Div1 no float, div2 left floating. This is the time to use clear float, and if it's purely official, the reader might try to write: Add Clear:right to the Div1 CSS style, and understand that floating elements are not allowed on the right side of the DIV1, because Div2 is a floating element, Therefore, a row is automatically moved down to satisfy the rule. In fact, this understanding is not correct, this does not have any effect.
对于CSS的清除浮动(clear),一定要牢记:这个规则只能影响使用清除的元素本身,不能影响其他元素。
How do you understand it? Take the above example, we want to let div2 move, but we are using the DIV1 element CSS style to clear the float, trying to remove the floating element on the right side of the Div1 (clear:right;) to force the Div2 down, which is not feasible, Because this clear float is called in Div1, it can only affect div1 and cannot affect div2.
For Div2 to move down, you must use the float in the div2 CSS style. In this example, the left side of Div2 has a floating element div1, so as long as the clear:left is used in the Div2 CSS style, to specify that the DIV2 element is not allowed floating elements to the left, so Div2 is forced to move down one line.