How to auto adjust height of div?

Discussion in 'CSS' started by miexl, Aug 15, 2009.

  1. #1
    I have a code just like this:

    
    <div>
            <div style="float: left;">
                  This is a dynamic content with 50 - 5000 words
            </div>
    </div>
    
    Code (markup):
    I have a floating div inside another div. What I want is that the outside div will adjust the height automatically (just like table) when the content is long.

    Please help
     
    miexl, Aug 15, 2009 IP
  2. c4gamerz

    c4gamerz Well-Known Member

    Messages:
    294
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Well you dont have to do anything as it will auto expand but if you set height like 300px to main div than you may get trouble.
     
    c4gamerz, Aug 15, 2009 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    What you are looking for is called float wrapping. Many people (we call them idiots) use a complex class called "clearfix"... me?

    overflow:hidden; /* wrap floats */
    zoom:1; /* trip haslayout, wrap floats IE6/earlier */

    That will make the outer div obey the height of children elements floating inside it. You could also use width:100% to trip haslayout assuming you are not setting any padding or borders.
     
    deathshadow, Aug 15, 2009 IP
  4. Dan Schulz

    Dan Schulz Peon

    Messages:
    6,032
    Likes Received:
    436
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Or even height: 1%; served to IE 6 and earlier (though if you code it right, there won't be a need to separate it from IE 7 and later).
     
    Dan Schulz, Aug 15, 2009 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    As long as the element around it doesn't have a fixed height - then height:1% can cause problems.
     
    deathshadow, Aug 17, 2009 IP
  6. Dan Schulz

    Dan Schulz Peon

    Messages:
    6,032
    Likes Received:
    436
    Best Answers:
    0
    Trophy Points:
    0
    #6
    True Jason, but since when do I ever have to deal with THAT? ;)
     
    Dan Schulz, Aug 17, 2009 IP
  7. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Don't tell people to blindly use overflow: hidden. Wait til they start adding dimensions to those outer boxes and suddenly get all their overflowing content getting cut off. Like off-to-the-side CSS tooltips.

    Overflow: hidden is my favourite float-encloser but it has a Day Job. Noobs don't know that.
     
    Stomme poes, Aug 18, 2009 IP