Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does order of float div and non-float div matter only in some cases?

I have a similar problem as CSS Auto Margin pushing down other elements: A right floating sidebar gets pushed down below the main non-floating content div. The answer proposed works: just reverse the order of the markup and write out the float div before the non-float div.

For example, this:

<div class="container">
    <div id="non-floating-content">
        fooburg content
    </div>
    <div id="float-right">
        test right
    </div>
</div>

needs to be awkwardly re-ordered as:

<div class="container">
    <div id="float-right">
        test right
    </div>
    <div id="non-floating-content">
        fooburg content
    </div>
</div>

So then, why does this also work without reordering: Elastic layout with max-width and min-width using grid based design? Check out the live demo. The ordering of the markup is still sensible: the float div is written out after the non-float div. Yet the float doesn't get pushed down on the page.

I ask because I would prefer not to have to hack the theme PHP (to reorder divs) in order to properly style it.

Other posts that also say the solution is to "re-order your divs":

  • 2 column div layout: right column with fixed width, left fluid
  • Semi Fluid Layout CSS/Html
like image 927
huyz Avatar asked Jul 06 '11 03:07

huyz


People also ask

What are the disadvantages of float in CSS?

Floating everything can make for a great deal of inconsistency; depending on how wide a screen is and how tall certain elements are rendered, you can end up with a hodgepodge of screen jag. For example, widening the screen would cause more elements to fit on the top line, so they would jump up.

Can clear property apply to floating and non floating elements in CSS?

The clear property applies to floating and non-floating elements.

Why should we use float property in CSS?

The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow (in contrast to absolute positioning).

Does float work on Div?

Its most common use is to wrap text around images. However, you can use the float property to wrap any inline elements around a defined HTML element, including lists, paragraphs, divs, spans, tables, iframes, and blockquotes.


1 Answers

The reason this works is because your containing element has no height. When you have nothing but floated elements inside a containing element, it will collapse to 0 height. If you were, for example, to add overflow: hidden; to #fluidColumnContainer, it would act as a clear-fix, expanding the container to contain the floated elements. Then you would see the right-floated element drop down again.

like image 126
kinakuta Avatar answered Oct 21 '22 00:10

kinakuta