CSS float and auto height

Night

How to get height:auto; for a parent element when child elements use float:left; or float:right?

parent

#parent {
    width:100px;
    height:auto;
        padding-bottom:10px;
        background:#0F0;
}

child_left

#child_left {
    width:50%;
    height:50px;
        float:left;
            background:#00F;
}

fiddle: http://jsfiddle.net/27EWw/

Josh Crozier

The default height of an element is auto, but it seems like are you looking for a clearfix. If an element's children are floated, they are essentially taken out of the flow of the document, therefore if the parent doesn't have defined dimensions, it will collapse upon itself. You could add overflow:auto to the parent element:

EXAMPLE HERE

#parent {
    width:100px;
    overflow:auto;
    padding-bottom:10px;
    background:#0F0;
}

Alternatively you could also use a clearfix:

EXAMPLE HERE

#parent:after {
    clear:both;
    display:table;
    content:'';
}

You could also have a clearfix class, either options would work. If you wanted to avoid these options, you would simply have to set dimensions on the parent element in the first place. Obviously, this isn't always the best option as some elements will have children with varying dimensions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

CSS float and auto height

From Dev

use auto height with float

From Dev

CSS height auto have no height

From Dev

CSS - Auto fill height

From Dev

CSS DIVS and Auto height

From Dev

CSS Transition from height auto to height 75%

From Dev

CSS Navigation bar auto height

From Dev

CSS Div auto increase height

From Dev

Div auto height not working - CSS

From Dev

Using overflow-y with height auto in css

From Dev

CSS Background image not showing with height auto

From Dev

My css height auto doesn't work

From Dev

CSS3 transition height auto to 100%

From Dev

HTML/CSS svg auto height & width

From Dev

Layout (float) different height blocks with css

From Dev

css vertical align within float and height 100%

From Dev

CSS float left with margin auto on div?

From Java

How can I transition height: 0; to height: auto; using CSS?

From Dev

In CSS, how to compute the percentage height within a `height:auto` parent?

From Java

CSS - Expand float child DIV height to parent's height

From Dev

CSS auto column-count when max-height is fixed

From Dev

CSS image height auto causing incorrect aspect ratio

From Dev

Responsive CSS align DIV below another DIV with height:auto

From Dev

CSS Auto-height Div doesn't position itself correctly

From Dev

auto sizing image in div with css works only with width, not height

From Dev

CSS: div with display:table; within a list element no auto height?

From Dev

HTML/CSS Textarea height auto? Text is falling behind

From Dev

css how to make columns auto fill background color to height 100%

From Dev

CSS line-height not affecting float:right element