<?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; layout</title>
	<atom:link href="http://webstandardstips.com/tag/layout/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>CSS shorthands for margin and paddings</title>
		<link>http://webstandardstips.com/2009/06/14/css-shorthands-for-margin-and-paddings/</link>
		<comments>http://webstandardstips.com/2009/06/14/css-shorthands-for-margin-and-paddings/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 19:47:38 +0000</pubDate>
		<dc:creator>Isofarro</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[bottom]]></category>
		<category><![CDATA[consistency]]></category>
		<category><![CDATA[grid]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[left]]></category>
		<category><![CDATA[margin]]></category>
		<category><![CDATA[padding]]></category>
		<category><![CDATA[page]]></category>
		<category><![CDATA[right]]></category>
		<category><![CDATA[shorthand]]></category>
		<category><![CDATA[top]]></category>

		<guid isPermaLink="false">http://webstandardstips.com/?p=19</guid>
		<description><![CDATA[The typical way of setting margins and paddings involve specifiying a value for each of the top, right, bottom and left in sequence. Depending on the situation, we can use shorthand values for both optimisation and clarity, but sometimes it isn&#8217;t always clear.
Note: Using file size optimisation as a basis for a styleguide for writing [...]]]></description>
			<content:encoded><![CDATA[<p>The typical way of setting margins and paddings involve specifiying a value for each of the top, right, bottom and left in sequence. Depending on the situation, we can use shorthand values for both optimisation and clarity, but sometimes it isn&#8217;t always clear.</p>
<p><strong>Note:</strong> Using file size optimisation as a basis for a styleguide for writing CSS is the wrong way to go. Always favour readability and legibility of the code over the number of bytes across the wire. Use a CSS compressor right before the CSS file gets deployed to a production environment; that&#8217;s the right place to optimise file size.</p>
<h3>Specifying all four values</h3>
<p>Specifying all four values one after the other is the most common shorthand of all for margins and paddings:</p>
<pre><code>
margin: 20px 10px 15px 5px;
</code></pre>
<p>This style rule applied a top-margin of 20 pixels, a right margin of 10 pixels, a bottom margin of 15 pixels and a left margin of 5 pixels. The rule that each side is defined in a clock-wise order starting from the top; hence the sequence of: top, right, bottom, left</p>
<p>This is the worst case scenario. It suggest there is neither horizontal consistency nor vertical consistency of margins. Designs done without horizontal consistency are harder to visualise and debug than those that are consistent. Specifying all four values doesn&#8217;t imply any consistency.</p>
<h3>Specifying just one value</h3>
<p>Specifying one value is the second most common approach to setting margins and paddings:</p>
<pre><code>
margin: 5px;
</code></pre>
<p>This style rule sets all four sides (top, right, bottom, left) to 5 pixels. This leads us to our first implicit rule of shorthand usage: even when a particular side is not explicitly specified, it is always set to something in a short hand rule.</p>
<p>This shorthand is equivalent to <code>margin: 5px 5px 5px 5px</code>. This is the height of consistency; all margins are the same. The cognitive load for a site maintainer is the lowest when one value is specified. There is the implication that the design is both horizontally and vertically consistent; everything is perfectly balanced, and nothing is asymmetric.</p>
<p>Specifying one value makes it abundantly clear that all four values are being set, and the resulting code is less cluttered than specifying all four values. So this one is a maintenance benefit.</p>
<h3>Specifying two values</h3>
<p>The next common margin and padding shorthand is specifying two values:</p>
<pre><code>
margin: 10px 5px;
</code></pre>
<p>What this particular rule does is to set both the top and bottom margins to 10 pixels, and both the right and left margins to 5 pixels. Essentially, this is the same as <code>margin: 10px 5px 10px 5px;</code>. The values in the property are just duplicated, in the same order.</p>
<p>The two shorthand values implicitly suggest both vertical consistency and horizontal consistency.  The resulting code is clear and less cluttered than specifying all four values, so again this is a maintenance plus. Flagging horizontal consistency and vertical consistency by using two values is another maintenance benefit &#8211; saying more with less code.</p>
<h3>Specifying three values</h3>
<p>Specifying three values is the least used of the shorthand methods. But surprisingly appreciated by many world-class web developers.</p>
<pre><code>
margin: 10px 5px 15px;
</code></pre>
<p>This style sets the top to 10 pixels, the right to 5 pixels, and the bottom to 15 pixels. That&#8217;s the easy part. What&#8217;s the left margin after this style rule?</p>
<p>The answer is that the left value takes on the same value as the right. So the equivalent 4 value rule is: <code>margin: 10px 5px 15px 5px;</code>. I found this particular shorthand method difficult to remember, because I didn&#8217;t understand the point behind this option. And so I never used it, preferring either to express all four values, or two, or one.</p>
<p>And the point behind three shorthand values is this:</p>
<p>Three value margin shorthands imply that there is horizontal consistency in the design. It means that the developer has coded the design in such a way that the left and right margins of each container are equal. Grid-based layouts are prime examples of where left and right margin consistency is essential, and the three value shorthand is a good indication that this approach has been used.</p>
<p>That&#8217;s the key to remember when you see three values in a margin and padding shorthand: The developer had build the page with consistent right and left margins. If you see four values in this type of layout, that means the consistency has been broken for a reason.</p>
<p>Due to the nature of a web page, horizontal consistency is more feasible and practical than vertical consistency. So the likelihood of left and right margins being equal is greater than the top and bottom margins being equal. It is in these situations where left and right are the same, but top and bottom are not that the three value margin shortcut is used.</p>
<p>It is a matter of taste whether you use the three shorthand values or not. I&#8217;ve tended to stay away from it, but I got caught out when it was used on a page created by someone else. But it&#8217;s worth knowing how it works and what extra meaning it implies.</p>
]]></content:encoded>
			<wfw:commentRss>http://webstandardstips.com/2009/06/14/css-shorthands-for-margin-and-paddings/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>

