Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Graham. What you describe is the default behavior of a DIV, or any block element for that matter. e.g. for the following HTML:</p> <pre><code>&lt;style type="text/css"&gt; dl { margin: 0; padding: 0;} #container { background-color: blue; padding: 5px 5px 5px 5px; } #inner { background-color: red; } &lt;/style&gt; &lt;div id="container"&gt; &lt;div id="inner"&gt; &lt;dl&gt; &lt;dt&gt;Stuff&lt;/dt&gt; &lt;dd&gt;Blah blah blah&lt;/dd&gt; &lt;dt&gt;Foobar&lt;/dt&gt; &lt;dd&gt;Bazquux&lt;/dd&gt; &lt;/dl&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>You will get the following rendered HTML:</p> <p><a href="http://c0180871.cdn.cloudfiles.rackspacecloud.com/normal.png">Basic Div http://c0180871.cdn.cloudfiles.rackspacecloud.com/normal.png</a></p> <p>The situation you describe when the container div doesn't expand to contain the inner div occurs when you have <em>floated</em> the inner div. Floating, by definition, breaks a block element out of the constraints of it's containing element. Applying "float: left;" to your #inner element gives the following:</p> <p><a href="http://c0180871.cdn.cloudfiles.rackspacecloud.com/floated.png">alt text http://c0180871.cdn.cloudfiles.rackspacecloud.com/floated.png</a></p> <p>The solution is to add a block level element at the bottom of the containing div that clears the floated element. This causes the containing div to wrap around this new block level element, and thus your floated elements as well.</p> <p>e.g.</p> <pre><code>&lt;style type="text/css"&gt; dl { margin: 0; padding: 0;} #container { background-color: blue; padding: 5px 5px 5px 5px; } #inner { background-color: red; float: left; } &lt;/style&gt; &lt;div id="container"&gt; &lt;div id="inner"&gt; &lt;dl&gt; &lt;dt&gt;Stuff&lt;/dt&gt; &lt;dd&gt;Blah blah blah&lt;/dd&gt; &lt;dt&gt;Foobar&lt;/dt&gt; &lt;dd&gt;Bazquux&lt;/dd&gt; &lt;/dl&gt; &lt;/div&gt; &lt;div style="clear: both;"&gt;&lt;/div&gt; &lt;/div&gt; </code></pre> <p>This will give output identical to the first image.</p> <p>Obviously, this can be a tedious thing to add to the bottom of your container divs if you do a lot of floating. </p> <p>Using CSS2 you can do this with a simple class definition (and a hack for IE of course):</p> <pre><code>&lt;style type="text/css"&gt; dl { margin: 0; padding: 0;} #container { background-color: blue; padding: 5px 5px 5px 5px; } .clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; } * html .clearfix {height: 1%;} #inner { background-color: red; float: left; } &lt;/style&gt; &lt;div id="container" class="clearfix"&gt; &lt;div id="inner"&gt; &lt;dl&gt; &lt;dt&gt;Stuff&lt;/dt&gt; &lt;dd&gt;Blah blah blah&lt;/dd&gt; &lt;dt&gt;Foobar&lt;/dt&gt; &lt;dd&gt;Bazquux&lt;/dd&gt; &lt;/dl&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>Simply add the <strong>clearfix</strong> class to any of your container divs that contain floated elements. Note the "* html" is the hack required by IE.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload