<?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>Web Standards Tips &#187; easyclearing</title>
	<atom:link href="http://webstandardstips.com/tag/easyclearing/feed/" rel="self" type="application/rss+xml" />
	<link>http://webstandardstips.com</link>
	<description>Practical advice for building websites with web standards</description>
	<lastBuildDate>Mon, 08 Mar 2010 13:39:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The empty div clearfix anti-pattern</title>
		<link>http://webstandardstips.com/2010/03/08/the-empty-div-clearfix-anti-pattern/</link>
		<comments>http://webstandardstips.com/2010/03/08/the-empty-div-clearfix-anti-pattern/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 13:39:05 +0000</pubDate>
		<dc:creator>Isofarro</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[antipattern]]></category>
		<category><![CDATA[clear]]></category>
		<category><![CDATA[easyclearing]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[markup]]></category>

		<guid isPermaLink="false">http://webstandardstips.com/?p=26</guid>
		<description><![CDATA[The common approach to laying out a page is through the use of float. A container floated left becomes a left-hand sidebar. And even within components of a page, the float is used to create columnar layouts.
The symptom of float-based layouts
One of the side-effects of using floats for layout is that the element being floated [...]]]></description>
			<content:encoded><![CDATA[<p>The common approach to laying out a page is through the use of float. A container floated left becomes a left-hand sidebar. And even within components of a page, the float is used to create columnar layouts.</p>
<h3>The symptom of float-based layouts</h3>
<p>One of the side-effects of using floats for layout is that the element being floated is drawn out of the flow of the content, and the content flows around the floated container. Even though this is the idea behind floats, it&#8217;s not entirely ideal for layout.</p>
<p>One too-long floated element, and other components further down end up being flowed around the floated element until the floated-element has been passed, and then the full width of the parent element is available.</p>
<p>This means that components later on in the page typically need some styling to the effect of <q>don&#8217;t start appearing until after the floated element has been passed</q>. Typically, this is done with a <code>clear</code> CSS property.</p>
<h3>Markup for presentation&#8217;s sake</h3>
<p>Leaving the clearing up of previous floats to the preceding elements isn&#8217;t a wise move. It forces the content container to take into consideration the layout it&#8217;s placed in. And that introduces assumptions and conflicts into layouts. A content container that&#8217;s clearing and previous sibling floats cannot then appear next to a previous siblinb container. So placing two content containers side-by-side is impossible.</p>
<p>A technique that&#8217;s prevalent on the web today is the use of an empty div used for the purposes of clearing previous floats. That way the following containers of content don&#8217;t have to take into account floated elements in the flow before them. The structure of this technique looks something like this:</p>
<pre><code>
&lt;div class="module"&gt;
	... internal elements, including a floated
	elements that may extend longer than the
	non-floated content
&lt;/div&gt;

<strong>&lt;div class="clearfix"&gt;&lt;/div&gt;</strong>

&lt;div class="module"&gt;
	... Next content containter, thanks to the
	previous div.clearfix doesn't need to
	worry about whether a floated element will
	interfere with it's own layout...
&lt;/div&gt;
</code></pre>
<p>The <var>.clearfix</var> is normally styled with <code>clear: left</code>, or <code>clear: both</code> if the layout may contain elements floated rightwards too.</p>
<p>This makes sense in some way. The next element doesn&#8217;t need to deal with extended floating elements, and can assume that it&#8217;s been handled.</p>
<h3>Downsides of solving presentational issues with markup</h3>
<p>There are two main drawbacks to this markup-based approach:</p>
<ul>
<li>It&#8217;s additional markup that has nothing to do with the content on the page. Granted, additional containers are needed for non-trivial layouts, but this should be done grudgingly.</li>
<li>Laying out the two modules side-by-side is now impossible. So the technique has sacrificed layout flexibility. This could now only be done by introducing extra markup.</li>
</ul>
<h3>Redelegating responsibility of clearing floats</h3>
<p>And yet, neither sacrifice is necessary. The content container should clean up after itself, and take responsibility for it&#8217;s child elements.</p>
<p>Simply, the content containers should always clear any contained floats. We&#8217;ve previously discussed <a href="http://webstandardstips.com/2008/11/18/easy-clearing-of-floats/">methods of self-clearing floats</a>. Applying these techniques we can refactor the above code and removing the extraneous empty div, whose purpose is now properly handled by the content container:</p>
<pre><code>
&lt;div class="module"&gt;
	... the div needs to take responsibility
	for it's child content...
&lt;/div&gt;

&lt;div class="module"&gt;
	... Next content containter isn't
	hindered by the previous content ...
&lt;/div&gt;
</code></pre>
<p>And adding in the style rules to self-clear any contained floats:</p>
<pre><code>
.module {
	overflow: hidden;
}
</code></pre>
<p>Note: <code>overflow: hidden</code> is a stable way of clearing floats, but it does have a drawback of not allowing child content to overflow outside the container&#8217;s edges. For the majority of layout scenarios, this is a good thing. In the very few cases where this feature is not desirable, then use an alternative self-clearing method, like the content :after approach, plus an IE-tailored style to force hasLayout.</p>
<h3>Self-sufficiency improves flexibility</h3>
<p>Content containers that clean up after their child elements reduce the maintenance headache. The order of content on the page can be more flexible. And two blocks of content can be placed side-by-side, with space permitting.</p>
<p>Essentially, self-clearing wrappers become a solid foundation, and so allows more intricate styling by isolating the effects of floated elements just to that atomic unit of content.</p>
]]></content:encoded>
			<wfw:commentRss>http://webstandardstips.com/2010/03/08/the-empty-div-clearfix-anti-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easy clearing of floats</title>
		<link>http://webstandardstips.com/2008/11/18/easy-clearing-of-floats/</link>
		<comments>http://webstandardstips.com/2008/11/18/easy-clearing-of-floats/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 16:01:42 +0000</pubDate>
		<dc:creator>Isofarro</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[agrade]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[clear]]></category>
		<category><![CDATA[content:after]]></category>
		<category><![CDATA[easyclearing]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[graded]]></category>
		<category><![CDATA[hasLayout]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[selfclear]]></category>
		<category><![CDATA[support]]></category>

		<guid isPermaLink="false">http://www.webstandardstips.com/?p=3</guid>
		<description><![CDATA[One of the main methods of creating stable layouts is to ensure that elements that contain floated elements clear these floats.
Given an element with an id of wrapper, the styles needed to easy clear any contained floats is:


#wrapper:after {
	content: ".";
	display: block;
	height: 0;
	clear: both;
	visibility: hidden;
}
#wrapper {
	zoom: 1;
}


