<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Garrick Cheung &#187; clear float</title>
	<atom:link href="http://www.garrickcheung.com/tag/clear-float/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.garrickcheung.com</link>
	<description>Sharing what I know and learn about CSS, MooTools, Javascript, PHP and etc.</description>
	<lastBuildDate>Fri, 30 Sep 2011 05:16:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>CSS Best Practice #3: Clear Floated Containers (with Height&#124;&#124;Width and Overflow)</title>
		<link>http://www.garrickcheung.com/css/css-best-practice-3-clear-floated-containers-with-heightwidth-and-overflow/</link>
		<comments>http://www.garrickcheung.com/css/css-best-practice-3-clear-floated-containers-with-heightwidth-and-overflow/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 01:00:21 +0000</pubDate>
		<dc:creator>Garrick</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML / XHTML]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[clear float]]></category>
		<category><![CDATA[hasLayout]]></category>
		<category><![CDATA[overflow]]></category>

		<guid isPermaLink="false">http://www.garrickcheung.com/?p=217</guid>
		<description><![CDATA[I know you&#8217;ve done it before. I think we&#8217;ve all done it. You have a container with a floated element inside. The container doesn&#8217;t wrap around everything, thus causing the element to break outside of the container. To fix this, we&#8217;d usually add a clear element at the bottom of the container so it would [...]]]></description>
			<content:encoded><![CDATA[<p>I know you&#8217;ve done it before. I think we&#8217;ve all done it. You have a container with a floated element inside. The container doesn&#8217;t wrap around everything, thus causing the element to break outside of the container. To fix this, we&#8217;d usually add a clear element at the bottom of the container so it would wrap everything within. But there is a better solution, which uses two CSS properties. For this example, I&#8217;ll place the styles inline.. please don&#8217;t hate me.<span id="more-217"></span></p>
<p><strong>If you want to just get to the <a href="#solution">better solution&#8230;</a></strong></p>
<h3>The Example</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;container&quot; style=&quot;border:1px solid #000;&quot;&gt;
    &lt;img src=&quot;something.gif&quot; height=&quot;50&quot; width=&quot;50&quot; alt=&quot;Alt img txt&quot; style=&quot;background:green; float:left;&quot; /&gt;
    &lt;p style=&quot;margin:0;&quot;&gt;Text inside the container div.&lt;/p&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<p>Proceeding with the example, div.clear is placed at the bottom of div.container, which solves the problem. Div.container&#8217;s border now wraps all the elements within.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;container&quot; style=&quot;border:1px solid #000;&quot;&gt;
    &lt;img src=&quot;something.gif&quot; height=&quot;50&quot; width=&quot;50&quot; alt=&quot;Alt img txt&quot; style=&quot;background:green; float:left;&quot; /&gt;
    &lt;p style=&quot;margin:0;&quot;&gt;Text inside the container div.&lt;/p&gt;
    &lt;div class=&quot;clear&quot; style=&quot;clear:both;&quot;&gt;&lt;/div&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<h3><a name="solution"></a>The Better Solution</h3>
<p>But why should we even bother placing an extra element? It&#8217;s not sexy. Here&#8217;s the solution: Use height:1% or width:100% and overflow:auto||hidden on div.container. Height and width on div.container would set the hasLayout property in IE, thus fixing a slew of float/box-model bugs (<a href="http://www.satzansatz.de/cssd/onhavinglayout.html" target="_blank">more on hasLayout</a>). Overflow auto or hidden tells div.container to wrap everything within it.</p>
<p><strong>Note:</strong> This won&#8217;t work if the element is floated, which would cause it to shrink to the percentage height or width.</p>
<p>The HTML</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;container&quot; style=&quot;border:1px solid #000;&quot;&gt;
    &lt;img src=&quot;something.gif&quot; height=&quot;50&quot; width=&quot;50&quot; alt=&quot;Alt img txt&quot; style=&quot;background:green; float:left;&quot; /&gt;
    &lt;p style=&quot;margin:0;&quot;&gt;Text inside the container div.&lt;/p&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<p>The CSS</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">div<span style="color: #6666ff;">.container</span> <span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">1%</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span><span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.garrickcheung.com/css/css-best-practice-3-clear-floated-containers-with-heightwidth-and-overflow/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

