
<?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>Vjeux &#187; CSS</title>
	<atom:link href="http://blog.vjeux.com/category/css/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.vjeux.com</link>
	<description>French Web Developer</description>
	<lastBuildDate>Wed, 21 Jul 2010 01:30:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CSS &#8211; Float Techniques</title>
		<link>http://blog.vjeux.com/2010/css/float-techniques.html</link>
		<comments>http://blog.vjeux.com/2010/css/float-techniques.html#comments</comments>
		<pubDate>Sat, 09 Jan 2010 18:14:29 +0000</pubDate>
		<dc:creator>vjeux</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://blog.vjeux.com/?p=792</guid>
		<description><![CDATA[CSS development is a hard land where you have to struggle with many browser incompatibilities and not so easy to use structures. Here are two extremely useful techniques that allow you to get around common float problems.
List of items without floats
Problem
It is common to display a list of items that flow horizontally. Common examples are [...]]]></description>
			<content:encoded><![CDATA[<p>CSS development is a hard land where you have to struggle with many browser incompatibilities and not so easy to use structures. Here are two extremely useful techniques that allow you to get around common float problems.</p>
<h3>List of items without floats</h3>
<h4>Problem</h4>
<p>It is common to display a list of items that flow horizontally. Common examples are tabs or cloud tags. A common way to write it is to use the float property.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  li { float: left; }
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>One<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Two<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Three<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>However, you are going to struggle with the way floating elements flow in the document. You have to clear them, you will have strange behaviors with multiple floats ...</p>
<h4>Technique</h4>
<p>There is a better way to do it: <code>display: inline-block;</code>. The element will behave inline (as a span) when being positioned in the flow and as a block when styling it. Exactly what you want.</p>
<p>The code is nearly the same, but the float is now gone!</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  li { display: inline-block; }
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>One<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Two<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Three<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>However, this code does not work in ie7 (and of course ie6). But ... There is a simple trick to make it work! Every time you do a <code>display: inline-block</code>, just add the two associated lines. The magic is explained by <a href="http://blog.mozilla.com/webdev/2009/02/20/cross-browser-inline-block/">Ryan Doherty</a> from Mozilla.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">li <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> inline-<span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
<span style="color: #808080; font-style: italic;">/* The following 2 lines do the magic */</span>
  zoom<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">*</span><span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>You can read more about the zoom property and its strangeness on the <a href="http://www.satzansatz.de/cssd/onhavinglayout.html">On Having Layout</a> article.</p>
<h4>Benefits</h4>
<ul>
<li>No more floats and all the problems involved</li>
<li>Cross browser (even ie6)</li>
<li>You can now center elements using <code>text-align</code></li>
</ul>
<h4>Demo</h4>
<style>
.demo-inline {
  text-align: center;
  background-color: #ccc;
  margin: 0 !important;
  padding: 0 !important;
}
.demo-inline li {
  display: inline-block;
  zoom: 1;
  *display: inline;
  border: 1px solid red;
  margin: 5px;
  padding: 5px;
  width: 50px;
  height: 50px;
}
</style>
<div style="width: 300px; margin-top: 10px;">
<ul class="demo-inline">
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</div>
<h3>No more <code>clear</code> to get around floating elements</h3>
<h4>Problem</h4>
<p>When working with floating elements, you are probably going to do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;float: left;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Left Menu<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;float: left;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Another Menu<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;float: right;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Content<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;clear: both;&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The problem is you have to use a <code>&lt;br /&gt;</code> which takes vertical space, completely destroy your padding/margin organization ...</p>
<h4>Technique</h4>
<p>There is one <a href="http://www.quirksmode.org/css/clearing.html">easy technique</a> described by PPK that is solving all the problems and has no side effect!</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;width: 100%; overflow: auto;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;float: left;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Left Menu<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;float: left;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Another Menu<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;float: right;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Content<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The trick is to add <code>width: 100%; overflow: auto;</code> to the parent box and it just works! The parent box now wraps around all the floating elements and you can properly style it. It works will all browsers, even with ie6.</p>
<h4>Demo</h4>
<div style="width: 300px; margin-top: 10px; margin-bottom: 20px;">
<div style="width: 100%; overflow: auto; border: 1px solid red; padding: 0; margin: 0;">
<div style="float: right; background-color: orange; height: 160px; width: 80px;"></div>
<div style="float: left; background-color: green; height: 80px; width: 200px;"></div>
<div style="float: left; background-color: blue; height: 40px; width: 150px;"></div>
</p></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.vjeux.com/2010/css/float-techniques.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
