注:本文是对众多博客的学习和总结,可能存在理解错误。请带着怀疑的眼光,同时如果有错误希望能指出。
如果你喜欢我的文章,可以关注我的私人博客:http://blog-qeesung.rhcloud.com/
入门前端也算是有三个月之久了,发现Float这个属性一直都很迷惑,只是知道一些简单的浮动规则,并没有深入去学习,一旦遇到一点复杂的浮动场景,自己也就懵了。
于是在网上找了数篇关于浮动的博客文章,加上自己理解,在这里总结一下。
w3 css float nine rulesCSS的float才开始学习的时候总是感觉没有任何的规律可言,总是有许多想不明白的地方,"为什么这个div块要在这个div块的下方?","为什么这个浮动元素会是这样对齐?"。
其实Float是有规则可以依据的,Float一共有九个规则来控制浮动元素的行为:
The left outer edge of a left-floating box may not be to the left of the left edge of its containing block. An analogous rule holds for right-floating elements.
If the current box is left-floating, and there are any left-floating boxes generated by elements earlier in the source document, then for each such earlier box, either the left outer edge of the current box must be to the right of the right outer edge of the earlier box, or its top must be lower than the bottom of the earlier box. Analogous rules hold for right-floating boxes.
The right outer edge of a left-floating box may not be to the right of the left outer edge of any right-floating box that is next to it. Analogous rules hold for right-floating elements.
A floating box"s outer top may not be higher than the top of its containing block. When the float occurs between two collapsing margins, the float is positioned as if it had an otherwise empty anonymous block parent taking part in the flow. The position of such a parent is defined by the rules in the section on margin collapsing.
The outer top of a floating box may not be higher than the outer top of any block or floated box generated by an element earlier in the source document.
The outer top of an element"s floating box may not be higher than the top of any line-box containing a box generated by an element earlier in the source document.
A left-floating box that has another left-floating box to its left may not have its right outer edge to the right of its containing block"s right edge. (Loosely: a left float may not stick out at the right edge, unless it is already as far to the left as possible.) An analogous rule holds for right-floating elements.
A floating box must be placed as high as possible.
A left-floating box must be put as far to the left as possible, a right-floating box as far to the right as possible. A higher position is preferred over one that is further to the left/right.
上述内容摘录于此处,咋一看挺复杂,其实真的挺复杂的,对于博主我这种英语渣来说,是挺难理解的,如果文中有翻译或者理解不对的地方,希望大家指正。
CSS Float Rule1原文:The left outer edge of a left-floating box may not be to the left of the left edge of its containing block. An analogous rule holds for right-floating elements.
译文:左浮动的盒子的左边界不会超出容器的左边界,右浮动同理.
以左浮动为例:
html
css
body{ background-color:yellow; } #outer-div{ height:200px; width:200px; background-color:red; } #inner-div{ height:100px; width:100px; background-color:blue; float:left; }
显示效果
100x100的蓝色盒子为左浮动元素,200x200的红色盒子为蓝色盒子的容器,可见蓝盒子的左边界没有超出红色盒子的左边界。
CSS Float Rule2原文:If the current box is left-floating, and there are any left-floating boxes generated by elements earlier in the source document, then for each such earlier box, either the left outer edge of the current box must be to the right of the right outer edge of the earlier box, or its top must be lower than the bottom of the earlier box. Analogous rules hold for right-floating boxes.
译文:如果盒子是左浮动的,那么在html文档中晚出现的左浮动盒子只允许出现在先出现的左浮动盒子的右边或者晚出现的左浮动盒子的顶部必须在早出现左浮动盒子的底部之下。右浮动同理.
以左浮动为例:
html
100 x 100100 x 100100 x 10065 x 10065 x 100100 x 100100 x 100400 x 500
css
#outer-div{ height:400px; width:500px; background-color:red; } #inner-div{ height:100px; width:100px; background-color:blue; float:left; margin:10px; } #inner-div1{ height:65px; width:100px; background-color:pink; float:left; margin:10px; } body{ background-color:yellow }
显示效果:
这里需要解释的有几个地方:
方块五为何不另起一行,放在方块一下面,而是放在方块四的下面? 这里我们将会在Rule8和Rule9解释
方块六和方块1之间为何空着那么宽的空间? 这是因为方块五早出现在方块六之前,所以方块六的顶部不会超出方块五的底部。
CSS Float Rule3原文:The right outer edge of a left-floating box may not be to the right of the left outer edge of any right-floating box that is next to it. Analogous rules hold for right-floating elements.
译文:左浮动盒子的右外边缘可能不在其旁边的任何右浮动框的左外边缘的右边,右浮动同理.
html
150 x 270 float:left100 x 250 float:right400 x 500
css
div{ margin:5px; } #outer-div{ height:400px; width:500px; background-color:red; } #float-left { height:150px; width:270px; float:left; background-color:blue; } #float-right{ height:100px; width:250px; float:right; background-color:blue; } body{ background-color:yellow }
显示效果:
左浮动盒子的宽度(270)+有浮动盒子的宽度(250)> 容器的宽度(500),所以右浮动盒子并没有和左浮动盒子并列,而是在左浮动盒子的下方。
CSS Float Rule4原文:A floating box’s outer top may not be higher than the top of its containing block. When the float occurs between two collapsing margins, the float is positioned as if it had an otherwise empty anonymous block parent taking part in the flow. The position of such a parent is defined by the rules in the section on margin collapsing.
译文:浮动盒子的顶部不能超出容器的顶部边界
这个就不用解释了
CSS Float Rule5原文:The outer top of a floating box may not be higher than the outer top of any block or floated box generated by an element earlier in the source document.
译文:浮动盒子的顶部不会超出在html文档中早出现的的块级元素(block)或者是浮动元素的顶部
html
123400 x 500
css
div{ margin:5px; } #outer-div{ height:400px; width:500px; background-color:red; } #float-left { height:100px; width:100px; float:left; background-color:lightblue; } #float-right{ height:100px; width:200px; float:right; background-color:lightyellow; } body{ background-color:yellow }
显示效果:
如果将红色容器的矿都缩短为,使之单行之内不能够容下三个浮动元素,那么哪一个盒子会被挤到下一行呢?1?2?3?
我们来试着将红色容器盒子的宽度缩减为400px
#outer-div{ height:400px; width:400px; background-color:red; }
我们将会得到下面显示效果
可见盒子三被挤到了下一行,这是因为盒子二的html文件中比盒子三早出现,所以盒子二不能低于盒子三。于是就将盒子三下沉。
CSS Float Rule6原文:The outer top of an element’s floating box may not be higher than the top of any line-box containing a box generated by an element earlier in the source document.
译文:浮动盒子的顶部不会超出在html文档中早出现的包含盒子的line-box元素顶部
html
Purus ut faucibus pulvinar elementum integer enim neque, volutpat ac tincidunt vitae, semper quis lectus nulla at volutpat diam ut. Dui vivamus arcu felis, bibendum ut tristique et, egestas quis ipsum suspendisse ultrices gravida dictum fusce ut placerat orci nulla!100 x 100
css
div{ margin:5px; } #outer-div{ height:400px; width:500px; background-color:red; } #float-left { height:100px; width:100px; float:left; background-color:lightblue; } body{ background-color:yellow }
显示效果:
在浮动元素之前是一行行内元素,所以浮动盒子就没有超过文本的最后一行的顶部。可能这里不太清晰,我们将文本换成图片再看一下。
html
100 x 100
显示效果
为什么浮动元素不浮动上去把一个图片元素挤下来呢?如果浮动元素向上浮动将一个图片元素挤到第二行,那么现在浮动元素就高于比它早出现的图片元素。就违反了规则
这里我们试着将红色盒子容器宽度缩小,使之一行之内容不下四个图片。
css
#outer-div{ height:400px; width:400px; background-color:red; }
那么图片将会折行,如下所示
行内元素图片四就环绕浮动元素,但是浮动元素并没有超出行内元素的。
CSS Float Rule7原文:A left-floating box that has another left-floating box to its left may not have its right outer edge to the right of its containing block’s right edge. (Loosely: a left float may not stick out at the right edge, unless it is already as far to the left as possible.) An analogous rule holds for right-floating elements.
译文:一个左浮动元素右边的另一个左浮动元素的右边界不会超出容器的右边界,右浮动同理
这个也很好理解,就是如果一行之内要放第二个浮动元素,如果放不下了,那就换行。这个就不详细解释啦
CSS Float Rule8CSS Float Rule9原文:A floating box must be placed as high as possible.
译文:一个浮动盒子应该放的尽可能的高
原文:A left-floating box must be put as far to the left as possible, a right-floating box as far to the right as possible. A higher position is preferred over one that is further to the left/right.
译文:一个左浮动元素应该放的尽可能的靠左,右浮动元素应该被放的尽可能的靠右。当元素既可以放置"最高"又可以"最右"的时候,优先考虑"最高"。
上面的两个规则要连起来一起看,这里我们就借鉴一下规则二中的例子
为什么盒子五不直接放在盒子一下面,这是因为当一个元素既可以放的最左(最右)和最高的时候,优先选择最高,所以这里就放在了盒子四的下面。
finally最后说两句,浮动元素其实对在它出现之前的元素影响不大,但是由于浮动是使元素脱离了文档流,那么在浮动元素之后出现的元素:
块元素:直接无视浮动元素,该怎么显示就怎么显示,并且会被浮动元素覆盖。
行内元素:行内元素会环绕在浮动元素周围。
下面我们就来一个简单的例子:
html
P1 : Augue neque, gravida in fermentum et, sollicitudin ac orci phasellus egestas tellus rutrum tellus pellentesque eu tincidunt tortor aliquam nulla?
100 x 100P2 : At lectus urna duis convallis convallis tellus, id interdum velit laoreet id donec ultrices tincidunt arcu, non sodales neque sodales ut etiam. Feugiat vivamus at augue eget arcu dictum varius?
css
div{ margin:5px; } #outer-div{ height:400px; width:500px; background-color:red; } #float-left { height:100px; width:100px; float:left; background-color:lightblue; } body{ background-color:yellow }
显示效果
这里因为p1是block元素,直接卡住了浮动元素,然后p2这个block元素直接忽视了浮动元素的存在,然后排版在浮动元素的下方,但是p2的中的文本是inline元素,于是就围绕浮动元素排布。
参考文章W3 float-rules
Everything You Never Knew About CSS Floats
The CSS Float Rules
CSS浮动float详解
回归CSS标准之Float
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/49936.html
注:本文是对众多博客的学习和总结,可能存在理解错误。请带着怀疑的眼光,同时如果有错误希望能指出。如果你喜欢我的文章,可以关注我的私人博客:http://blog-qeesung.rhcloud.com/ 入门前端也算是有三个月之久了,发现Float这个属性一直都很迷惑,只是知道一些简单的浮动规则,并没有深入去学习,一旦遇到一点复杂的浮动场景,自己也就懵了。 于是在网上找了数篇关于浮动的博客文章,加...
摘要:我们可以看一下的可见是由内部支持的,其实现原理上就避免了栈进栈出的消耗,直接由最内层的返回值。另外可以实现外部直接向最内层的传递值,比如这段代码的输出是这样传值的方式,在用循环重新的模式下是无法实现的。这也就是必须使用,而不能使用的原因。 在python 3.3里,generator新增了一个语法 yield from 这个yield from的作用是什么?看下面两段对比的代码: d...
摘要:根据文件名引入。这和指令非常相似,只要后面的条件为就会执行。被编译为循环指令的形式,是个变量名,是一个表达式,他将返回一个列表值。被编译为学完了回过头来整理也真是麻烦,算是总结吧。 前戏:下面这些玩意还是比较实用的,好像是进阶Sass必备的,以后写起 CSS 要溜得飞起啊! 规则(Rules) 1. @import Sass 扩展了 CSS 的 @import 规则,让它能够引入 SC...
摘要:如对应的英文表达为并继续乱序成。要求输入乱序的英文表达式,找出其中包含的所有的数字,并按照从小到大输出。思路和代码首先将数字和英文表示列出来粗略一看,我们知道有许多字母只在一个英文数字中出现,比如只出现在中。 题目要求 Given a non-empty string containing an out-of-order English representation of digits...
阅读 2005·2021-09-07 10:14
阅读 1460·2019-08-30 15:53
阅读 2246·2019-08-30 12:43
阅读 2794·2019-08-29 16:37
阅读 735·2019-08-26 13:29
阅读 1983·2019-08-26 13:28
阅读 422·2019-08-23 18:33
阅读 3401·2019-08-23 16:09