The :after selector deals with most modern browsers, so Firefox, [...]]]></description>
			<content:encoded><![CDATA[<p>One of the main methods of creating stable layouts is to ensure that elements that contain floated elements clear these floats.</p>
<p>Given an element with an id of wrapper, the styles needed to easy clear any contained floats is:</p>
<pre>
<code>
#wrapper:after {
	content: ".";
	display: block;
	height: 0;
	clear: both;
	visibility: hidden;
}
#wrapper {
	zoom: 1;
}
</code>
</pre>
<p>The <code>:after</code> selector deals with most modern browsers, so Firefox, Safari, Opera (the ones listed as Grade A browsers in Yahoo!&#8217;s <a href="http://developer.yahoo.com/yui/articles/gbs/#gbschart">Graded Browser Support</a> chart). Internet Explorer 6 and 7 ignore this rule, so the second stylerule, the <code>zoom: 1</code> triggers <code>hasLayout</code> in IE6 and 7 which does the self-clearing of floats.</p>
<p>If valid CSS is an absolute and non-negotiable requirement, then <code>zoom: 1</code> is going to be a problem. So instead you could replace the <code>zoom:1</code> selector rule with another style-rule that triggers hasLayout in IE, like <code>height: 0%</code>, or <code>overflow: hidden</code> &#8211; pick your poison.</p>
]]></content:encoded>
			<wfw:commentRss>http://webstandardstips.com/2008/11/18/easy-clearing-of-floats/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

