css (float floating and clear Clear)

Tutorial start:

 

       First of all you know, div is a block-level element, a separate line on the page, top-down arrangement, which is legendary stream . As shown below:

 

 

 

       As can be seen, even if the width is small div1, page and line can accommodate div1 div2, div2 div1 not ranked behind, because div element is an exclusive line.

       Note that these theories, refers to the standard stream div.

       Side dishes that no matter how complex the layout, the basic starting point are: " how to display more than one line in the div element ."

       Obviously the standard flow has been unable to meet demand, which use the float.      

       Let float can be understood as a div element from the standard flow, floating above the standard flow, and the flow is not a standard level.

       For example, assume div2 floating above figure, it will flow out of the standard, but div1, div3, div4 stream which remains a standard, so div3 automatically moved upward, occupying the position div2, reconstitute a stream. Figure:

 

 

       As can be seen from the figure, due to the provision of the floating div2, so that it no longer belongs to the standard flow, DIV3 move automatically replace div2 position, div1, div3, div4 sequentially arranged, as a new stream. And because the float is floating in the standard stream, thus blocking part div2 div3, div3 looks change "short" a.

       As used herein div2 is left floating (float: left;), can be understood as the float arranged left and right float (float: right;) of course, is arranged right. Here it's left, right, left of the page is to say, the right edge.

       If we adopt the right div2 float, the effect would be as follows:

 

 

       At this time, the right edge of the page by div2 arrangement, no blocking DIV3, the reader can clearly see the above stated flow div1, div3, div4 thereof.

       目前为止我们只浮动了一个div元素,多个呢?

       下面我们把div2和div3都加上左浮动,效果如图:

 

 

       同理,由于div2、div3浮动,它们不再属于标准流,因此div4会自动上移,与div1组成一个“新”标准流,而浮动是漂浮在标准流之上,因此div2又挡住了div4。

       咳咳,到重点了,当同时对div2、div3设置浮动之后,div3会跟随在div2之后,不知道读者有没有发现,一直到现在,div2在每个例子中都是浮动的,但并没有跟随到div1之后。因此,我们可以得出一个重要结论:

       假如某个div元素A是浮动的,如果A元素上一个元素也是浮动的,那么A元素会跟随在上一个元素的边(如果一行放不下这两个元素,那么A元素会被挤到下一行);如果A元素上一个元素是标准流中的元素,那么A的相对垂直位置不会改变,也就是说A的顶部总是和上一个元素的底部对齐。

       div的顺序是HTML代码中div的顺序决定的。

       靠近页面边缘的一端是,远离页面边缘的一端是

 

 

       为了帮助读者理解,再举几个例子。

       假如我们把div2、div3、div4都设置成浮动,效果如下:

 

 

 

       根据上边的结论,跟着小菜理解一遍:先从div4开始分析,它发现上边的元素div3是浮动的,所以div4会跟随在div3之后;div3发现上边的元 素div2也是浮动的,所以div3会跟随在div2之后;而div2发现上边的元素div1是标准流中的元素,因此div2的相对垂直位置不变,顶部仍 然和div1元素的底部对齐。由于是左浮动,左边靠近页面边缘,所以左边是前,因此div2在最左边。

       假如把div2、div3、div4都设置成浮动,效果如下:

 

 

       道理和左浮动基本一样,只不过需要注意一下前后对应关系。由于是右浮动,因此右边靠近页面边缘,所以右边是前,因此div2在最右边。

       假如我们把div2、div4左浮动,效果图如下:

 

       依然是根据结论,div2、div4浮动,脱离了标准流,因此div3将会自动上移,与div1组成标准流。div2发现上一个元素div1是标准流中的 元素,因此div2相对垂直位置不变,与div1底部对齐。div4发现上一个元素div3是标准流中的元素,因此div4的顶部和div3的底部对齐, 并且总是成立的,因为从图中可以看出,div3上移后,div4也跟着上移,div4总是保证自己的顶部和上一个元素div3(标准流中的元素)的底部对齐

       至此,恭喜读者已经掌握了添加浮动,但还有清除浮动,有上边的基础清除浮动非常容易理解。

       经过上边的学习,可以看出:元素浮动之前,也就是在标准流中,是竖向排列的,而浮动之后可以理解为横向排列。

       清除浮动可以理解为打破横向排列。

       清除浮动的关键字是clear,官方定义如下:

 

       语法:

       clear : none | left | right | both

       取值:

       none  :  默认值。允许两边都可以有浮动对象

       left   :  不允许左边有浮动对象

       right  :  不允许右边有浮动对象

       both  :  不允许有浮动对象

 

       定义非常容易理解,但是读者实际使用时可能会发现不是这么回事。

       定义没有错,只不过它描述的太模糊,让我们不知所措。

       根据上边的基础,假如页面中只有两个元素div1、div2,它们都是左浮动,场景如下:

     此时div1、div2都浮动,根据规则,div2会跟随在div1后边,但我们仍然希望div2能排列在div1下边,就像div1没有浮动,div2左浮动那样。

     这时候就要用到清除浮动(clear),如果单纯根据官方定义,读者可能会尝试这样写:在div1的CSS样式中添加clear:right;,理解为不允许div1的右边有浮动元素,由于div2是浮动元素,因此会自动下移一行来满足规则。

       其实这种理解是不正确的,这样做没有任何效果。看小菜定论:

       对于CSS的清除浮动(clear),一定要牢记:这个规则只能影响使用清除的元素本身,不能影响其他元素。

       怎么理解呢?就拿上边的例子来说,我们是想让div2移动,但我们却是在div1元素的CSS样式中使用了清除浮动,试图通过清除div1右边的浮动元素 (clear:right;)来强迫div2下移,这是不可行的,因为这个清除浮动是在div1中调用的,它只能影响div1,不能影响div2。

       根据小菜定论,要想让div2下移,就必须在div2的CSS样式中使用浮动。本例中div2的左边有浮动元素div1,因此只要在div2的CSS样式中使用clear:left;来指定div2元素左边不允许出现浮动元素,这样div2就被迫下移一行。

      那么假如页面中只有两个元素div1、div2,它们都是右浮动呢?读者此时应该已经能自己推测场景,如下:

       此时如果要让div2下移到div1下边,要如何做呢?

       同样根据小菜定论,我们希望移动的是div2,就必须在div2的CSS样式中调用浮动,因为浮动只能影响调用它的元素。

       可以看出div2的右边有一个浮动元素div1,那么我们可以在div2的CSS样式中使用clear:right;来指定div2的右边不允许出现浮动元素,这样div2就被迫下移一行,排到div1下边。

参考:http://www.cnblogs.com/acorn/p/5249089.html

Guess you like

Origin www.cnblogs.com/yuer20180726/p/11006553.html