<?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>Karmagination &#187; CSS</title>
	<atom:link href="http://www.karmagination.com/blog/tag/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.karmagination.com/blog</link>
	<description>Creation begins with imagination</description>
	<lastBuildDate>Fri, 25 Sep 2009 03:44:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>The Elegant Solution to Centered CSS Floats</title>
		<link>http://www.karmagination.com/blog/2009/02/23/the-elegant-solution-to-centered-css-floats/</link>
		<comments>http://www.karmagination.com/blog/2009/02/23/the-elegant-solution-to-centered-css-floats/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 01:00:29 +0000</pubDate>
		<dc:creator>Kean</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[cross-browser]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[hack]]></category>

		<guid isPermaLink="false">http://www.karmagination.com/?p=29</guid>
		<description><![CDATA[While the title mentions centered floats, the solution that I am about to present does not use floats at all but the end result is the same. Floats are notorious for their browser compatibilities and quirkiness. Through the years, there are only a few elegant solutions on float related problems. One notable solution is the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F02%2F23%2Fthe-elegant-solution-to-centered-css-floats%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F02%2F23%2Fthe-elegant-solution-to-centered-css-floats%2F&amp;source=karmagination&amp;style=normal&amp;service=bit.ly&amp;service_api=R_5ed1fb4fa93b5df6c239dff2a25003de&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>While the title mentions centered floats, the solution that I am about to present does not use floats at all but the end result is the same. Floats are notorious for their browser compatibilities and quirkiness. Through the years, there are only a few elegant solutions on float related problems. One notable solution is the use of hasLayout and the overflow property to clear floats which worked for all major browsers.</p>
<p>I have tried to tackle the centered floats problem a few times without much success, usually giving up and relying on <a href="http://www.cssplay.co.uk/menus/centered.html">methods</a> that <a href="http://pmob.co.uk/pob/centred-float.htm">counter-position</a> the container from it&#8217;s parent. It is not until I came across the <a href="http://blog.mozilla.com/webdev/2009/02/20/cross-browser-inline-block/">Mozilla Webdev post</a> on how to create a cross-browser inline block which reminded me of the reason why it&#8217;s so hard to create centered float is because we lacked the ability to define a cross-browser inline block. With that ability, we can just use text-align: center to do the job as what it would have done to images.</p>
<p>Now, let us jump directly into the CSS.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">li <span style="color: #009900;">&#123;</span>
	<span style="color: #009966; font-style: italic;">/* you can set a height or width if you need to */</span>
&nbsp;
	display<span style="color: #339933;">:</span> inline<span style="color: #339933;">-</span>block<span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/* for modern browsers */</span>	
	display<span style="color: #339933;">:</span> <span style="color: #339933;">-</span>moz<span style="color: #339933;">-</span>inline<span style="color: #339933;">-</span>stack<span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/* for FF 2 */</span>
	zoom<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/* for IE, triggers hasLayout */</span>
	<span style="color: #339933;">*</span>display<span style="color: #339933;">:</span> inline<span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/* IE 7 and below */</span>
&nbsp;
	background<span style="color: #339933;">:</span>#CCC<span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/* not needed */</span>
	margin<span style="color: #339933;">:</span> 5px<span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/* not needed */</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
ul <span style="color: #009900;">&#123;</span>
	text<span style="color: #339933;">-</span>align<span style="color: #339933;">:</span>center<span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/* aligns all the children to center */</span>
&nbsp;
	background<span style="color: #339933;">:</span>#<span style="color: #CC0000;">999</span><span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/* not needed */</span>
	margin<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/* not needed */</span>
	padding<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/* not needed */</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is the html.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;ul&gt;
    &lt;li&gt;aeroplane&lt;/li&gt;
    &lt;li&gt;bird&lt;/li&gt;
    &lt;li&gt;cat&lt;/li&gt;
    &lt;li&gt;dog&lt;/li&gt;
    &lt;li&gt;elephant&lt;/li&gt;
    &lt;li&gt;fish&lt;/li&gt;
&lt;/ul&gt;</pre></div></div>

<p>And the code in action.</p>
<div id="test_center"><span>aeroplane</span><span>bird</span><span>cat</span><span>dog</span><span>elephant</span><span>fish</span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.karmagination.com/blog/2009/02/23/the-elegant-solution-to-centered-css-floats/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

