marknadal marknadal - 1 year ago 413
CSS Question

Make child visible outside an overflow:hidden parent

In CSS the

is set on parent containers in order to allow it to expand with the height of their floating children.

But it also has another interesting feature when combined with
margin: auto

If PREVIOUS sibling is a floating element, it will actually appear juxtapose to it. That is if the sibling is
then the container with
float:none overflow:hidden
will appear to the right of the sibling, no newline - just as if it was floating in the normal flow. If the previous sibling is
then the container will appear to the left of the sibling. Resizing this container will accurately show it centered inbetween the floating elements. Say if you have two previous siblings, one
the other
, the container will appear centered inbetween the two.

So here's the problem...

How do I maintain that type of layout WITHOUT masking children?

Googling all over the web gives me ways on how to
and expand a container... but I can't find any alternative solution to maintaining the left/right previous-child centering. If you make the container
then the container suddenly ignores the layout flow of the floating elements and appears layered ontop of the floating element.

So question:

I have to have the container
to preserve layout...

how can I make it so the children aren't masked? I need to have the child absolutely positioned relative to the parent outside the container.


How do I
so I can absolutely position a child relative to the parent outside the container... YET preserve the sibling float-like-layout-flow?

Answer Source

You can use the clearfix to do "layout preserving" the same way overflow: hidden does.

.clearfix:after {
    content: ".";    
    display: block;    
    height: 0;    
    overflow: hidden; 
.clearfix:after { clear: both; }
.clearfix { zoom: 1; } /* IE < 8 */

add class="clearfix" class to the parent, and remove overflow: hidden;