Initially, this example will only work in a browser that supports :nth-child
, such as Chrome or FireFox. I have an unordered list in which elements with an odd number move left and left, and elements with an even list float on the right and on the right. For instance:
HTML:
<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; }β
Here is the violin.
Goal:
All elements with an odd number should be placed on the left and stacked on top of each other, with no spaces between them, except for the 2px field. The same applies to items with an even list number, except that they should be floated to the right.
Questions in a nutshell:
I am confused why in this example, LI # 5 cannot appear above LI # 4, and LI # 8 cannot appear above LI # 7. In other words, why is LI # 5 cleaning LI # 2 and LI # 8 cleaning LI # 5? Also, why doesn't LI # 3 clear LI # 2 (I don't want this, but if LI # 5 clears LI # 2, why doesn't LI # 3 join the side)?
TL DR
My thoughts are still somewhat unconvincing. It looks like the browser does not want the top of the element to appear above the top of another element if the first element is defined later in the markup. It's true?
I understand that floating elements are placed on a line and that any floating elements that are not fitted on this line will simply jump to a new line and go from there (unlike the concept of a free stream of absolutely positioned elements), in other words, the moved element is removed from the stream documents, but not quite as literally as an absolutely positioned element, since the floating element is still located relative to any line from which it started, and still takes up space in the document flow NTA. So, how is the next "line" determined from which the elements of the fleet hang? In this example, why does LI # 3 not clear LI # 2, but LI # 5 is?
What I'm not looking for:
A smart solution to create this layout by changing layouts, discarding floats, or using JavaScript. I know how to do it all. I just want to learn the mysterious ways of swimming.