how to make a div to wrap two float divs inside?

125,805

Solution 1

It's a common problem when you have two floats inside a block. The best way of fixing it is using clear:both after the second div.

<div style="display: block; clear: both;"></div>

It should force the container to be the correct height.

Solution 2

Aside from the clear: both hack, you can skip the extra element and use overflow: hidden on the wrapping div:

<div style="overflow: hidden;">
    <div style="float: left;"></div>
    <div style="float: left;"></div>
</div>

Solution 3

This should do it:

<div id="wrap">
  <div id="nav"></div>
  <div id="content"></div>
  <div style="clear:both"></div>
</div>

Solution 4

overflow:hidden (as mentioned by @Mikael S) doesn't work in every situation, but it should work in most situations.

Another option is to use the :after trick:

<div class="wrapper">
  <div class="col"></div>
  <div class="col"></div>
</div>

.wrapper {
  min-height: 1px; /* Required for IE7 */
  }

.wrapper:after {
  clear: both;
  display: block;
  height: 0;
  overflow: hidden;
  visibility: hidden;
  content: ".";
  font-size: 0;
  }

.col {
  display: inline;
  float: left;
  }

And for IE6:

.wrapper { height: 1px; }

Solution 5

Float everything.

If you have a floated div inside a non-floated div, everything gets all screwy. That's why most CSS frameworks like Blueprint and 960.gs all use floated containers and divs.

To answer your particular question,

<div class="container">
<!--
.container {
  float: left;
  width: 100%;
}
-->
  <div class="sidebar">
  <!--
  .sidebar {
    float: left;
    width: 20%;
    height: auto;
  }
  -->
  </div>
  <div class="content">
  <!--
  .sidebar {
    float: left;
    width: 20%;
    height: auto;
  }
  -->
  </div>
</div>

should work just fine, as long as you float:left; all of your <div>s.

Share:
125,805

Related videos on Youtube

WilliamLou
Author by

WilliamLou

Updated on July 05, 2022

Comments

  • WilliamLou
    WilliamLou almost 2 years

    I don't know if it's a common problem, but I can't find the solution in web so far. I would like to have two divs wrapped inside another div, however these two divs inside have to be align the same level (for example: left one takes 20%width of the wrappedDiv, right one take another 80%). To achieve this purpose, I used the following example CSS. However, now the wrap DIV didn't wrap those divs all. The wrap Div has a small height than those two divs contained inside. How could I make sure that the wrap Div has the largest height as the contained divs? Thanks!!!

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
        <title>liquid test</title>
        <style type="text/css" media="screen">
            body
            {
                margin: 0;
                padding: 0;
                height:100%;
            }
            #nav
            {
                float: left;
                width: 25%;
                height: 150px;
                background-color: #999;
                margin-bottom: 10px;
            }
    
            #content
            {
                float: left;
                margin-left: 1%;
                width: 65%;
                height: 150px;
                background-color: #999;
                margin-bottom: 10px;
            }       
            #wrap
            {
              background-color:#DDD;
              height:100%;
            }
    
    </style>
    </head>
    <body>
    <div id="wrap">
        <h1>wrap1 </h1>
        <div id="nav"></div>
        <div id="content"><a href="index.htm">&lt; Back to article</a></div>
    </div>
    </body>
    </html>
    
  • mbillard
    mbillard over 14 years
    A div is already a block element so you can remove that from your style.
  • Bronx
    Bronx over 14 years
    I like the overflow:hidden approach best.
  • tybro0103
    tybro0103 about 14 years
    I'm so glad someone put this on here... it drives me crazy when people add an element just to clear the floats when its just a css issue.
  • Alex Dn
    Alex Dn over 11 years
    It works with overflow:auto; to. I think it's hack with overflow more elegant "hack" then div clear :)
  • Kos
    Kos over 11 years
    This has some drawbacks, this site also recommends .container { word-wrap: break-word; } and .container img { max-width: 100%; height: auto; } to make big content behave well.
  • A. Sallai
    A. Sallai over 10 years
    This doesn't seem to work on my body element in which i have two floated divs. The body doesnt take up the size of the inner elements even with overflow:hidden. I always used this approach, no idea why it doesn't work, plz help!
  • eugene82
    eugene82 about 10 years
    overflow: auto; also does the job
  • crush
    crush over 9 years
    I wouldn't call it clear: both a hack really. It does exactly what it's expected to do. It is code bloat, though.