Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7888597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:52:21+00:00 2026-06-03T05:52:21+00:00

First off, this example will only work in a browser that supports :nth-child ,

  • 0

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:

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’s the fiddle.

Goal:

All odd numbered list items should be floated to the left and be stacked on top of one another with no gap in between except the 2px margin. The same is true with the even numbered list items, except they should be floated right.

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)?

TL;DR

My thoughts so far are somewhat inconclusive. It appears that the browser doesn’t want the top of an element to appear above the top of another element if the first element is defined later in the markup. Is that true?

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?

What I’m NOT looking for:

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:52:22+00:00Added an answer on June 3, 2026 at 5:52 am

    From the CSS 2.1 spec:

    Here are the precise rules that govern the behavior of floats:

    1 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.

    2 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.

    3 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.

    4 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.

    5 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.

    6 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.

    7 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.

    8 A floating box must be placed as high as
    possible.

    9 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.

    So

    It appears that the browser doesn’t want the top of an element to
    appear above the top of another element if the first element is
    defined later in the markup. Is that true?

    Yes. It’s covered by rules 5 and 6 above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off I would like to say that this is my first post and
First off the html row looks like this: <tr class=evenColor> blahblah TheTextIneed blahblah and
First off, this is NOT a duplicate of: Turn a C string with NULL
First off, I'm aware this is a bad practice and I have answered many
First off, I am running this on a dual core 2.66Ghz processor machine. I
First off - hello, this is my first Stack Overflow question so I'll try
First off, I apologize if this has been asked before. I can't seem to
First off, sorry if this is a stupid question. I installed code::blocks to try
First off, thanks in advance for your help. This issue is driving me nuts.
This one is a huge issue: first off, while I know a little bit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.