First off, this example will only work in a browser that supports :nth-child
, like Chrome or FireFox. I have an unordered list where the odd numbered list items are floated left and clear left, and the even numbered list items float right and clear right. Like so:
<>光>:>
<ul class="waterfall">
<li style="height: 100px;">1</li>
<li style="height: 200px;">2</li>
<li style="height: 50px;">3</li>
<li style="height: 100px;">4</li>
<li style="height: 200px;">5</li>
<li style="height: 50px;">6</li>
<li style="height: 50px;">7</li>
<li style="height: 50px;">8</li>
</ul>
<>CSS:
.waterfall {width:302px;}
.waterfall LI {
min-width: 150px;
background-color: #CCC;
margin-top: 2px;
}
.waterfall LI:nth-child(odd) {
float: left;
clear: left;
text-align: right;
}
.waterfall LI:nth-child(even) {
float: right;
clear: right;
text-align: left;
}
<http://jsfiddle.net/bryandowning/HNY9r/"rel=“nofollow> The fiddle.
Goal:
所有奇数清单项目都应在左边上浮动,相互之间排在顶上,除2px差值外,没有差距。 即便是编号清单的项目也是如此,但应当加以浮动。
Questions a nutshell:
I m confused as to exactly why, in this example, LI #5 can t appear above LI #4 and LI #8 can t appear above LI #7. In other words, why is LI #5 clearing LI #2, and LI #8 clearing LI #5? Also, why doesn t LI #3 clear LI #2 (I don t want it to, but if LI #5 is clearing LI #2, why doesn t LI #3 join the party)?
http://www.ohchr.org。
我迄今的想法有些不确定。 看来,如果在标记中后面界定了第一个要素,则浏览器不会让一个要素的顶端出现在另一个要素的顶端。 情况如何?
I understand that floated elements are placed on a line, and that any floated elements not fitting on that line will just jump down to a new line and flow from there (as opposed to the free-flow concept of absolutely positioned elements). In other words, a floated element is removed from the document flow, but not quite as literally as an absolutely positioned element, because the floated element is still positioned relative to whatever line it started from and it still takes up space in the document flow. So, how is the next "line" from which floated elements hang from determined? In this example, why isn t LI #3 clearing LI #2, but LI #5 is?
www.un.org/spanish/ecosoc I m NOT:
A clever solution to make this layout work by changing the markup, abandoning floats, or using JavaScript. I know how to do all that. I simply wish to be schooled in the mysterious ways of the float.