<?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>CSS Tutorial &#187; fixed width</title>
	<atom:link href="http://www.csstutorial.net/tag/fixed-width/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.csstutorial.net</link>
	<description>CSS Tutorial</description>
	<lastBuildDate>Mon, 30 Jan 2012 06:00:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>2-Column, Center-aligned Fixed Width Layout with CSS</title>
		<link>http://www.csstutorial.net/2010/02/creating-a-basic-2-column-center-aligned-fixed-width-layout-with-css/</link>
		<comments>http://www.csstutorial.net/2010/02/creating-a-basic-2-column-center-aligned-fixed-width-layout-with-css/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 16:46:19 +0000</pubDate>
		<dc:creator>Andrea Barnett</dc:creator>
				<category><![CDATA[CSS Basics]]></category>
		<category><![CDATA[CSS Page Layouts]]></category>
		<category><![CDATA[Killer CSS Tips and Tricks]]></category>
		<category><![CDATA[center-aligned layout]]></category>
		<category><![CDATA[fixed width]]></category>
		<category><![CDATA[two-column layout]]></category>

		<guid isPermaLink="false">http://www.csstutorial.net/?p=463</guid>
		<description><![CDATA[How to build a simple center-aligned fixed width two-column layout.]]></description>
			<content:encoded><![CDATA[<p>This type of layout is used a lot, and for good reason.  It works. </p>
<p>So let&#8217;s start off by looking at the HTML:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot;</span>
<span style="color: #00bbdd;">&quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=iso-8859-1&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;author&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Andrea Barnett for Killersites&quot;</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>2-Column, Center-aligned Fixed Width Layout with CSS<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;header&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>This is the Page Header<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;left-column&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>The Navigation<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;right-column&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>The Main Content<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;footer&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>The Page Footer Goes Here<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p><a href='http://www.csstutorial.net/wp-content/uploads/2010/02/layout011.html'>Basic Layout Components</a></p>
<p>I am going to apply a different background color to each division just to make it more obvious where exactly each division is.  Colors should be indicated by hex codes &#8211; for example #FFFFFF for white or #000000 for black.  But for the purpose of this demonstration, I&#8217;ll use color names, as it makes things more obvious.  So I&#8217;m adding the following CSS code to the header:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">&lt;style type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span><span style="color: #00AA00;">&gt;</span>
&nbsp;
<span style="color: #cc00cc;">#header</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#left-column</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">yellow</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#right-column</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">teal</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#footer</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> pink<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
&lt;/style<span style="color: #00AA00;">&gt;</span></pre></div></div>

<p>To achieve the fixed width portion of the layout, I&#8217;m adding a wrapper division around the entire body html we have so far, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">&lt;body<span style="color: #00AA00;">&gt;</span>
&nbsp;
&lt;div id<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;wrapper&quot;</span><span style="color: #00AA00;">&gt;</span>
&nbsp;
&lt;div id<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;header&quot;</span><span style="color: #00AA00;">&gt;</span>
...
&nbsp;
&lt;/div<span style="color: #00AA00;">&gt;</span> &lt;!-- This closes the wrapper division --<span style="color: #00AA00;">&gt;</span>
&nbsp;
&lt;/body<span style="color: #00AA00;">&gt;</span></pre></div></div>

<p>and in the CSS, I&#8217;m giving this wrapper division a background color just for demonstration purposes &#8211; and a width.  The width is based on current trends per <a href="http://www.w3schools.com/browsers/browsers_display.asp">W3Schools Browser Trends</a>.  According to this information, the vast majority of all surfers views at resolutions of 1280 or higher, so I&#8217;ll shoot for 1280px.  To allow room for the scroll bar and a bit of the (white) body background to show, I&#8217;ll set the width of my wrapper at 1200px.  The CSS that actually centers everything is a right and left margin of &#8216;auto&#8217; to the wrapper:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#wrapper</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">silver</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1200px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>And now we see <a href='http://www.csstutorial.net/wp-content/uploads/2010/02/layout02.html'>THIS</a>. Isn&#8217;t it just beautiful?  The division is centered by this line</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span></pre></div></div>

<p>The &#8217;0&#8242; tells the browser to display a margin of zero for the top and bottom of the wrapper division (that can be changed if you want a top and/or bottom margin) and to automatically calculate the right and left margin so the division will center.  But what about the 2-column thing? I&#8217;m getting to that!</p>
<p>So we&#8217;ve set our wrapper width at 1200px.  That means whatever fits inside, cannot be any larger that those 1200px.  So I&#8217;ll set my left-column division to 300px and the right-column division to 900px.  But that does not yet create the 2-column layout we&#8217;re working on.  In order to achieve that, I have to get the divisions to line up next to each other, and there are several ways to achieve that. For this example, I&#8217;ll float the left-column by adding this CSS</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#left-column</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">yellow</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">300px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#right-column</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">teal</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">900px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">300px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>and just to make everything more realistic, I&#8217;ll add a bit more content to my html, and now we see <a href='http://www.csstutorial.net/wp-content/uploads/2010/02/layout03.html'>THIS</a> &#8211; this does NOT look quite right.  So what&#8217;s happening?  That&#8217;s a topic for a whole other post, but in a nutshell:  A floated division is removed from the actual flow of things &#8211; kind of hovering over the floor like a balloon, so not affecting the actual floor space.  The non-floated divisions, however, do take up &#8216;floor space&#8217; and fall in place in their natural order.  So the footer division follows the content division, ignoring our entire &#8216;balloon&#8217;.  There&#8217;s an easy fix, so:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#footer</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> pink<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">both</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p> and <a href='http://www.csstutorial.net/wp-content/uploads/2010/02/layout04.html'>voil&aacute;</a> &#8211; &#8216;clear: both;&#8217; told our footer division to do just that &#8211; clear any floated or non-floated divisions and get in line where it belongs.</p>
<p>And here you go &#8211; a very basic 2-column, center-aligned fixed width layout with CSS.</p>
<p> As you get rid of the ridiculous background colors and add your own styling &#8211; mainly margins and padding to the right and left column &#8211; you will probably find that your lay-out breaks. That is because any right and/or left margin and/or padding adds to the total width of your division.  Right now, the wrapper is 1200px wide, same as the total of the right (900px) and left (300px) column.  But if you add a 10px right/left padding to the right-column, it&#8217;s suddenly 920px wide and no longer fits next to the left column.  So you have to adjust the total width of right-column to 880px.  Because 10px left padding + 10px right padding plus 880px = 900px.  Simple math.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csstutorial.net/2010/02/creating-a-basic-2-column-center-aligned-fixed-width-layout-with-css/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

