<?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>Code Couch &#187; HTML/XHTML</title>
	<atom:link href="http://www.codecouch.com/tag/htmlxhtml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codecouch.com</link>
	<description>The ramblings of two code monkeys</description>
	<lastBuildDate>Mon, 09 Jan 2012 21:11:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How to use “clearfix” CSS to clear floats without markup</title>
		<link>http://www.codecouch.com/2008/10/how-to-use-clearfix-css-to-clear-floats-without-markup/</link>
		<comments>http://www.codecouch.com/2008/10/how-to-use-clearfix-css-to-clear-floats-without-markup/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 21:43:19 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Front-end]]></category>
		<category><![CDATA[HTML/XHTML]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=165</guid>
		<description><![CDATA[Here we extended the normal rules for clearfix and provide a complete CSS solution to clearing floated elements (ensuring that a parent element inherits it's floated child element's height). A complete clearfix redux.]]></description>
			<content:encoded><![CDATA[<p>When an element is floated, it is taken out of the normal flow of the document. As a result, the element takes up no height (in the normal flow) &#8211; and any wrapping parent element will not automatically resize to accommodate the height of the floated element. Here is some sample HTML that shows this in it&#8217;s simple form:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="html4strict">...
<span class="sc2">&lt;<span class="kw2">div</span> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;parent&quot;</span> <span class="kw3">style</span><span class="sy0">=</span><span class="st0">&quot;background-color:#F00;&quot;</span>&gt;</span>
	<span class="sc2">&lt;<span class="kw2">div</span> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;child&quot;</span> <span class="kw3">style</span><span class="sy0">=</span><span class="st0">&quot;float: left;&quot;</span>&gt;</span>Floated element<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">div</span>&gt;</span>
<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">div</span>&gt;</span>
...</pre></div></div></div>

<p>Given the child element is floated, it doesn&#8217;t affect the height of the parent element &#8211; and so the parent element remains with no height (and the red background colour is never shown).</p>
<p>The <a href="http://www.w3.org/TR/CSS2/visuren.html#floats">W3 describe floating an element</a> and the use of a &#8220;clearing element&#8221; (a block level element with the <b>clear</b> CSS property set). This is not acceptable for all layouts. It would be nicer if all this could be carried out using CSS &#8211; providing a separation of content and layout into the bargain.</p>
<p>Rather than attempt to merely re-word much of what is already online, you can read about how the CSS for this works in more detail at <a href="http://www.positioniseverything.net/easyclearing.html">Position is Everything</a>. Whilst their implementation is slightly different to the one presented below, their description is excellent (and they are credited with bringing together all the threads to make this solution usable).</p>
<p>The <b>clearfix</b> solution below is our current &#8220;best practise&#8221; effort to date. We have switched from using the typical &#8220;.&#8221; to using a space &#8221; &#8221; in the <b>content</b> rule due to <a href="http://perishablepress.com/press/2008/02/05/lessons-learned-concerning-the-clearfix-css-hack/">reports of some layout issues in Firefox</a>.</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="css">&lt;style type<span class="sy0">=</span><span class="st0">&quot;text/css&quot;</span><span class="sy0">&gt;</span>
<span class="re1">.clearfix</span><span class="re2">:after </span><span class="br0">&#123;</span>
	<span class="kw1">clear</span><span class="sy0">:</span> <span class="kw2">both</span><span class="sy0">;</span>
	<span class="kw1">display</span><span class="sy0">:</span> <span class="kw2">block</span><span class="sy0">;</span>
	<span class="kw1">content</span><span class="sy0">:</span> <span class="st0">&quot; &quot;</span><span class="sy0">;</span>
	<span class="kw1">height</span><span class="sy0">:</span> <span class="re3">0px</span><span class="sy0">;</span>
	<span class="kw1">visibility</span><span class="sy0">:</span> <span class="kw2">hidden</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
<span class="re1">.clearfix</span> <span class="br0">&#123;</span>
	<span class="kw1">display</span><span class="sy0">:</span> inline-<span class="kw2">block</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
<span class="coMULTI">/* Hide these rules from IE-mac \*/</span>
<span class="sy0">*</span> html <span class="re1">.clearfix</span> <span class="br0">&#123;</span>
	<span class="kw1">height</span><span class="sy0">:</span> <span class="re3">1%</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
<span class="re1">.clearfix</span> <span class="br0">&#123;</span>
	<span class="kw1">display</span><span class="sy0">:</span> <span class="kw2">block</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
li<span class="re1">.clearfix</span> <span class="br0">&#123;</span>
	<span class="kw1">display</span><span class="sy0">:</span> <span class="kw2">list-item</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
<span class="coMULTI">/* End hide from IE-mac */</span>
&lt;/style<span class="sy0">&gt;</span></pre></div></div></div>

<p>We extended the normal rules to include setting the correct display (list-item) for use on list items (as a direct result of some problems with floated lists). If you don&#8217;t care about IE-mac support (don&#8217;t forget that <a href="http://www.codecouch.com/2005/12/microsoft-finally-retires-ie-5-for-the-mac/">Microsoft finally retired IE 5 for the Mac</a>) then you can avoid using the &#8220;commented backslash hack&#8221; all together.</p>
<p>Going back to our original HTML code, we can place the <b>clearfix</b> class on the parent element to ensure that it inherits the height of it&#8217;s floating child elements (and the red background will now show):</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="html4strict">...
<span class="sc2">&lt;<span class="kw2">div</span> <span class="kw3">class</span><span class="sy0">=</span><span class="st0">&quot;clearfix&quot;</span> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;parent&quot;</span> <span class="kw3">style</span><span class="sy0">=</span><span class="st0">&quot;background-color:#F00;&quot;</span>&gt;</span>
	<span class="sc2">&lt;<span class="kw2">div</span> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;child&quot;</span> <span class="kw3">style</span><span class="sy0">=</span><span class="st0">&quot;float: left;&quot;</span>&gt;</span>Floated element<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">div</span>&gt;</span>
<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">div</span>&gt;</span>
...</pre></div></div></div>

<p>You can read more about the <b>clearfix</b> solution all over the place, including:</p>
<ul>
<li><a href="http://www.positioniseverything.net/easyclearing.html">Position is Everything</a></li>
<li><a href="http://www.456bereastreet.com/archive/200603/new_clearing_method_needed_for_ie7/">456 Berea Street</a></li>
<li><a href="http://www.sitepoint.com/blogs/2005/02/26/simple-clearing-of-floats/">Sitepoint</a></li>
</ul>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=How+to+use+%E2%80%9Cclearfix%E2%80%9D+CSS+to+clear+floats+without+markup+http%3A%2F%2Fis.gd%2FVKtPYo" title="Post to Twitter"><img class="nothumb" src="http://www.codecouch.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-big4.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2008/10/how-to-use-clearfix-css-to-clear-floats-without-markup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

