<?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>Nettuts+ &#187; HTML &amp; CSS</title>
	<atom:link href="http://net.tutsplus.com/category/tutorials/html-css-techniques/feed/" rel="self" type="application/rss+xml" />
	<link>http://net.tutsplus.com</link>
	<description>Web Development &#38; Design Tutorials</description>
	<lastBuildDate>Fri, 20 Nov 2009 19:56:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>20 Email Design Best Practices and Resources for Beginners</title>
		<link>http://net.tutsplus.com/tutorials/html-css-techniques/20-email-design-best-practices-and-resources-for-beginners/</link>
		<comments>http://net.tutsplus.com/tutorials/html-css-techniques/20-email-design-best-practices-and-resources-for-beginners/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 19:39:24 +0000</pubDate>
		<dc:creator>Matthew Kirk</dc:creator>
				<category><![CDATA[HTML & CSS]]></category>

		<guid isPermaLink="false">http://net.tutsplus.com/?p=7309</guid>
		<description><![CDATA[<img src="http://nettuts.s3.amazonaws.com/469_email/200x200.jpg" alt="20 Email Design Best Practices for Beginners" />]]></description>
			<content:encoded><![CDATA[<p>Even for experience designers, building email newsletters isn&#8217;t easy. You receive a lovely looking design, and you crack on with the development. Unfortunately, it just doesn&#8217;t work as it should in every email clients. Styles don&#8217;t display, images aren&#8217;t visible, etc.</p>
<p>This is where these twenty best practices come in handy.</p>
<p><span id="more-7309"></span></p>
<h3>1: Keep the Design Simple</h3>
<p>Emails are not like complex website designs; they should be nicely designed, but somewhat basic. Try basing your designs on a main header image followed by the main content.</p>
<p>The cleaner the design, the easier it will be to code, and the less chance of any abnormalities happening between various browsers and email clients.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/469_email/images/1.jpg" border="0" /></div>
<h3>2: Use Tables</h3>
<p>Email clients live in the past, so all emails must be built using tables for layout. Some CSS styling can be used, but we will discuss this later.</p>
<h4>Not Accepted</h4>
<pre name="code" class="html">
	&lt;div id="header"&gt;
		&lt;h4&gt;Header 1&lt;/h4&gt;
	&lt;/div&gt;
	&lt;div id="content"&gt;
		Lorem ipsum dolor sit amet.
	&lt;/div&gt;f
	&lt;div id="footer"&gt;
		Sign off and footer
	&lt;/div&gt;
</pre>
<h4>Accepted</h4>
<pre name="code" class="html">
	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;Header 1&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Lorem ipsum dolor sit amet.&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Sign off and footer&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
</pre>
<h3>3: Have Web Browsers at the Ready</h3>
<p>Make sure you have as many web browsers as possible available to you. Who knows who will view your email, and what he or she will be using to view it! </p>
<p>At the very least, use these:</p>
<ul>
<li>Internet Explorer 6</li>
<li>Internet Explorer 7</li>
<li>Internet Explorer 8</li>
<li>Mozilla Firefox 3</li>
<li>Apple Safari 3</li>
<li>Google Chrome</li>
</ul>
<h3>4: Sign Up for all the Major Email Clients</h3>
<p>Sign up for as many email accounts as you can think of. Below is a list of email clients to get you started:</p>
<ul>
<li>Google Mail (<a href="http://mail.google.com" title="http://mail.google.com" target="_blank">http://mail.google.com)</a></li>
<li>Hotmail/Live Mail (<a href="http://www.hotmail.com" title="http://www.hotmail.com" target="_blank">http://www.hotmail.com)</a></li>
<li>Yahoo Mail (<a href="http://mail.yahoo.com" title="http://mail.yahoo.com" target="_blank">http://mail.yahoo.com)</a></li>
<li>AOL Mail (<a href="http://webmail.aol.com" title="http://webmail.aol.com" target="_blank">http://webmail.aol.com)</a></li>
</ul>
<p><strong>Please note that they&#8217;re are other, more convenient services that can be used instead; however, many of these charge monthly fees. For more information, review <a href="http://www.litmusapp.com">Litmusapp</a>.<br />
</strong></p>
<h3>5: Use Inline Styles</h3>
<p>If this were the website world, every developer on the planet would say, &quot;do not use inline styles, create a class for it&quot;. Unfortunately, in an email, this is not possible, as the email clients will strip them out, and we don&#39;t want that. So if anything needs to be styled, use inline styles.</p>
<p>Elements like font type and size can be used within the &lt;table> tag, but individual styles should be placed on &lt;td>&#39;s.</p>
<h3>6: Give all Images Alt Tags</h3>
<p>This is a very important step to take, but is often forgotten by many. Styling the &lt;td> for which images are in, with font types, size and color, will allow for your email to degrade gracefully when images are off by default.</p>
<h4>No Alt Tags</h4>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/469_email/images/6_unstyled.jpg" border="0" /></div>
<h4>Alt Tags</h4>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/469_email/images/6_styled.jpg" border="0" /></div>
<h3>7: Do not Set Widths or Heights to Images</h3>
<p>Again, this is a further step to take in order for a lovely gracefully degraded email. If images are off by default, there dimensions will be present, leaving a lot of unnecessary white space throughout.</p>
<h3>8: Wrap the Email in a 100% Width Table</h3>
<p>Email clients only take the code within the body tags, not the body tags themselves. In order to use a background color, you must create a 100% width table to &quot;fake&quot; the background effect.</p>
<pre name="code" class="html">
	&lt;table width="100%"&gt;
		&lt;tr&gt;
			&lt;td&gt;
				&lt;table width="600" align="center"&gt;
					&lt;tr&gt;
						&lt;td&gt;Lorem ipsum dolor sit amet.&lt;/td&gt;
					&lt;/tr&gt;
				&lt;/table&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
</pre>
<h3>9: No Wider than 600px</h3>
<p>Many people don&#39;t actually open their email; they instead view them in the preview panel. On average the smallest preview panel is around 600px, so always design your emails accordingly, unless you don&#39;t want your full email viewable in the preview panel, of course.</p>
<h3>10: Link Styling</h3>
<p>Don&#8217;t forget to style the &lt;a> tag. This will overwrite the email client&#39;s standard link tags.</p>
<pre name="code" class="html">
	&lt;a href="#" style="color:#000000; text-decoration:none;"&gt;Link&lt;/a&gt;
</pre>
<h3>11: Try not to Nest Tables</h3>
<p>Apart from the 100% width wrap table, you should try your best not to nest additional tables. This is easily avoidable; use the stacking system instead.</p>
<p>This allows for a much easier, controllable email.</p>
<h4>Avoid</h4>
<pre name="code" class="html">
	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;
				&lt;table&gt;
					&lt;tr&gt;
						&lt;td&gt;Lorem ipsum dolor sit amet.&lt;/td&gt;
					&lt;/tr&gt;
				&lt;/table&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;
				&lt;table&gt;
					&lt;tr&gt;
						&lt;td&gt;Lorem ipsum dolor sit amet.&lt;/td&gt;
					&lt;/tr&gt;
				&lt;/table&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
</pre>
<h4>Use</h4>
<pre name="code" class="html">
	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;Lorem ipsum dolor sit amet.&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
	&lt;table&gt;
		&lt;tr&gt;
			&lt;td>Lorem ipsum dolor sit amet.&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
</pre>
<h3>12: Avoid Background Images</h3>
<p>Stick to block colors rather than images for the backgrounds for your text; only use funky gradients, images, etc. when no text is involved.</p>
<h3>13: Borders don&#39;t Work</h3>
<p>Within emails, we don&#39;t have much room for browser or email clients specific fixes, so when we have borders that can either sit outside or inside the &lt;td> or be included or excluded from the &lt;td> width, there&#39;s not much we can do. </p>
<p>The fix? Drop two extra &lt;td>&#39;s to either side of the main &lt;td>, and set the background color in each one. This will again &quot;fake&quot; the look of a border and work in all browsers and email clients.</p>
<h4>Won&#8217;t Work</h4>
<pre name="code" class="html">
	&lt;td width="600" style="border-right:1px solid #000000; border-left:1px solid #000000;"&gt;Lorem ipsum&lt;/td&gt;
</pre>
<h4>Will Work</h4>
<pre name="code" class="html">
	&lt;td width="1"&gt;&lt;/td&gt;
	&lt;td width="598"&gt;Lorem ipsum dolor sit amet.&lt;/td&gt;
	&lt;td width="1"&gt;&lt;/td&gt;
</pre>
<h3>14: Hotmail Bug Fixes</h3>
<p>Over the past couple of years, Microsoft has vastly improved the Hotmail/Live service. But&#8230; one huge bug you will come across is the strange padding added to all images. Why do they do this? Who knows? All I know is, there is a wonderfully easy fix.</p>
<pre name="code" class="html">
	&lt;img src="image.jpg" style="display:block;"&gt;
</pre>
<p>On every image tag, simply add display:block, as shown above.</p>
<h3>15: Encode All Characters</h3>
<p>Although we don&#8217;t technically have to encode characters, it&#8217;s best we do.</p>
<p>When viewing emails in various email clients, we cannot guarantee the charset every website is using, so encoding characters allows us to be certain that all characters are being displayed as they should.</p>
<h4>May Work</h4>
<pre name="code" class="html">
	"Some sample code - with special characters"
</pre>
<h4>Will Definitely Work</h4>
<pre name="code" class="html">
	&amp;quot;Some sample code &amp;#45; with special characters&amp;quot;
</pre>
<h3>16: JavaScript = Junk Email</h3>
<p>You cannot, unfortunately, include any type of JavaScript. So no fancy pop-ups or auto-scrolling emails please! If you do decide to include it anyway, your email may be sent to the junk folder. Email clients will see you as a threat. And this is obviously not good. So please stick to plain old HTML.</p>
<h3>17: Give the User a Way Out</h3>
<p>When sending general newsletters to various clients/customers, although you have a lovely designed and developed email, that user may not want your email (hard to take, I know). Always allow them a way out, by adding an unsubscribe link to the bottom of the email, like so:</p>
<blockquote><p>If you would like to unsubscribe from this newsletter, simply click here</p>
</blockquote>
<h3>18: Users Want Options</h3>
<p>Some users may be utilizing a very basic email client &#8211; maybe they&#8217;re checking there webmail at work or on their phones. Images and complex designs may not be best for these types of clients,. Consider, at the top of the email newsletter, having a link which points to the email on a web server somewhere, so the user can view the email in all its glory.</p>
<blockquote><p>Cannot view this email? View it here</p>
</blockquote>
<h3>19: Use a spacer.gif</h3>
<p>Some browsers (Internet Explorer), don&#39;t get on with empty &lt;td>&#39;s. Even if the &lt;td> is set to 10px in width. IE will ignore this and set it to 0.</p>
<p>The fix is to add a transparent GIF, and set this to 10px wide. This then provides you with something to put within the &lt;td>, thus fixing IE&#39;s issues with having empty &lt;td>&#39;s.</p>
<h4>Won&#8217;t Work</h4>
<pre name="code" class="html">
	&lt;td width="300"&gt;Lorem ipsum dolor sit amet&lt;/td&gt;
    &lt;td width="10"&gt;&lt;/td&gt;
    &lt;td width="300"&gt;Lorem ipsum dolor sit amet&lt;/td&gt;
</pre>
<h4>Will Work</h4>
<pre name="code" class="html">
	&lt;td width="300"&gt;Lorem ipsum dolor sit amet&lt;/td&gt;
    &lt;td width="10"&gt;&lt;img src="images/spacer.gif" width="10" height="1" /&gt;&lt;/td&gt;
    &lt;td width="300"&gt;Lorem ipsum dolor sit amet&lt;/td&gt;
</pre>
<h3>20: Send Tests</h3>
<p>This is the most important aspect of email design; sending test emails allows you to view them in all browsers and email clients, looking for any bugs and odd variations.</p>
<p>I have setup a way to allow me to send test, by all means, <a href="http://www.matthewkirk.co.uk/test/email/" title="http://www.matthewkirk.co.uk/test/email/" target="_blank">use mine!</a></p>
<p>
Username: test<br />
Password: nettuts
</p>
<p>
For more comprehensive testing, use a service like <a href="http://campaignmonitor.com">CampaignMonitor</a> or <a href="http://www.litmusapp.com">LitmusApp</a>.
</p>
<h3>Other Helpful Resources</h3>
<h4>General Email Marketing info: </h4>
<ul>
<li><a href="http://www.shayhowe.com/resource/smart-email-marketing/">http://www.shayhowe.com/resource/smart-email-marketing/</a></li>
<li><a href="http://www.webdesignerdepot.com/2009/11/a-guide-to-creating-email-newsletters/">http://www.webdesignerdepot.com/2009/11/a-guide-to-creating-email-newsletters/</a></li>
<li><a href="http://www.smartbiz.com/article/articleview/2073/1/53/">http://www.smartbiz.com/article/articleview/2073/1/53/</a></li>
<li><a href="http://www.smartbiz.com/article/view/2076/1/53">http://www.smartbiz.com/article/view/2076/1/53</a></li>
<li><a href="http://www.kamikazemusic.com/quick-tips/basics-html-email-newsletters/">http://www.kamikazemusic.com/quick-tips/basics-html-email-newsletters/</a></li>
<li><a href="http://www.mailchimp.com/articles/email_marketing_subject_line_comparison/">http://www.mailchimp.com/articles/email_marketing_subject_line_comparison/</a></li>
<li><a href="http://lyrishq.lyris.com/index.php/Email-Marketing/Email-Subject-Lines-15-Rules-to-Write-Them-Right.html">http://lyrishq.lyris.com/index.php/Email-Marketing/Email-Subject-Lines-15-Rules-to-Write-Them-Right.html</a></li>
<li><a href="http://net.tutsplus.com/misc/6-easy-ways-to-improve-your-html-emails/">http://net.tutsplus.com/misc/6-easy-ways-to-improve-your-html-emails/</a></li>
<li><a href="http://www.copyblogger.com/persuasive-writing/">http://www.copyblogger.com/persuasive-writing/</a> </li>
</ul>
<h4>Email Inspiration: </h4>
<ul>
<li><a href="http://www.campaignmonitor.com/gallery/">Campaign Monitor Gallery</a> </li>
<li><a href="http://www.beautiful-email-newsletters.com/">Beautiful Email Newsletters</a></li>
<li><a href="http://htmlemailgallery.com/">HTML Email Gallery</a></li>
</ul>
<h4>Services </h4>
<ul>
<li><a href="http://mailchimp.com">Mail Chimp</a></li>
<li><a href="http://campaignmonitor.com">Campaign Monitor</a></li>
<li><a href="http://www.myemma.com/">Emma Email Marketing</a></li>
<li><a href="http://aweber.com">AWeber</a></li>
<li><a href="http://www.interspire.com/sendstudio/">Interspace SendStudio</a></li>
<li><a href="http://www.feelbreeze.com/">Feel Breeze</a></li>
</ul>
<h4>Email Design Tools </h4>
<ul>
<li><a href="http://litmusapp.com">LitmusApp</a></li>
<li><a href="http://www.previewmyemail.com">Preview My Email</a></li>
</ul>
<h4>Premium Email Templates </h4>
<ul>
<li><a href="http://themeforest.net/category/email-templates">ThemeForest Email Templates</a></li>
</ul>
<ul class="webroundup">
<li>Follow us on <a href="http://www.twitter.com/nettuts">Twitter</a>, or subscribe to the <a href="http://feeds.feedburner.com/nettuts" title="Nettuts+ RSS Feed">Nettuts+ RSS Feed</a> for the best web development tutorials on the web.</li>
</ul>
<p>
<script type="text/javascript"><!--digg_url = "post permalink (not digg url)"; // -->
</script><br />
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://net.tutsplus.com/tutorials/html-css-techniques/20-email-design-best-practices-and-resources-for-beginners/feed/</wfw:commentRss>
		<slash:comments>81</slash:comments>
		</item>
		<item>
		<title>9 Most Common IE Bugs and How to Fix Them</title>
		<link>http://net.tutsplus.com/tutorials/html-css-techniques/9-most-common-ie-bugs-and-how-to-fix-them/</link>
		<comments>http://net.tutsplus.com/tutorials/html-css-techniques/9-most-common-ie-bugs-and-how-to-fix-them/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 10:30:14 +0000</pubDate>
		<dc:creator>Siddharth</dc:creator>
				<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[ie bugs]]></category>
		<category><![CDATA[ie6]]></category>

		<guid isPermaLink="false">http://net.tutsplus.com/?p=7764</guid>
		<description><![CDATA[<img src="http://nettuts.s3.amazonaws.com/494_ie/images/200x200.jpg" alt="9 Most Common IE Bugs and How to Fix Them" />]]></description>
			<content:encoded><![CDATA[<p>
Internet Explorer &#8211; the bane of most web developers&#8217; existence. Up to 60% of your development can be wasted just trying to squash out IE specific bugs which isn&#8217;t really a productive use of your time. In this tutorial, you are going to learn about the most common IE bugs and rendering disparities and how to easily squash them or deal with them. Interested? Let&#8217;s get started.
</p>
<p><span id="more-7764"></span></p>
<h3>1. Centering a Layout</h3>
<p>Centering an element is probably something every web developer has to do while creating a layout. The easiest and most versatile way to center an element is to just add <em>margin: auto;</em> to the relevant element. The above method will take care of centering the element irrespective of the resolution and/or browser width. IE 6 in quirks mode however decides to handle this in the most unfortunate way possible: by not handling it at all.</p>
<p>Consider the Following Code:</p>
<pre name="code" class="css">
#container{
	border: solid 1px #000;
	background: #777;
	width: 400px;
	height: 160px;
	margin: 30px 0 0 30px;
}

#element{
	background: #95CFEF;
	border: solid 1px #36F;
	width: 300px;
	height: 100px;
	margin: 30px auto;

}
</pre>
<p>The output you&#8217;d expect:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/1-1.png" alt="Tutorial Image" border="0" /></div>
<p>But what IE actually gives you:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/1-2.png" alt="Tutorial Image" border="0" /></div>
<p>This is mainly due to IE6 in quirks mode and below not recognizing the <em>auto</em> value we set to the <em>margin</em> property. Fortunately, this is easily fixed.</p>
<h4>The Fix</h4>
<p>The easiest and most reliable way to center content for IE6 and below is to apply <em>text-align: center</em> to the parent element and then apply <em>text-align: left</em> to the element to be centered to make sure the text within it is aligned properly. </p>
<pre name="code" class="css">
#container{
	border: solid 1px #000;
	background: #777;
	width: 400px;
	height: 160px;
	margin: 30px 0 0 30px;
	text-align: center;
}

#element{
	background: #95CFEF;
	border: solid 1px #36F;
	width: 300px;
	height: 100px;
	margin: 30px 0;
    	text-align: left;

}
</pre>
<h3>2. Staircase Effect</h3>
<p>Almost every web developer uses lists to create his navigation. Usually, you create the container element, create some links inside and then float the anchors in the direction he wants and calls it a day. Usually. IE though decides to make it a lot more complicated. Peruse through the following code:</p>
<pre name="code" class="html">
    &lt;ul&gt;
        &lt;li&gt;&lt;a href="#"&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="#"&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="#"&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
</pre>
<pre name="code" class="css">
ul {
    list-style: none;
}

ul li a {
   	display: block;
   	width: 130px;
	height: 30px;
   	text-align: center;
   	color: #fff;
   	float: left;
	background: #95CFEF;
	border: solid 1px #36F;
	margin: 30px 5px;
}
</pre>
<p>A standard compliant browser renders it like so:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/2-1.png" alt="Tutorial Image" border="0" /></div>
<p>And the IE screen shot:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/2-2.png" alt="Tutorial Image" border="0" /></div>
<p>Not a particularly pleasing navigation if you ask me. Fortunately, there are 2 fixes you can try.</p>
<h4>Fix #1</h4>
<p>The easiest way to combat this is to float the li elements themselves instead of the anchor elements. Just add this and you should be done.</p>
<pre name="code" class="css">
ul li {
	float: left;
}
</pre>
<h4>Fix #2</h4>
<p>The second way is to apply <em>display: inline</em> to the enclosing li element. In addition to fixing this bug, it also fixes the double margin bug mentioned below. Purists may not like placing block elements inside inline elements though.</p>
<pre name="code" class="css">
ul li {
	display: inline
}
</pre>
<h3>3. Double Margin on Floated Elements</h3>
<p>This bug is probably among the first ones a web developer starting out will run into and is specific to Internet Explorer 6 and below. Triggering it is as simple as floating an element and then applying a margin in the direction it has been floated. And voila! The margin will be doubled in the rendered output. Not really something you&#8217;d look forward to while creating a pixel perfect layout. </p>
<p>Consider this code:</p>
<pre name="code" class="css">
#element{
	background: #95CFEF;
	width: 300px;
	height: 100px;
	float: left;
	margin: 30px 0 0 30px;
	border: solid 1px #36F;
}
</pre>
<p>On standards compliant browsers, this is how it looks:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/3-1.png" alt="Tutorial Image" border="0" /></div>
<p>But here is how IE 6 decides to render it:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/3-2.png" alt="Tutorial Image" border="0" /></div>
<h4>The Fix</h4>
<p>The fix for this specific bug is to apply <em>display: inline</em> to the floated element and everything works as it is supposed to. Our previous code now changes to:</p>
<pre name="code" class="css">
#element{
	background: #95CFEF;
	width: 300px;
	height: 100px;
	float: left;
	margin: 30px 0 0 30px;
	border: solid 1px #36F;
   	display: inline;
}
</pre>
<h3>4. Inability to Have Elements with Small Heights</h3>
<p>As part of creating a layout, you may need to create elements with very small heights as custom borders for elements. Usually, you&#8217;ll just have to add <em>height: XXpx</em> to the style&#8217;s declarations and you should be done. Or so you thought. Check the page in IE and you&#8217;ll probably do a double take.</p>
<p>As an example, look at the following code:</p>
<pre name="code" class="html">
#element{
	background: #95CFEF;
	border: solid 1px #36F;
	width: 300px;
	height: 2px;
	margin: 30px 0;
}
</pre>
<p>And the output is just as expected: A 2 px element with a 1 px border.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/4-1.png" alt="Tutorial Image" border="0" /></div>
<p>And in IE:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/4-2.png" alt="Tutorial Image" border="0" /></div>
<h4>Fix #1</h4>
<p>The cause of this bug is pretty simple: IE simply refuses to size an element smaller than the set font size. Simply setting the font size to 0 lets you have an element as small and short as you like.</p>
<pre name="code" class="html">
#element{
	background: #95CFEF;
	border: solid 1px #36F;
	width: 300px;
	height: 2px;
	margin: 30px 0;
    	font-size: 0;
}
</pre>
<h4>Fix #2</h4>
<p>The next best way to deal with this bug is to apply <em>overflow: hidden</em> to the element so it collapses to the intended height.</p>
<pre name="code" class="html">
#element{
	background: #95CFEF;
	border: solid 1px #36F;
	width: 300px;
	height: 2px;
	margin: 30px 0;
    	overflow: hidden
}
</pre>
<h3>5. Auto Overflow and Relatively Positioned Items</h3>
<p>This bug rears its ugly head when in a layout you set the parent container&#8217;s <em>overflow</em> property to <em>auto</em> and place a relatively positioned item inside it. The relatively positioned item violates its parent element&#8217;s boundaries and overflows outside. Let me demonstrate. Look the following code:</p>
<pre name="code" class="html">
&lt;div id="element"&gt;&lt;div id="anotherelement"&gt;&lt;/div&gt;&lt;/div&gt;
</pre>
<pre name="code" class="css">
#element{
	background: #95CFEF;
	border: solid 1px #36F;
	width: 300px;
	height: 150px;
	margin: 30px 0;
	overflow: auto;
}

#anotherelement{
	background: #555;
	width: 150px;
	height: 175px;
	position: relative;
	margin: 30px;
}
</pre>
<p>And the expected output:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/5-1.png" alt="Tutorial Image" border="0" /></div>
<p>And IE&#8217;s output:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/5-2.png" alt="Tutorial Image" border="0" /></div>
<h4>The Fix</h4>
<p>The easiest way to fix this annoying bug is to just position the parent element relatively too. </p>
<pre name="code" class="css">
#element{
	background: #95CFEF;
	border: solid 1px #36F;
	width: 300px;
	height: 150px;
	margin: 30px 0;
	overflow: auto;
    	position: relative;
}
</pre>
<h3>6. Fixing the Broken Box Model</h3>
<p>Internet Explorer&#8217;s misinterpretation of the box model is perhaps its unforgivable mistake. IE 6 in semi-standards compliant mode sidesteps this but this issue can still be triggered by quirks mode.  </p>
<p>Two div elements. One with the fix applied and one without. The difference in the width and height is the sum of the paddings applied on each side.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/6.png" alt="Tutorial Image" border="0" /></div>
<h4>The Fix</h4>
<p>I am sure the quandary with the box model needs neither explanation nor demonstration so I&#8217;ll just give you the fix. </p>
<p>The trick is to set the width normally for all standards compliant browsers, target IE5/6 alone and then feed it the corrected width. This is how you&#8217;d usually go on about:</p>
<pre name="code" class="css">
#element{
	width: 400px;
    	height: 150px;
	padding: 50px;
}
</pre>
<p>This now changes to:</p>
<pre name="code" class="html">
#element {
    width: 400px;
    height: 150px;
   \height: 250px;
   \width: 500px
}
</pre>
<p>Essentially, you add the padding to the original width and feed it to IE 6. The fix targets IE 6 in quirks mode alone so you need not worry about IE 6 in normal mode messing things up.</p>
<h3>7. Setting a Minimum Width and Height</h3>
<p>Setting an minimum height to an element is absolutely imperative when trying to convert a beautiful design into a pixel perfect design. Unfortunately, IE completely ignores the <em>min-height</em> property instead taking the height declared as the minimum height.</p>
<h4>Fix #1</h4>
<p>The fix is a hack created by <a href="http://www.dustindiaz.com/min-height-fast-hack/">Dustin Diaz</a>. It utilizes the <em>!important</em> declaration to make it work. The snippet looks like so:</p>
<pre name="code" class="css">
#element {
  min-height:150px;
  height:auto !important;
  height:150px;
}
</pre>
<h4>Fix #2</h4>
<p>The second way is to take advantage of the fact that IE can&#8217;t parse child selectors.</p>
<pre name="code" class="css">
#element {
    min-height: 150px;
    height: 150px;
}

html>body #element {
	height: auto;
}
</pre>
<h3>8. Floated Layout Misbehaving</h3>
<p>One of the important concepts of building table-less layouts using CSS is floating elements. In most cases, IE6 handles this with aplomb but in certain cases it fumbles. When faced with unbreakable content or elements whose width exceeds its parent&#8217;s width, it causes havoc with the layouts. Let me show you. </p>
<p>Consider this piece of code:</p>
<pre name="code" class="html">
&lt;div id="container"&gt;
	&lt;div id="element"&gt;http://net.tutsplus.com/&lt;/div&gt;
	&lt;div id="anotherelement"&gt;&lt;/div&gt;
&lt;/div&gt;
</pre>
<pre name="code" class="css">
#element, #anotherelement{
	background: #95CFEF;
	border: solid 1px #36F;
	width: 100px;
	height: 150px;
	margin: 30px;
	padding: 10px;
	float: left;
}

#container{
	background: #C2DFEF;
	border: solid 1px #36F;
	width: 365px;
	margin: 30px;
	padding: 5px;
	overflow: auto;
}
</pre>
<p>The expected output as viewed on a standards compliant browser:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/8-1.png" alt="Tutorial Image" border="0" /></div>
<p>In IE:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/8-2.png" alt="Tutorial Image" border="0" /></div>
<p>As you can see, the first div expands itself to fit the content which ultimately breaks the layout. </p>
<h4>The Fix</h4>
<p>There is no elegant fix for this bug. The only way to save the layout is to apply <em>overflow: hidden</em> to the element but at the cost of clipping the unbreakable content. The layout will be saved but the extra content won&#8217;t. </p>
<pre name="code" class="css">
#element{
	background: #C2DFEF;
	border: solid 1px #36F;
	width: 365px;
	margin: 30px;
	padding: 5px;
	overflow: hidden;
}
</pre>
<h3>9. Space Between List Items</h3>
<p>IE 6 adds vertical spacing even none is specified in specific cases. Let&#8217;s look at the code first.</p>
<pre name="code" class="html">
&lt;ul&gt;
 &lt;li&gt;&lt;a href="#"&gt;Link 1&lt;/a&gt;&lt;/li&gt;
 &lt;li&gt;&lt;a href="#"&gt;Link 2&lt;/a&gt;&lt;/li&gt;
 &lt;li&gt;&lt;a href="#"&gt;Link 3&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</pre>
<pre name="code" class="css">
ul {
	margin:0;
	padding:0;
	list-style:none;
}

li a {
	background: #95CFEF;
	display: block;
}
</pre>
<p>What it should look like:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/9-1.png" alt="Tutorial Image" border="0" /></div>
<p>What IE gives us:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/494_ie/images/9-2.png" alt="Tutorial Image" border="0" /></div>
<p>Fortunately, there are a plethora of fixes you could try.</p>
<h4>Fix #1</h4>
<p>The easiest way out is to just define a width for the anchor tags and voila! everything renders as it should. Declaring a width triggers the element&#8217;s IE specific <em>hasLayout</em> property. You could instead define a height if you want to.</p>
<pre name="code" class="css">
li a {
	background: #95CFEF;
	display: block;
    	width: 200px;
}
</pre>
<h4>Fix #2</h4>
<p>The next method is to just float the anchor left and then clearing it.</p>
<pre name="code" class="css">
li a {
	background: #95CFEF;
	float: left;
    	clear: left;
}
</pre>
<h4>Fix #3</h4>
<p>The third method is to add <em>display: inline</em> to the enclosing <em>li</em> element. This also fixes the double margin bug as noted above.</p>
<pre name="code" class="css">
li {
	display: inline;
}
</pre>
<h3>Conclusion</h3>
<p>And there you have it: the nine most common IE rendering bugs, and how to squash them. Hopefully this has saved you some blood, sweat and tears while debugging your next creation. I&#8217;ll be watching the comments section frequently; so chime in here if you are having difficulties implementing the fixes noted here.</p>
<p>Questions? Nice things to say? Criticisms? Hit the comments section and leave me a comment. Happy coding!</p>
<ul class="webroundup">
<li>Follow us on <a href="http://www.twitter.com/nettuts">Twitter</a>, or subscribe to the <a href="http://feeds.feedburner.com/nettuts" title="Nettuts+ RSS Feed">Nettuts+ RSS Feed</a> for the best web development tutorials on the web.</li>
</ul>
<p>
<script type="text/javascript"><!--digg_url = "post permalink (not digg url)"; // -->
</script><br />
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://net.tutsplus.com/tutorials/html-css-techniques/9-most-common-ie-bugs-and-how-to-fix-them/feed/</wfw:commentRss>
		<slash:comments>203</slash:comments>
		</item>
		<item>
		<title>Learn how to Create a Retro Animated Flip-Down Clock</title>
		<link>http://net.tutsplus.com/tutorials/html-css-techniques/learn-how-to-create-a-retro-animated-flip-down-clock/</link>
		<comments>http://net.tutsplus.com/tutorials/html-css-techniques/learn-how-to-create-a-retro-animated-flip-down-clock/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 18:48:38 +0000</pubDate>
		<dc:creator>Alexandru Pitea</dc:creator>
				<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[JavaScript & AJAX]]></category>
		<category><![CDATA[clock]]></category>
		<category><![CDATA[MooTools]]></category>

		<guid isPermaLink="false">http://net.tutsplus.com/?p=7313</guid>
		<description><![CDATA[<img src="http://nettuts.s3.amazonaws.com/470_clock/preview.jpg" alt="Create an Animated Flip Down Clock" width="200" height="200"/>]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, we will create an animated flip down clock inspired by the 70&#8217;s. Using the Mootools framework, I tried to replicate the flip action of the pads and make it as lifelike as possible. With it&#8217;s retro styling, it could be a really neat thing to add to your website, so let&#8217;s get started! </p>
<p><span id="more-7313"></span></p>
<div class="tutorial_image">
<a href="http://nettuts.s3.amazonaws.com/470_clock/Source/Source.zip"><img src="http://nettuts.com/wp-content/themes/nettuts/site_images/button_src_nm.jpg"></a><br />
<a href="http://nettuts.s3.amazonaws.com/470_clock/Source/index.html"><img src="http://nettuts.com/wp-content/themes/nettuts/site_images/button_demo_nm.jpg"></a>
</div>
<h3>Tutorial Details</h3>
<ul>
<li><b>Program</b>: Mootools </li>
<li><b>Difficulty:</b> Easy </li>
<li><b>Estimated Completion Time:</b> ~ 1 hour </li>
</ul>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/470_clock/0.jpg" border="0" /></div>
<h3>Step 1: The Main Concept</h3>
<p>The clock is composed of three groups of images: hours, minutes and seconds, which are split in an upper part and a lower part so we can obtain the &#8220;flip&#8221; effect. The main animation consists of reducing the height of the upper part from 100% to 0%, then increasing the height of the lower part from 0% to 100% for each group in which a digit changes. Here is the basic scheme.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/470_clock/1.jpg" border="0" /></div>
<h3>Step 2: Photoshop</h3>
<p>First, we must create our images. </p>
<p>
Select the &#8220;Rounded Ractangle Tool&#8221; (U), set the radius to 10px and the color to #0a0a0a and create a 126px by 126px ractangle, you can change the dimension according to your needs, just keep them an even number. Resterize the shape by going to Layer > Rasterize > Shape or Right Click > Rasterize Layer. Now we want to create that &quot;gap&quot; between the two parts and make the upper background a little bit lighter. Place a guide line at the horizontal half of our pad, then select the hole pad (Ctrl + Click on the layer icon) and with the Rectangular Marquee Tool (M) select the upper half in intersect mode (hold Shift + Alt). Now we just have to fill the selection with #121212 using the Paint Bucket Tool (G). Next trace a 2px, black line using our guide line as help, on a separate layer.  </p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/470_clock/2.jpg" border="0" /></div>
<p>Now we have to add the digits. Using the Type Tool (T) make a new layer with the digits and place it under the line we&#8217;ve created earlier.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/470_clock/3.jpg" border="0" /></div>
<p>Just add a little overlay on the numbers to make them look a bit more realistic. Create a new layer above the digits layer, select the lower part of the pad and fill with #b8b8b8, then fill the upper part with #d7d7d7. Now set the blending mode to &#8220;Multiply&#8221;.   </p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/470_clock/4.jpg" border="0" /></div>
<p>Ok. Now that we have our completed pad, we have to split it up. The main idea is to split the right digit from the left one, so instead of having 60 images for the minutes and seconds groups, you end up with 20 images that we will use for both groups. So basicly we have to split our pad into 4 images with the same dimensions. I used the Crop Tool (C) for this job.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/470_clock/5.jpg" border="0" /></div>
<p>After you crop the pad, change the digit and save each time as a separate .png so you end up with all the files you need ( numbers from 0 &#8211; 9 ). Repeat this step for all parts of the pad. Note that for the hours pad, you don&#8217;t separate the digits, you have just the upper and lower part. In the end here is our folder structure (&quot;Double&quot; for minutes and seconds, &quot;Single&quot; for hours): </p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/470_clock/6.jpg" border="0" /></div>
<h3>Step 3: HTML Markup</h3>
<p>Now that we have our files ready we can start coding. First of all, we need two containers for our images, one for the &quot;upperPart&quot; of our clock and one for the &quot;lowerPart&quot;.</p>
<pre name="code" class="html">
&lt;div id=&quot;upperPart&quot;&gt;
&lt;/div&gt;

&lt;div id=&quot;lowerPart&quot;&gt;
&lt;/div&gt;
</pre>
<p>Next we add the images. Here is a scheme with the id&#8217;s I&#8217;ve used:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/470_clock/7.jpg" border="0" /></div>
<pre name="code" class="html">
&lt;div id=&quot;upperPart&quot;&gt;
    &lt;img src=&quot;spacer.png&quot; /&gt;&lt;img id=&quot;hoursUp&quot; src=&quot;Single/Up/AM/0.png&quot; /&gt;
    &lt;img id=&quot;minutesUpLeft&quot; src=&quot;Double/Up/Left/0.png&quot; /&gt;&lt;img id=&quot;minutesUpRight&quot; src=&quot;Double/Up/Right/0.png&quot; /&gt;
    &lt;img id=&quot;secondsUpLeft&quot; src=&quot;Double/Up/Left/0.png&quot; /&gt;&lt;img id=&quot;secondsUpRight&quot; src=&quot;Double/Up/Right/0.png&quot; /&gt;
&lt;/div&gt;
&lt;div id=&quot;lowerPart&quot;&gt;
    &lt;img src=&quot;spacer.png&quot; /&gt;&lt;img id=&quot;hoursDown&quot; src=&quot;Single/Down/AM/0.png&quot;/&gt;
    &lt;img id=&quot;minutesDownLeft&quot; src=&quot;Double/Down/Left/0.png&quot; /&gt;&lt;img id=&quot;minutesDownRight&quot; src=&quot;Double/Down/Right/0.png&quot; /&gt;
    &lt;img id=&quot;secondsDownLeft&quot; src=&quot;Double/Down/Left/0.png&quot; /&gt;&lt;img id=&quot;secondsDownRight&quot; src=&quot;Double/Down/Right/0.png&quot; /&gt;
&lt;/div&gt;
</pre>
<p>I had to use a transparent spacer image that is  1px wide and the same hight as the other images in order to keep the containers from shrinking when the pads flip. Also, there must not be any space between the images from the same group (e.g. &quot;minutesUpLeft&quot; and &quot;minutesUpRight&quot;). </p>
<p>Ok. These will be the front pads of our clock that will flip down, now we have to set up the back ones, so when the front pads flip, the new digits can be seen on the them. We will wrap what we&#8217;ve done so far in a div and just duplicate it above itself, adding to each image id the word &quot;Back&quot; and changing to the appropriate source file.</p>
<pre name="code" class="html">
&lt;div id=&quot;back&quot;&gt;
    &lt;div id=&quot;upperPartBack&quot;&gt;
        &lt;img src=&quot;spacer.png&quot; /&gt;&lt;img id=&quot;hoursUpBack&quot; src=&quot;Single/Up/AM/0.png&quot; /&gt;
        &lt;img id=&quot;minutesUpLeftBack&quot; src=&quot;Double/Up/Left/0.png&quot; /&gt;&lt;img id=&quot;minutesUpRightBack&quot; src=&quot;Double/Up/Right/0.png&quot; /&gt;
        &lt;img id=&quot;secondsUpLeftBack&quot; src=&quot;Double/Up/Left/0.png&quot; /&gt;&lt;img id=&quot;secondsUpRightBack&quot; src=&quot;Double/Up/Right/0.png&quot; /&gt;
    &lt;/div&gt;
    &lt;div id=&quot;lowerPartBack&quot;&gt;
        &lt;img src=&quot;spacer.png&quot; /&gt;&lt;img id=&quot;hoursDownBack&quot; src=&quot;Single/Down/AM/0.png&quot; /&gt;
        &lt;img id=&quot;minutesDownLeftBack&quot; src=&quot;Double/Down/Left/0.png&quot; /&gt;&lt;img id=&quot;minutesDownRightBack&quot; src=&quot;Double/Down/Right/0.png&quot; /&gt;
        &lt;img id=&quot;secondsDownLeftBack&quot; src=&quot;Double/Down/Left/0.png&quot; /&gt;&lt;img id=&quot;secondsDownRightBack&quot; src=&quot;Double/Down/Right/0.png&quot; /&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;div id=&quot;front&quot;&gt;
    &lt;div id=&quot;upperPart&quot;&gt;
        &lt;img src=&quot;spacer.png&quot; /&gt;&lt;img id=&quot;hoursUp&quot; src=&quot;Single/Up/AM/0.png&quot; /&gt;
        &lt;img id=&quot;minutesUpLeft&quot; src=&quot;Double/Up/Left/0.png&quot; /&gt;&lt;img id=&quot;minutesUpRight&quot; src=&quot;Double/Up/Right/0.png&quot; /&gt;
        &lt;img id=&quot;secondsUpLeft&quot; src=&quot;Double/Up/Left/0.png&quot; /&gt;&lt;img id=&quot;secondsUpRight&quot; src=&quot;Double/Up/Right/0.png&quot; /&gt;
    &lt;/div&gt;
    &lt;div id=&quot;lowerPart&quot;&gt;
        &lt;img src=&quot;spacer.png&quot; /&gt;&lt;img id=&quot;hoursDown&quot; src=&quot;Single/Down/AM/0.png&quot; /&gt;
        &lt;img id=&quot;minutesDownLeft&quot; src=&quot;Double/Down/Left/0.png&quot; /&gt;&lt;img id=&quot;minutesDownRight&quot; src=&quot;Double/Down/Right/0.png&quot; /&gt;
        &lt;img id=&quot;secondsDownLeft&quot; src=&quot;Double/Down/Left/0.png&quot; /&gt;&lt;img id=&quot;secondsDownRight&quot; src=&quot;Double/Down/Right/0.png&quot; /&gt;
    &lt;/div&gt;
&lt;/div&gt;
</pre>
<p>Here is the complete .html file with reference to the stylesheet and the javascript file &quot;animate.js&quot; in which we will create the animation.</p>
<pre name="code" class="html">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Create an Animated Flip Down Clock&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;style.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;div id=&quot;wrapper&quot;&gt;
	&lt;div id=&quot;back&quot;&gt;
		&lt;div id=&quot;upperPartBack&quot;&gt;
			&lt;img src=&quot;spacer.png&quot; /&gt;&lt;img id=&quot;hoursUpBack&quot; src=&quot;Single/Up/AM/0.png&quot; /&gt;
			&lt;img id=&quot;minutesUpLeftBack&quot; src=&quot;Double/Up/Left/0.png&quot; /&gt;&lt;img id=&quot;minutesUpRightBack&quot; src=&quot;Double/Up/Right/0.png&quot; /&gt;
			&lt;img id=&quot;secondsUpLeftBack&quot; src=&quot;Double/Up/Left/0.png&quot; /&gt;&lt;img id=&quot;secondsUpRightBack&quot; src=&quot;Double/Up/Right/0.png&quot; /&gt;
		&lt;/div&gt;
		&lt;div id=&quot;lowerPartBack&quot;&gt;
			&lt;img src=&quot;spacer.png&quot; /&gt;&lt;img id=&quot;hoursDownBack&quot; src=&quot;Single/Down/AM/0.png&quot; /&gt;
			&lt;img id=&quot;minutesDownLeftBack&quot; src=&quot;Double/Down/Left/0.png&quot; /&gt;&lt;img id=&quot;minutesDownRightBack&quot; src=&quot;Double/Down/Right/0.png&quot; /&gt;
			&lt;img id=&quot;secondsDownLeftBack&quot; src=&quot;Double/Down/Left/0.png&quot; /&gt;&lt;img id=&quot;secondsDownRightBack&quot; src=&quot;Double/Down/Right/0.png&quot; /&gt;
		&lt;/div&gt;
	&lt;/div&gt;

	&lt;div id=&quot;front&quot;&gt;
		&lt;div id=&quot;upperPart&quot;&gt;
			&lt;img src=&quot;spacer.png&quot; /&gt;&lt;img id=&quot;hoursUp&quot; src=&quot;Single/Up/AM/0.png&quot; /&gt;
			&lt;img id=&quot;minutesUpLeft&quot; src=&quot;Double/Up/Left/0.png&quot; /&gt;&lt;img id=&quot;minutesUpRight&quot; src=&quot;Double/Up/Right/0.png&quot;/&gt;
			&lt;img id=&quot;secondsUpLeft&quot; src=&quot;Double/Up/Left/0.png&quot; /&gt;&lt;img id=&quot;secondsUpRight&quot; src=&quot;Double/Up/Right/0.png&quot;/&gt;
		&lt;/div&gt;
		&lt;div id=&quot;lowerPart&quot;&gt;
			&lt;img src=&quot;spacer.png&quot; /&gt;&lt;img id=&quot;hoursDown&quot; src=&quot;Single/Down/AM/0.png&quot;/&gt;
			&lt;img id=&quot;minutesDownLeft&quot; src=&quot;Double/Down/Left/0.png&quot;/&gt;&lt;img id=&quot;minutesDownRight&quot; src=&quot;Double/Down/Right/0.png&quot; /&gt;
			&lt;img id=&quot;secondsDownLeft&quot; src=&quot;Double/Down/Left/0.png&quot;  /&gt;&lt;img id=&quot;secondsDownRight&quot; src=&quot;Double/Down/Right/0.png&quot;  /&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;script src=&quot;mootools.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;animate.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;/html&gt;
</pre>
<h3>Step 4: CSS</h3>
<p>The main thing we have to do now is to overlap the &#8220;front&#8221; and &#8220;back&#8221; divs. First we position the main wrapper where we need it and then give the same absolute position to both containers.</p>
<pre name="code" class="css">
#wrapper{
	position:absolute;
	top: 100px;
	left:400px;
}

#front, #back{
	position:absolute;
	top:0px;
}
</pre>
<p>Now we need to vertically align the upper part to the bottom and the lower part to the top, so that the pads would look like they&#8217;re anchored to the middle of the clock. I added the height and visibility properties for the front parts because we need them for the animation later on.</p>
<pre name="code" class="css">
#upperPart, #upperPartBack{
	vertical-align:bottom;
}

#lowerPart, #lowerPartBack{
	vertical-align:top;
}

#upperPart img{
	position:relative;
	height:63px;
	vertical-align:bottom;
	visibility:visible;
}

#lowerPart img{
	position:relative;
	height:63px;
	vertical-align:top;
	visibility:visible;
}

#lowerPartBack img{
	position:relative;
	vertical-align:top;
}

#upperPartBack img{
	position:relative;
	vertical-align:bottom;
}
</pre>
<p>Finally all we have to do is to constrain the width of the pads because we want to play around only with their height. By default, if you change one of them, the hole image will be scaled.</p>
<pre name="code" class="css">
#hoursUp, #hoursDown, #hoursUpBack, #hoursDownBack{
	width:126px;
}

#minutesUpLeft, #minutesUpRight, #minutesDownLeft, #minutesDownRight,
#minutesUpLeftBack, #minutesUpRightBack, #minutesDownLeftBack, #minutesDownRightBack,
#secondsUpLeft, #secondsUpRight, #secondsDownLeft, #secondsDownRight,
#secondsUpLeftBack, #secondsUpRightBack, #secondsDownLeftBack, #secondsDownRightBack{
	width:63px;
}
</pre>
<p> Here it is all put together: </p>
<pre name="code" class="css">
body{
	background:#000;
}

#wrapper{
	position:absolute;
	top: 100px;
	left:400px;
}

#front, #back{
	position:absolute;
	top:0px;
}

#upperPart, #upperPartBack{
	vertical-align:bottom;
}

#lowerPart, #lowerPartBack{
	vertical-align:top;
}

#upperPart img{
	position:relative;
	height:63px;
	vertical-align:bottom;
	visibility:visible;
}

#lowerPart img{
	position:relative;
	height:63px;
	vertical-align:top;
	visibility:visible;
}

#lowerPartBack img{
	position:relative;
	vertical-align:top;
}

#upperPartBack img{
	position:relative;
	vertical-align:bottom;
}

#hoursUp, #hoursDown, #hoursUpBack, #hoursDownBack{
	width:126px;
}

#minutesUpLeft, #minutesUpRight, #minutesDownLeft, #minutesDownRight,
#minutesUpLeftBack, #minutesUpRightBack, #minutesDownLeftBack, #minutesDownRightBack,
#secondsUpLeft, #secondsUpRight, #secondsDownLeft, #secondsDownRight,
#secondsUpLeftBack, #secondsUpRightBack, #secondsDownLeftBack, #secondsDownRightBack{
	width:63px;
}
</pre>
<h3>Step 5: Creating the Animation</h3>
<p>First of all we need some variables to store the time that is shown on the pads. Note: h = hours, m1 = the left minute digit, m2 = the right minute digit, s1 = the left second digit, s2 = the right second digit.</p>
<pre name="code" class="js">
	var h_current = -1;
	var m1_current = -1;
	var m2_current = -1;
	var s1_current = -1;
	var s2_current= -1;
</pre>
<p>Now we create a function that will run every second and update our clock. First we get the current time and determine the time of day, AM or PM.</p>
<pre name="code" class="js">
function retroClock(){

       now = new Date();
       h = now.getHours();
       m1 = now.getMinutes() / 10;
       m2 = now.getMinutes() % 10;
       s1 = now.getSeconds() / 10;
       s2 = now.getSeconds() % 10;
       if(h &lt; 12)
          ap = &quot;AM&quot;;
       else{
          if( h == 12 )
              ap = &quot;PM&quot;;
          else{
              ap = &quot;PM&quot;;
              h -= 12; }
       }
</pre>
<p>Then we compare it to the time shown on the pads and change which group is different. It uses a function called &#8220;flip&#8221; that I will describe in a minute.</p>
<pre name="code" class="js">
       if( h != h_current){
          flip(&#039;hoursUp&#039;, &#039;hoursDown&#039;, h, &#039;Single/Up/&#039;+ap+&#039;/&#039;, &#039;Single/Down/&#039;+ap+&#039;/&#039;);
          h_current = h;
      }

      if( m2 != m2_current){
          flip(&#039;minutesUpRight&#039;, &#039;minutesDownRight&#039;, m2, &#039;Double/Up/Right/&#039;, &#039;Double/Down/Right/&#039;);
          m2_current = m2;

          flip(&#039;minutesUpLeft&#039;, &#039;minutesDownLeft&#039;, m1, &#039;Double/Up/Left/&#039;, &#039;Double/Down/Left/&#039;);
          m1_current = m1;
      }

       if (s2 != s2_current){
          flip(&#039;secondsUpRight&#039;, &#039;secondsDownRight&#039;, s2, &#039;Double/Up/Right/&#039;, &#039;Double/Down/Right/&#039;);
          s2_current = s2;

          flip(&#039;secondsUpLeft&#039;, &#039;secondsDownLeft&#039;, s1, &#039;Double/Up/Left/&#039;, &#039;Double/Down/Left/&#039;);
          s1_current = s1;
      }
}//end retroClock
</pre>
<p>Now, the flip function. It has a few parameters: upperId, lowerId = the ids of the upper and lower pads that will flip; changeNumber = the new value that will be shown; pathUpper, pathLower = the paths to the source files for the new value. The animation is composed of the following steps: <br />
  The front upper pad takes the value of the back one and it&#8217;s made visible, overlaing it, while the lower one is also made visible but it&#8217;s height is changed to 0px.</p>
<pre name="code" class="js">
function flip (upperId, lowerId, changeNumber, pathUpper, pathLower)
{
    var upperBackId = upperId+&quot;Back&quot;;
    $(upperId).src = $(upperBackId).src;
    $(upperId).setStyle(&quot;height&quot;, &quot;63px&quot;);
    $(upperId).setStyle(&quot;visibility&quot;, &quot;visible&quot;);

    $(lowerId).setStyle(&quot;height&quot;, &quot;0px&quot;);
    $(lowerId).setStyle(&quot;visibility&quot;, &quot;visible&quot;);
</pre>
<p>Now we change the back upper and front lower pad to the new value.</p>
<pre name="code" class="js">
	$(upperBackId).src = pathUpper+parseInt(changeNumber)+&quot;.png&quot;;
   $(lowerId).src = pathLower+parseInt(changeNumber)+&quot;.png&quot;;
</pre>
<p>Having this setup we can start the actual animation. As I mentioned earlier it consists of reducing the height of the front upper part to 0%, 0px, and increasing the height of the front lower part to 100%, 63px in this case. After the animation is complete, the back lower pad takes the new value and the front pads are make hidden.</p>
<pre name="code" class="js">
    var flipUpper = new Fx.Tween(upperId, {duration: 200, transition: Fx.Transitions.Sine.easeInOut});
    flipUpper.addEvents({
        &#039;complete&#039;: function(){
            var flipLower = new Fx.Tween(lowerId, {duration: 200, transition: Fx.Transitions.Sine.easeInOut});
                flipLower.addEvents({
                    &#039;complete&#039;: function(){
                        lowerBackId = lowerId+&quot;Back&quot;;
                        $(lowerBackId).src = $(lowerId).src;
                        $(lowerId).setStyle(&quot;visibility&quot;, &quot;hidden&quot;);
                        $(upperId).setStyle(&quot;visibility&quot;, &quot;hidden&quot;);
                    }				});
                flipLower.start(&#039;height&#039;, 64);

        }
                        });
    flipUpper.start(&#039;height&#039;, 0);
}//end flip
</pre>
<p>The final thing to do is to make our main function run every second. </p>
<pre name="code" class="js">
	setInterval(&#039;retroClock()&#039;, 1000);;
</pre>
<p>Here it is all put together.</p>
<pre name="code" class="js">
var h_current = -1;
var m1_current = -1;
var m2_current = -1;
var s1_current = -1;
var s2_current= -1;

function flip (upperId, lowerId, changeNumber, pathUpper, pathLower)
{
    var upperBackId = upperId+&quot;Back&quot;;
    $(upperId).src = $(upperBackId).src;
    $(upperId).setStyle(&quot;height&quot;, &quot;63px&quot;);
    $(upperId).setStyle(&quot;visibility&quot;, &quot;visible&quot;);

    $(lowerId).setStyle(&quot;height&quot;, &quot;0px&quot;);
    $(lowerId).setStyle(&quot;visibility&quot;, &quot;visible&quot;);

	$(upperBackId).src = pathUpper+parseInt(changeNumber)+&quot;.png&quot;;
    $(lowerId).src = pathLower+parseInt(changeNumber)+&quot;.png&quot;;

    var flipUpper = new Fx.Tween(upperId, {duration: 200, transition: Fx.Transitions.Sine.easeInOut});
    flipUpper.addEvents({
        &#039;complete&#039;: function(){
            var flipLower = new Fx.Tween(lowerId, {duration: 200, transition: Fx.Transitions.Sine.easeInOut});
                flipLower.addEvents({
                    &#039;complete&#039;: function(){
                        lowerBackId = lowerId+&quot;Back&quot;;
                        $(lowerBackId).src = $(lowerId).src;
                        $(lowerId).setStyle(&quot;visibility&quot;, &quot;hidden&quot;);
                        $(upperId).setStyle(&quot;visibility&quot;, &quot;hidden&quot;);
                    }				});
                flipLower.start(&#039;height&#039;, 64);

        }
                        });
    flipUpper.start(&#039;height&#039;, 0);
}//end flip

function retroClock(){

     now = new Date();
     h = now.getHours();
     m1 = now.getMinutes() / 10;
     m2 = now.getMinutes() % 10;
     s1 = now.getSeconds() / 10;
     s2 = now.getSeconds() % 10;
     if(h &lt; 12)
        ap = &quot;AM&quot;;
     else{
        if( h == 12 )
            ap = &quot;PM&quot;;
        else{
            ap = &quot;PM&quot;;
            h -= 12; }
     }

     if( h != h_current){
        flip(&#039;hoursUp&#039;, &#039;hoursDown&#039;, h, &#039;Single/Up/&#039;+ap+&#039;/&#039;, &#039;Single/Down/&#039;+ap+&#039;/&#039;);
        h_current = h;
    }

    if( m2 != m2_current){
        flip(&#039;minutesUpRight&#039;, &#039;minutesDownRight&#039;, m2, &#039;Double/Up/Right/&#039;, &#039;Double/Down/Right/&#039;);
        m2_current = m2;

        flip(&#039;minutesUpLeft&#039;, &#039;minutesDownLeft&#039;, m1, &#039;Double/Up/Left/&#039;, &#039;Double/Down/Left/&#039;);
        m1_current = m1;
    }

     if (s2 != s2_current){
        flip(&#039;secondsUpRight&#039;, &#039;secondsDownRight&#039;, s2, &#039;Double/Up/Right/&#039;, &#039;Double/Down/Right/&#039;);
        s2_current = s2;

        flip(&#039;secondsUpLeft&#039;, &#039;secondsDownLeft&#039;, s1, &#039;Double/Up/Left/&#039;, &#039;Double/Down/Left/&#039;);
        s1_current = s1;
    }
}//end retroClock

setInterval(&#039;retroClock()&#039;, 1000);
</pre>
<h3>Finished</h3>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/470_clock/0.jpg" border="0" /></div>
<p>We are finished! Hope you&#8217;ve enjoyed working on this little project, it has a somewhat complex concept, but in the end it is a really neat gadget for your websites. Please do not hesitate to send any suggestions you may have!
</p>
<ul class="webroundup">
<li>Follow us on <a href="http://www.twitter.com/nettuts">Twitter</a>, or subscribe to the <a href="http://feeds.feedburner.com/nettuts" title="Nettuts+ RSS Feed">Nettuts+ RSS Feed</a> for more daily web development tuts and articles.</li>
</ul>
<p>
<script type="text/javascript"><!--digg_url = "post permalink (not digg url)"; // -->
</script><br />
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://net.tutsplus.com/tutorials/html-css-techniques/learn-how-to-create-a-retro-animated-flip-down-clock/feed/</wfw:commentRss>
		<slash:comments>90</slash:comments>
		</item>
		<item>
		<title>CSS: The Very First Steps &#8211; New Plus Tutorial</title>
		<link>http://net.tutsplus.com/tutorials/html-css-techniques/css-the-very-first-steps-new-plus-tutorial/</link>
		<comments>http://net.tutsplus.com/tutorials/html-css-techniques/css-the-very-first-steps-new-plus-tutorial/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 18:47:10 +0000</pubDate>
		<dc:creator>Jeffrey Way</dc:creator>
				<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[plus tutorial]]></category>

		<guid isPermaLink="false">http://net.tutsplus.com/?p=7076</guid>
		<description><![CDATA[<img src="http://nettutsplus.s3.amazonaws.com/34_css_first_steps/200x200.jpg" alt="CSS: The First Steps - New Plus Tutorial" />]]></description>
			<content:encoded><![CDATA[<p>
This one is for those who are hoping to take their first steps into web design. This 70 minute Plus <strong>video tutorial</strong> will assume that you have zero knowledge of CSS. Over the course of the screencast, you&#8217;ll learn about the basic syntax, a plethora of different properties, and how to create the beginnings of your very first website.
</p>
<p>
Don&#8217;t know what CSS stands for? Need help diving into CSS? Never created a website before? If so, this screencast was tailor-made for you! <a href="http://net.tutsplus.com/about/join-plus/">Sign up for a Plus membership!</a>
</p>
<p><span id="more-7076"></span></p>
<h3>Join Tuts Plus</h3>
<div class="tutorial_image"><img src="http://miscfiles.s3.amazonaws.com/banners/nettuts_468x60.jpg" border=0 alt="NETTUTS+ Screencasts and Bonus Tutorials" width=468 height=60></div>
<p>
For those unfamiliar, the family of TUTS sites runs a premium membership service called <a href="http://www.tutsplus.com">&#8220;TUTSPLUS&#8221;</a>. For $9 per month, you gain access to exclusive premium tutorials, screencasts, and freebies at <a href="http://net.tutsplus.com">Nettuts+</a>, <a href="psd.tutsplus.com">Psdtuts+</a>, <a href="http://audio.tutsplus.com">Audiotuts+</a>, and <a href="vector.tutsplus.com">Vectortuts+!</a> For the price of a pizza, you&#8217;ll learn from some of the best minds in the business. <a href="http://net.tutsplus.com/about/join-plus/">Join today!</a> </p>
<ul class="webroundup">
<li>Subscribe to the <a href="http://feeds.feedburner.com/nettuts" title="Nettuts+ RSS Feed">Nettuts+ RSS Feed</a> for more daily web development tuts and articles.</li>
</ul>
<p>
<script type="text/javascript"><!--digg_url = "post permalink (not digg url)"; // -->
</script><br />
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://net.tutsplus.com/tutorials/html-css-techniques/css-the-very-first-steps-new-plus-tutorial/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Object-Oriented CSS: What, How, and Why</title>
		<link>http://net.tutsplus.com/tutorials/html-css-techniques/object-oriented-css-what-how-and-why/</link>
		<comments>http://net.tutsplus.com/tutorials/html-css-techniques/object-oriented-css-what-how-and-why/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 11:15:56 +0000</pubDate>
		<dc:creator>Andrew Burgess</dc:creator>
				<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://net.tutsplus.com/?p=6986</guid>
		<description><![CDATA[<img alt="Object-Oriented CSS: What, How, and Why" src="http://nettuts.s3.amazonaws.com/450_ooCss/200x200.jpg" />]]></description>
			<content:encoded><![CDATA[<p>It sounds like an oxymoron, or at least an impossibility, doesn&#8217;t it? How can a static language that&#8217;s really more like markup than programming be object-oriented? In this tutorial, I&#8217;m going to introduce you to the idea of object-oriented CSS, show you how it works, and try to convince you that you should be using it too!</p>
<p><span id="more-6986"></span></p>
<div class="tutorial_image">
<a href="http://nettuts.s3.amazonaws.com/450_ooCss/demo.zip"><img src="http://nettuts.com/wp-content/themes/nettuts/site_images/button_src_nm.jpg"></a><br />
<a href="http://nettuts.s3.amazonaws.com/450_ooCss/demo/index.htm"><img src="http://nettuts.com/wp-content/themes/nettuts/site_images/button_demo_nm.jpg"></a>
</div>
<h3>What is Object-Oriented CSS?</h3>
<p>Object-oriented CSS, at its core, is simply writing cleaner, <abbr title="Don't Repeat Yourself">DRY</abbr>er CSS. It&#8217;s not a different language: still the same old CSS we all know and love. It&#8217;s just a paradigm shift. Really, object-oriented CSS is a few simple patterns and best practices.</p>
<p>So why call it object-oriented? Well, according to <a title="Wikipedia article on Object Oriented Programming" href="http://en.wikipedia.org/wiki/Object_oriented">Wikipedia</a>, </p>
<blockquote>
<p>Object-oriented programming (OOP) is a programming paradigm that uses &#8220;objects&#8221; &mdash; data structures consisting of datafields and methods &mdash; and their interactions to design applications and computer programs.</p>
</blockquote>
<p>If we were to re-write that definition for object-oriented CSS, it might say something like this:</p>
<blockquote>
<p>Object-oriented CSS is a coding paradigm that styles &#8220;objects&#8221; or &#8220;modules&#8221;&mdash;nestable chunks of HTML that define a section of a webpage&mdash;with robust, reusable styles.</p>
</blockquote>
<p>This basically means that you have a standard &#8220;object&#8221; (an HTML structure). Then, you have CSS classes that you apply to objects, which define the design and flow of the object.</p>
<p>Confused? Let&#8217;s look a bit of theory.</p>
<h3>What is the Theory Behind it?</h3>
<div class="tutorial_image"><img alt="Nicole Sullivan's Presentation" src="http://nettuts.s3.amazonaws.com/450_ooCss/oocss.jpg" /></div>
<blockquote>
<p>There are two main principles [in object-oriented CSS]: the first is to separate the structure from the skin and the second is to separate the container from the content.</p>
</blockquote>
<p>I quote <a href="http://www.stubbornella.org" title="Nicloe Sullivan's website">Nicole Sullivan</a>, whose brainchild object-oriented CSS is. So how do these two principles play themselves out?</p>
<p>Separating the structure from the skin means that your layout styling and your design styling are separate. One  very practical way to do this is to use a grid system; <a href="http://960.gs/">there</a> <a href="http://developer.yahoo.com/yui/grids/">are</a> <a href="http://www.blueprintcss.org/">many</a> <a href="http://www.yaml.de/en/">options</a>, or you can create your own. If you&#8217;re not using a grid system, you&#8217;ll probably just define the structure on the primary object on your page; that&#8217;s what we&#8217;ll be doing today.</p>
<p>Separating the container from the content means that any object (the container) should be able to adapt itself to accept any content; for example, it shouldn&#8217;t <em>need</em> to have an h3 at the top, followed by an unordered list for it to look right. This allows flexibility and reusability, which is paramount. </p>
<h3>Why Should I Code This Way?</h3>
<p>There are a few good reasons why you would want to write your CSS in an object-oriented way. One of the biggest benefits&mdash;and we&#8217;ve already mentioned it&mdash;is that your CSS will be more reusable. But your stylesheets should also become much smaller. Object-oriented CSS should make it easier to change the design of a site. </p>
<p>Writing your styling this way can also give you peace of mind: it will be much easier to change parts of your site without breaking things. Object-oriented CSS also enables you to change your site consistently.</p>
<h3>How do I Practice Object-Oriented CSS?</h3>
<p>Well, if you&#8217;ve come this far, you&#8217;re interested in <em>how</em> exactly you write CSS with an object-oriented mindset. Here we go!</p>
<p>The first step is really prep for the CSS: you have to determine your HTML object. Generally, your object will have a header, a body, and a footer, although the header and footer are optional. Here&#8217;s a very basic object.</p>
<pre class="html" name="code">
&lt;div class="object">
    &lt;div class="head">&lt;/div>
    &lt;div class="body">&lt;/div>
    &lt;div class="foot">&lt;/div>
&lt;/div>
</pre>
<p>Before you scream &#8220;DIVITIS!&#8221; realize that this isn&#8217;t as bad as it looks; consider this:</p>
<pre name="code" class="html">
&lt;article>
    &lt;header>&lt;/header>
    &lt;section>&lt;/section>
    &lt;footer>&lt;/footer>
&lt;/article>
</pre>
<p>Using HTML5, we now have got an object with semantic meaning and no soup-like characteristics. Actually, this is the object we&#8217;ll use today. </p>
<p>If we&#8217;re going to write some CSS, we&#8217;ll need something to style, so let&#8217;s whip up a very basic template: a blog home page, and a single post page. We&#8217;ll be using a few HTML5 elements and some CSS3 styling today!</p>
<h4>index.htm</h4>
<pre class="html" name="code">
&lt;!DOCTYPE html>
&lt;html>
  &lt;head>
    &lt;meta charset='utf-8' />
    &lt;title>Object Oriented CSS&lt;/title>
    &lt;!--[if IE]>&lt;script src="http://nettuts.s3.amazonaws.com/450_ooCss/http://html5shiv.googlecode.com/svn/trunk/html5.js">&lt;/script>&lt;![endif]-->
    &lt;!-- This makes IE recognize and use the HTML5 elements -->
    &lt;link type="text/css" rel="stylesheet" href="css/reset.css" />
    &lt;link type="text/css" rel="stylesheet" href="css/text.css" />
    &lt;link type="text/css" rel="stylesheet" href="css/styles.css" />
  &lt;/head>
  &lt;body>
    &lt;article id="container">
      &lt;header>
        &lt;h1>Object Oriented CSS&lt;/h1>
        &lt;h2 class="subtitle">(really just a bunch of best practices and patterns; not a new language)&lt;/h2>
        &lt;nav>
          &lt;ul>
             &lt;li>&lt;a href="index.htm" class="selected">Home&lt;/a>&lt;/li>
             &lt;li>&lt;a href="=post.htm">Archives&lt;/a>&lt;/li>
             &lt;li>&lt;a href="#">About&lt;/a>&lt;/li>
             &lt;li>&lt;a href="#">Contact Us&lt;/a>&lt;/li>
          &lt;/ul>
        &lt;/nav>
      &lt;/header>

      &lt;section>
        &lt;article class="post">
          &lt;header>
            &lt;span class="date">September 10, 2009&lt;/span>
            &lt;h2>&lt;a href="post.htm">Check out WorkAwesome!&lt;/a>&lt;/h2>
          &lt;/header>
          &lt;section>
            &lt;img src="http://nettuts.s3.amazonaws.com/450_ooCss/img/wa.jpg" alt="Work Awesome" />
            &lt;p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis urna dui,
            ut tempus tortor. Aliquam enim massa, porta a vehicula in, vehicula at lectus. Ut turpis
            diam, porttitor a iaculis quis, bibendum non lectus. Nullam vitae erat a felis pulvinar
            tempor ut id leo. Aenean accumsan feugiat ultrices. Duis rhoncus eros id odio faucibus
            imperdiet. Nulla nibh mauris, placerat sagittis placerat sed, commodo in mi.&lt;/p>
          &lt;/section>
          &lt;footer>
            &lt;ul>
              &lt;li>&lt;a href="#">Read More . . .&lt;/a>&lt;/li>
              &lt;li>&lt;a href="#">Retweet!&lt;/a>&lt;/li>
              &lt;li>&lt;a href="#">Comments (4)&lt;/a>&lt;/li>
            &lt;/ul>
          &lt;/footer>
        &lt;/article>
        &lt;article class="post ext">
          &lt;header>
            &lt;span class="date">September 7, 2009&lt;/span>
            &lt;h2>The Intro Post&lt;/h2>
          &lt;/header>
          &lt;section>
            &lt;img src="http://nettuts.s3.amazonaws.com/450_ooCss/img/wa.jpg" alt="Work Awesome" />
            &lt;p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis urna dui,
            ut tempus tortor. Aliquam enim massa, porta a vehicula in, vehicula at lectus. Ut turpis
            diam, porttitor a iaculis quis, bibendum non lectus. Nullam vitae erat a felis pulvinar
            tempor ut id leo. Aenean accumsan feugiat ultrices. Duis rhoncus eros id odio faucibus
            imperdiet. Nulla nibh mauris, placerat sagittis placerat sed, commodo in mi.&lt;/p>
          &lt;/section>
          &lt;footer>
            &lt;ul>
              &lt;li>&lt;a href="#">Read More . . .&lt;/a>&lt;/li>
              &lt;li>&lt;a href="#">Retweet!&lt;/a>&lt;/li>
              &lt;li>&lt;a href="#">Comments (4)&lt;/a>&lt;/li>
            &lt;/ul>
          &lt;/footer>
        &lt;/article>
        &lt;article class="post">
          &lt;header>
            &lt;span class="date">September 5, 2009&lt;/span>
            &lt;h2>Welcome&lt;/h2>
          &lt;/header>
          &lt;section>
            &lt;img src="http://nettuts.s3.amazonaws.com/450_ooCss/img/wa.jpg" alt="Work Awesome" />
            &lt;p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis urna dui,
            ut tempus tortor. Aliquam enim massa, porta a vehicula in, vehicula at lectus. Ut turpis
            diam, porttitor a iaculis quis, bibendum non lectus. Nullam vitae erat a felis pulvinar
            tempor ut id leo. Aenean accumsan feugiat ultrices. Duis rhoncus eros id odio faucibus
            imperdiet. Nulla nibh mauris, placerat sagittis placerat sed, commodo in mi.&lt;/p>
          &lt;/section>
          &lt;footer>
            &lt;ul>
              &lt;li>&lt;a href="#">Read More . . .&lt;/a>&lt;/li>
              &lt;li>&lt;a href="#">Retweet!&lt;/a>&lt;/li>
              &lt;li>&lt;a href="#">Comments (4)&lt;/a>&lt;/li>
            &lt;/ul>
          &lt;/footer>
        &lt;/article>
      &lt;/section>
      &lt;aside>
        &lt;article class="side-box">
          &lt;header>
            &lt;h3>Archives&lt;/h3>
            &lt;p>look into the past&lt;/p>
          &lt;/header>
          &lt;section>
            &lt;ul>
              &lt;li>&lt;a href="#">August 2009&lt;/a>&lt;/li>
              &lt;li>&lt;a href="#">July 2009&lt;/a>&lt;/li>
              &lt;li>&lt;a href="#">June 2009&lt;/a>&lt;/li>
              &lt;li>&lt;a href="#">May 2009&lt;/a>&lt;/li>
              &lt;li>&lt;a href="#"> . . . &lt;/a>&lt;/li>
            &lt;/ul>
          &lt;/section>
        &lt;/article>
        &lt;article class="pop-out side-box">
          &lt;header class="post-it">
            &lt;h3>Recent Comments&lt;/h3>
            &lt;p>see what folks are saying&lt;/p>
          &lt;/header>
          &lt;section>
            &lt;ul>
              &lt;li>
                &lt;p>I think oocss is really cool!&lt;/p>
                &lt;p class="meta">By J. Garret on Sept 12, about "The Intro Post"&lt;/p>
              &lt;/li>
              &lt;li>
                &lt;p>Are you kidding? CSS can't ever be Object Oriented.&lt;/p>
                &lt;p class="meta">By Joe Thomas on Sept 11, about "The Intro Post"&lt;/p>
              &lt;/li>
              &lt;li>
                &lt;p>Envato has done it again; workAwesome is simply awesome!&lt;/p>
                &lt;p class="meta">By GV on Sept 10, about "Check out WorkAwesome"&lt;/p>
              &lt;/li>
              &lt;li>
                &lt;p>I really like the site's desing; another work of art from Collis.&lt;/p>
                &lt;p class="meta">By Anonymous on Sept 10, about "Check out WorkAwesome"&lt;/p>
              &lt;/li>
            &lt;/ul>
          &lt;/section>
        &lt;/article>
      &lt;/aside>

      &lt;footer>
        &lt;ul>
          &lt;li>Standard&lt;/li>
          &lt;li>Footer&lt;/li>
          &lt;li>Information&lt;/li>
          &lt;li>Yada&lt;/li>
          &lt;li>Yada&lt;/li>
          &lt;li>&copy; 2009&lt;/li>
        &lt;/ul>
      &lt;/footer>

    &lt;/article>
  &lt;/body>
&lt;/html>
</pre>
<p>I know it&#8217;s kind of long, so here&#8217;s a diagram of our basic structure:</p>
<div class="tutorial_image"><img alt="layout" src="http://nettuts.s3.amazonaws.com/450_ooCss/layout.jpg" /></div>
<p>Look familiar? That&#8217;s our object, with an aside added for our sidebar. We&#8217;ll look at the single post page a bit later, but let&#8217;s hop into some CSS right now!</p>
<p>You&#8217;ll notice that we are linking to three stylesheets: reset.css, text.css, and styles.css. Reset.css is <a href="http://meyerweb.com/eric/tools/css/reset/index.html">Eric Meyer&#8217;s reset</a>. Text.css is important: the second step of object-oriented CSS is styling a few key basic elements; usually, these are text elements, like headers and lists. Styling these objects is important because it determines the consistent look and feel of the site; these elements, for the most part, won&#8217;t receive any additional direct styling.</p>
<p>Here&#8217;s what we&#8217;ve got in our text.css:</p>
<pre class="css" name="code">
body { font: 13px/1.6 Helvetica, Arial, FreeSans, sans-serif;}
h1, h2, h3, h4, h5, h6 { color:#333; }
h1 { font-size: 50px; text-shadow:1px 1px 0 #fff; font-family:arial black, arial}
h2 { font-size: 23px; }
h3 { font-size: 21px; }
h4 { font-size: 19px; }
h5 { font-size: 17px; }
h6 { font-size: 15px; }
p, h1, h2, h3, h4, h5, h6, ul { margin-bottom: 20px; }
article, aside, dialog, figure, footer, header, hgroup, menu, nav, section { display: block; }
</pre>
<p>(Hats off to the <a href="http://960.gs" title="960 Grids System">960 Grid System</a>, whose text.css file this is spun off.)</p>
<p>Now we&#8217;ll start building styles.css; this is where the object-oriented magic begins. However, I&#8217;ll first just throw in a few top-level styles, just to set a body background and some link and list styles.</p>
<pre class="css" name="code">
body
  { background:url(../img/grad.jpg) repeat-x #c0c0c0;
  }
/*Note: All the image will be in the downloadable source code. */
a
  { text-decoration:none;
    color:#474747;
    padding:1px;
  }
a:hover
  { background:#db5500;
    color:#fff;
  }
.selected
  { border-bottom:1px solid #db5500;
  }
li
  { padding-left:15px;
    background:url(../img/bullet.png) 0 5.9px no-repeat;
  }
</pre>
<p>Our first order of business is to define the structure of the page:</p>
<pre class="css" name="code">
#container
  { margin:40px auto;
    width:960px;
    border:1px solid #ccc;
    background:#ececec;
  }
  #container > header, #container > footer
    { padding:80px 80px 80px;
      width:800px;
      overflow:hidden;
      border: 1px solid #ccc;
      border-width:1px 0 1px 0;
    }
  #container > header
    { background:url(../img/header.jpg) repeat-x #d9d9d7;
    }
    #container > header li, #container > footer li
    { float:left;
      padding:0 5px 0 0;
      background:none;
    }
  #container > section
    { background:#fdfdfd;
      padding:0 40px 40px 80px;
      float:left;
      width:493px;
      border-right:1px solid #ccc;
    }
  #container > aside
    { padding-top:20px;
      float:left;
      width:346px;
    }
  #container > footer
  { padding:40px 80px 80px;
    background:#fcfcfc;
  }
    #container > footer li:after
    { content:" |"
    }
      #container > footer li:last-child:after
      { content:""
      }
</pre>
<p>Notice that we&#8217;re styling our container object by starting all our selectors with #container. However, that&#8217;s a special case: usually, you will want to use classes, because they are freely reusable. </p>
<p>We can use class selectors for our post styling:</p>
<pre class="css" name="code">
.post
  { overflow:visible;
    margin-top:40px;
  }
  .post > header
    { margin:0 0 20px 0;
      position:relative;
    }
  .post .date
    { padding:2px 4px ;
      background:#474747;
      color:#ececec;
      font-weight:bold;
      transform:rotate(270deg);
      -moz-transform:rotate(270deg);
      -webkit-transform:rotate(270deg);
      position:absolute;
      top:60px;
      left:-105.5px;
    }
  .post img
    { float:left;
      margin-right:10px;
    }
    .post.ext img
      { float:right;
        margin:0 0 0 10px;
      }
.post footer
    { overflow:hidden;
    }
  .post footer li
    { float:left;
      background:none;
    }
</pre>
<p>Let&#8217;s look at what makes this CSS object-oriented. Firstly, we have not limited the class to a specific element; we could add it to anything. This gives us the most flexibility possible. Then, notice that we haven&#8217;t set any heights or widths; that is part of separating the structure from the skin; we already wrote the sturcture styling, so this object will fill whatever space the structure gives it.</p>
<p>Also, we have styled all the elements involved in an independant way: the parent elements don&#8217;t require certain children to look right; although we have styled child elements, nothing will break if they aren&#8217;t there. And the child elements are, for the most part, not dependant on their parents; I haven&#8217;t styled the h2 in a post object, because it should be consistent across the site; we already took care of that in text.css.</p>
<p>There&#8217;s another important bit, the part most like object-oriented programming: extended classes. You probably saw that we have styling for both .post img and .post.ext img. I&#8217;m sure you know what they will do, but here&#8217;s the proof:</p>
<div class="tutorial_image"><img alt="postext" src="http://nettuts.s3.amazonaws.com/450_ooCss/postext.png" /></div>
<p>Simply by adding another class to your object, you can change minor parts of the look and feel; I should mention that Nicole Sullivan would have created a class called .postExt, and then applied both to the object; I prefer to do it this way, because it allows me to use the same class name (&#8221;ext&#8221;) for all my extensions, and I think it makes the HTML look cleaner. You just have to remember not to put a space in the selector; &#8220;.post .ext&#8221; will look for an element in class ext <em>inside</em> an element in class post. Without the space, it will look for an element in both classes.</p>
<h3>On to the Sidebar</h3>
<p>Now that we have the main content area set out, let&#8217;s look at the sidebar. We&#8217;ve got two objects in the sidebar: an archives list and a recent comments list. To begin, we&#8217;ll create a .side-box class for them:</p>
<pre class="css" name="code">
.side-box
  { padding: 20px 80px 20px 40px;
  }
  .side-box:not(:last-child)
    { border-bottom:1px solid #ccc;
    }
    .side-box > header h3
      { margin-bottom:0;
      }
    .side-box > header p
    { text-transform:uppercase;
      font-style:italic;
      font-size:90%;
    }
</pre>
<p>Again, you should take note that we&#8217;re being careful to follow the two rules: we&#8217;ve separated the structure from the skin by not setting the height or width; the object flows to fill whatever space it needs to take. And we&#8217;ve separated container from content by not making child elements required for proper styling. Yes, I have adjusted the h3 styling, which makes that particular h3 look dependant on the class side-box; in most cases, that&#8217;s undesireable, but in this case, I haven&#8217;t done too much.</p>
<div class="tutorial_image"><img alt="sidebar" src="http://nettuts.s3.amazonaws.com/450_ooCss/sidebar.png" /></div>
<p>But let&#8217;s create an extension for this; however, since we want to do a large visual adjustment, we won&#8217;t extent the side-box class directly; we&#8217;ll create a new class.</p>
<pre class="css" name="code">
.pop-out > section > *
  { display:block;
    background:#fefefe;
    border:1px solid #474747;
    padding:20px;
    position:relative;
    width: 120%;
    left:20px;
  }
</pre>
<p>So what does this do?  Notice the selector: we are targeting every element directly inside the body of our object; when styling an object, you always apply the class to the object itself. This CSS will &#8220;pop&#8221; the content out to the right. </p>
<p><em>&#8220;HEY, you set a width! What about separating struture from skin?&#8221;</em> I set a width because we are targeting the elements inside the body of our object; since the object has padding, that inner element is a bit narrow by itself. That sounds like an excuse for breaking the rule, but I don&#8217;t think I really broke the rule: I set the width as a percentage, which still depends on structure styling further up the cascade.</p>
<p>Here&#8217;s what that looks like.</p>
<div class="tutorial_image"><img alt="popout" src="http://nettuts.s3.amazonaws.com/450_ooCss/popout.png" /></div>
<p>I just told you that you should always apply classes to the top-level objects, and use child selectors to mold the inner elements. But part of object-oriented CSS is being able to mix and match styles easily. It&#8217;s quite possible you&#8217;ll want to use a similar header on two objects that aren&#8217;t really related in any way. It&#8217;s ideal to have a collection of header and footer classes that you apply directly to the header or footer of an object. This way, you aren&#8217;t adding similar code to multiple non-related classes, but abstracting that away and applying it in the relevent place. Let&#8217;s create an abstracted header.</p>
<p>You&#8217;ll notice that we gave the header of our recent comments object a class called &#8216;post-it.&#8217; Let&#8217;s create that class now.</p>
<pre class="css" name="code">
.post-it
  { border:1px solid #db5500;
    padding: 10px;
    font-style:italic;
    position:relative;
    background:#f2965c;
    color:#333;
    transform:rotate(356deg);
    -moz-transform:rotate(356deg);
    -webkit-transform:rotate(356deg);
    z-index:10;
    top:10px;
    box-shadow:1px 1px 0px #333;
    -moz-box-shadow:1px 1px 0px #333;
    -webkit-box-shadow:1px 1px 0px #333;
  }
</pre>
<div class="tutorial_image"><img alt="postit" src="http://nettuts.s3.amazonaws.com/450_ooCss/postit.png" />
<p>A poor semblance of a post-it note!</p>
</div>
<p>It&#8217;s important to stress that even though we plan to use this for a header, we haven&#8217;t specified that in the selector. If this resembles a common design aspect of our site, we may end up wanting this style for something else; for example, we could extend it with classes that adjust the colour and rotation, and use them on a wall or bulliten board or something similar. </p>
<blockquote>
<p>
Don&#8217;t lock down your selectors</strong>; you never know when you&#8217;ll want those styles!
</p>
</blockquote>
<p>Often you&#8217;ll want to take it further than just header and footer classes; a component library is a huge part of object-oriented CSS; a basic rule might be this: whenever you want to apply the same style in multiple un-related places, abstract. Of course, you might find after hard coding something into an object that it would be more useful if you could use it elsewhere; don&#8217;t be afraid to refactor. It&#8217;s always to your benefit. </p>
<p>You&#8217;ll see these components in the page header and the recent comments box.</p>
<pre class="css" name="code">
.meta
  { font-size:75%;
    font-style:italic;
  }
.subtitle
  { text-transform:uppercase;
    font-size:90%;
    font-weight:bold;
    letter-spacing:1px;
    text-shadow:1px 1px 0 #fff;
  }
</pre>
<p>Well, we&#8217;re done with our index page; let&#8217;s look at a bit more object-oriented-ness on a single post page. Just copy that index page, rename it &#8216;post.htm&#8217; and swap out the page header and section for this code.</p>
<pre class="html" name="code">
      &lt;header>
        &lt;h1>Object Oriented CSS&lt;/h1>
        &lt;p class="subtitle">(really just a bunch of best practices and patterns; not a new language)&lt;/p>
        &lt;nav>
          &lt;ul>
             &lt;li>&lt;a href="index.htm">Home&lt;/a>&lt;/li>
             &lt;li>&lt;a href="#" class="selected">Archives&lt;/a>&lt;/li>
             &lt;li>&lt;a href="#">About&lt;/a>&lt;/li>
             &lt;li>&lt;a href="#">Contact Us&lt;/a>&lt;/li>
          &lt;/ul>
        &lt;/nav>
      &lt;/header>
      &lt;section>
        &lt;article class="post">
          &lt;header>
            &lt;span class="date">September 10, 2009&lt;/span>
            &lt;h2>Check out WorkAwesome!&lt;/h2>
          &lt;/header>
          &lt;section>
            &lt;img src="http://nettuts.s3.amazonaws.com/450_ooCss/img/wa.jpg" alt="Work Awesome" />
            &lt;p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis urna dui,
            ut tempus tortor. Aliquam enim massa, porta a vehicula in, vehicula at lectus. Ut turpis
            diam, porttitor a iaculis quis, bibendum non lectus. Nullam vitae erat a felis pulvinar
            tempor ut id leo. Aenean accumsan feugiat ultrices. Duis rhoncus eros id odio faucibus
            imperdiet. Nulla nibh mauris, placerat sagittis placerat sed, commodo in mi.&lt;/p>
            &lt;p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis urna dui,
            ut tempus tortor. Aliquam enim massa, porta a vehicula in, vehicula at lectus. Ut turpis
            diam, porttitor a iaculis quis, bibendum non lectus. Nullam vitae erat a felis pulvinar
            tempor ut id leo. Aenean accumsan feugiat ultrices. Duis rhoncus eros id odio faucibus
            imperdiet. Nulla nibh mauris, placerat sagittis placerat sed, commodo in mi.&lt;/p>
            &lt;p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis urna dui,
            ut tempus tortor. Aliquam enim massa, porta a vehicula in, vehicula at lectus. Ut turpis
            diam, porttitor a iaculis quis, bibendum non lectus. Nullam vitae erat a felis pulvinar
            tempor ut id leo. Aenean accumsan feugiat ultrices. Duis rhoncus eros id odio faucibus
            imperdiet. Nulla nibh mauris, placerat sagittis placerat sed, commodo in mi.&lt;/p>
            &lt;p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis urna dui,
            ut tempus tortor. Aliquam enim massa, porta a vehicula in, vehicula at lectus. Ut turpis
            diam, porttitor a iaculis quis, bibendum non lectus. Nullam vitae erat a felis pulvinar
            tempor ut id leo. Aenean accumsan feugiat ultrices. Duis rhoncus eros id odio faucibus
            imperdiet. Nulla nibh mauris, placerat sagittis placerat sed, commodo in mi.&lt;/p>
          &lt;/section>
          &lt;footer>
            &lt;ul>
              &lt;li>&lt;a href="#">Digg!&lt;/a>&lt;/li>
              &lt;li>&lt;a href="#">Share!&lt;/a>&lt;/li>
              &lt;li>&lt;a href="#">Like!&lt;/a>&lt;/li>
            &lt;/ul>
          &lt;/footer>
        &lt;/article>
        &lt;article class="comments">
          &lt;header>&lt;h2>Comments&lt;/h2>&lt;/header>
          &lt;section>
            &lt;article class="comment">
              &lt;header>&lt;p>First-commenter&lt;/p>&lt;p class="meta">Sept 10&lt;/p>&lt;/header>
              &lt;section>&lt;p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis urna dui,&lt;/p>&lt;/section>
            &lt;/article>
            &lt;article class="reply comment">
                  &lt;header>&lt;p>Reply-commenter&lt;/p>&lt;p class="meta">Sept 12&lt;/p>&lt;/header>
                  &lt;section>&lt;p>diam, porttitor a iaculis quis, bibendum non lectus. Nullam vitae erat a felis pulvinar&lt;/p>&lt;/section>
                &lt;/article>
            &lt;article class="comment">
              &lt;header>&lt;p>Second-commenter&lt;/p>&lt;p class="meta">Sept 10&lt;/p>&lt;/header>
              &lt;section>&lt;p>tempor ut id leo. Aenean accumsan feugiat ultrices. Duis rhoncus eros id odio faucibus &lt;/p>&lt;/section>
            &lt;/article>
            &lt;article class="author comment">
              &lt;header>&lt;p>The author&lt;/p>&lt;p class="meta">Sept 11&lt;/p>&lt;/header>
              &lt;section>&lt;p>diam, porttitor a iaculis quis, bibendum non lectus. Nullam vitae erat a felis pulvinar&lt;/p>&lt;/section>
            &lt;/article>
            &lt;article class="comment">
              &lt;header>&lt;p>Fourth-commenter&lt;/p>&lt;p class="meta">Sept 12&lt;/p>&lt;/header>
              &lt;section>&lt;p>imperdiet. Nulla nibh mauris, placerat sagittis placerat sed, commodo in mi&lt;/p>&lt;/section>
            &lt;/article>
          &lt;/section>
</pre>
<p>Now we can see where some reusability comes in; we can use the post class for the blog posting here; that&#8217;s the same post class we used for the home page; because we didn&#8217;t specify the structure, but let the object expand to take in whatever we give it, it can handle the whole post. </p>
<p>\</p>
<h3>Comments</h3>
<p>Now let&#8217;s turn our attention to the comments on this page; comments are a great place to use object-oriented CSS. We&#8217;ll start by adding this:</p>
<pre class="css" name="code">
.comment
  { border:1px solid #ccc;
    border-radius:7px;
    -moz-border-radius:7px;
    -webkit-border-radius:7px;
    padding:10px;
    margin:0 0 10px 0;
  }
    .comment > header > p
      { font-weight:bold;
        display:inline;
        margin:0 10px 20px 0;
      }
</pre>
<p>This alone gives us some psuedo-attractive comments; but we can do more. Our HTML has classes for replies and author comments.</p>
<pre class="css" name="code">
.reply.comment
  { margin-left:80px;
  }
.author.comment
  { color:#ececec;
    background:#474747;
  }
</pre>
<p>Make sure you don&#8217;t have a space between the two class names. This is a bit different from what we did earlier: instead of changing the styling of the comment class, we are actually extending it (So maybe these are the real extended classes).</p>
<p>And the finished comments . . .</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/450_ooCss/comments.png" alt="comments" /></div>
<p>And while we&#8217;re here, let&#8217;s add a comment form to our component library.</p>
<pre class="css" name="code">
.comments-form p
  { padding:5px;
    border-radius:5px;
    -moz-border-radius:5px;
    -webkit-border-radius:5px;
  }
.comments-form p:hover
  { background:#ececec;
  }
.comments-form label
  { display:inline-block;
    width:70px;
    vertical-align:top; }
.comments-form label:after
  { content: " : ";
  }
.comments-form input[type=text],
.comments-form button,
.comments-form textarea
  { width:200px;
    border:1px solid #ccc;
    border-radius:5px;
    -moz-border-radius:5px;
    -webkit-border-radius:5px;
    padding:2px;
  }
.comments-form button[type=submit]
  { margin-left:70px;
  }
</pre>
<p>This gives us a nice rounded-corner form with a soft hover effect. </p>
<div class="tutorial_image"><img alt="commentsform" src="http://nettuts.s3.amazonaws.com/450_ooCss/commentsform.png" /></div>
<p>I&#8217;m kind of torn here; a fundamental concept of object-oriented CSS is the ability to add a class to different elements; but what about this case? This class would basically be useless on anything but a form; should we change the selectors to form.comments-form as a kind of security? What do you think?</p>
<p>Well, that really covers the ideas of object-oriented CSS.</p>
<h3>Review the Main Steps</h3>
<ol>
<li>Determine your object.</li>
<li>Set your reset, base elements, and base object styles.</li>
<li>Apply classes to objects and components for the desired look and feel.</li>
</ol>
<p>I didn&#8217;t use the standard message example, but it&#8217;s really a great way to get into object-oriented CSS. Try creating an message class that you can apply to different elements, like a paragraph, a list item, or a complete object. Then, extend it with error, warning, and info classes. See how flexible and reuseable you can make it.</p>
<h3>Is Object-Oriented CSS Always Useful?</h3>
<p>I don&#8217;t think the answer to this question is a simple &#8220;yes&#8221; or &#8220;no&#8221;; that&#8217;s probably because object-oriented CSS isn&#8217;t a language, but a collection of patterns and best practices. You may not always want to apply the idea of the standard HTML object, but most of the CSS principles are always valid. If you think about it, you&#8217;ll see it&#8217;s really about where you want to optimize: you could code you&#8217;re HTML minimalistically, with a few ids for handles, and be able to style it easily; but the CSS won&#8217;t be reusable, and the site might break later when you come to change things. Or you could write a bit more HTML and optimze the CSS with the principles we&#8217;ve looked at in this tutorial. </p>
<p>I&#8217;m willing to expand my HTML a bit so that I can write CSS that is reusable, flexible, and DRY. What&#8217;s your opinion? </p>
<h3>Other Resources</h3>
<ul>
<li><a href="http://www.stubbornella.org/content/2009/02/28/object-oriented-css-grids-on-github/">Nicole Sullivan&#8217;s Blog Post on OOCSS</a></li>
<li><a href="http://github.com/stubbornella/oocss">Sullivan&#8217;s OOCSS framework on Github</a></li>
<li><a href="http://developer.yahoo.net/blogs/theater/archives/2009/03/website_and_webapp_performance.html">And her presentation on OOCSS</a></li>
</ul>
<ul class="webroundup">
<li>Follow us on <a href="http://www.twitter.com/nettuts">Twitter</a>, or subscribe to the <a href="http://feeds.feedburner.com/nettuts" title="Nettuts+ RSS Feed">Nettuts+ RSS Feed</a> for more daily web development tuts and articles.</li>
</ul>
<p>
<script type="text/javascript"><!--digg_url = "post permalink (not digg url)"; // -->
</script><br />
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://net.tutsplus.com/tutorials/html-css-techniques/object-oriented-css-what-how-and-why/feed/</wfw:commentRss>
		<slash:comments>131</slash:comments>
		</item>
		<item>
		<title>30 CSS Best Practices for Beginners &#8211; Basix</title>
		<link>http://net.tutsplus.com/tutorials/html-css-techniques/30-css-best-practices-for-beginners/</link>
		<comments>http://net.tutsplus.com/tutorials/html-css-techniques/30-css-best-practices-for-beginners/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 17:46:08 +0000</pubDate>
		<dc:creator>Glen Stansberry</dc:creator>
				<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[basix]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[css best practices]]></category>

		<guid isPermaLink="false">http://net.tutsplus.com/?p=6741</guid>
		<description><![CDATA[<img src="http://nettuts.s3.amazonaws.com/438_cssBestPractices/200x200.jpg" alt="30 CSS Best Practices for Beginners" />]]></description>
			<content:encoded><![CDATA[<p>CSS is a language that is used by nearly <em>every</em> developer at some point. While it&#8217;s a language that we sometimes take for granted, it is powerful and has many nuances that can help (or hurt) our designs. Here are thirty of the best CSS practices that will keep you writing solid CSS and avoiding some costly mistakes.</p>
<p><span id="more-6741"></span></p>
<h3>1. Make it Readable</h3>
<p>The readability of your CSS is incredibly important, though most people overlook <em>why</em> it&#8217;s important. Great readability of your CSS makes it much easier to maintain in the future, as you&#8217;ll be able to find elements quicker. Also, you&#8217;ll never know who might need to look at your code later on.</p>
<p>
<strong>&lt;editors-note> </strong>When writing CSS, most developers fall into one of two groups.
</p>
<h4>Group 1: All on one line</h4>
<pre name="code" class="css">
.someDiv { background: red; padding: 2em; border: 1px solid black; }
</pre>
<h4>Group 2: Each style gets its own line</h4>
<pre name="code" class="css">
.someDiv {
  background: red;
  padding: 2em;
  border: 1px solid black;
}
</pre>
<p>Both practices are perfectly acceptable, though you&#8217;ll generally find that group two despises group one! Just remember &#8211; choose the method that looks best TO YOU. That&#8217;s all that matters. <strong>&lt;/editors-note></strong></p>
<h3>2. Keep it Consistent</h3>
<p>Along the lines of keeping your code readable is making sure that the CSS is consistent. You should start to develop your own &#8220;sub-language&#8221; of CSS that allows you to quickly name things. There are certain classes that I create in nearly every theme, and I use the same name each time. For example, I use &#8220;.caption-right&#8221; to float images which contain a caption to the right.</p>
<p>Think about things like whether or not you&#8217;ll use underscores or dashes in your ID&#8217;s and class names, and in what cases you&#8217;ll use them. When you start creating your own standards for CSS, you&#8217;ll become much more proficient.</p>
<h3>3. Start with a Framework</h3>
<p>Some design purists scoff at the thought of using a <a href="http://www.smashingmagazine.com/2007/09/21/css-frameworks-css-reset-design-from-scratch/">CSS framework</a> with each design, but I believe that if someone else has taken the time to maintain a tool that speeds up production, why reinvent the wheel? I know frameworks shouldn&#8217;t be used in every instance, but most of the time they can help.</p>
<p>Many designers have their own framework that they have created over time, and that&#8217;s a great idea too. It helps keep consistency within the projects.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/438_cssBestPractices/blueprint.png" alt="CSS frameworks" /></div>
<p><strong>&lt;editors-note></strong> I disagree. CSS frameworks are fantastic tools&#8230;for those who know how to use them. </p>
<blockquote><p> It&#8217;s less a matter of reinventing the wheel, and more a matter of understanding how the wheel works. </p>
</blockquote>
<p>If you&#8217;re just getting started with CSS, I&#8217;d personally recommend that you stay away from these frameworks for at least a year. Otherwise, you&#8217;ll only confuse yourself. <strong>Learn CSS&#8230;then take shortcuts! </strong> <strong>&lt;/editors-note></strong>  </p>
<h3>4. Use a Reset</h3>
<p>Most CSS frameworks have a reset built-in, but if you&#8217;re not going to use one then <em>at least</em> consider using a reset. Resets essentially eliminate browser inconsistencies such as heights, font sizes, margins, headings, etc. The reset allows your layout look consistent in all browsers.</p>
<p> The <a href="http://meyerweb.com/eric/tools/css/reset/index.html">MeyerWeb</a> is a popular reset, along with <a href="http://developer.yahoo.com/yui/reset/">Yahoo&#8217;s developer reset</a>. Or you could always <a href="http://net.tutsplus.com/tutorials/html-css-techniques/weekend-quick-tip-create-your-own-resetcss-file/">roll your own reset</a>, if that tickles your fancy.</p>
<h3>5. Organize the Stylesheet with a Top-down Structure</h3>
<p>It always makes sense to lay your stylesheet out in a way that allows you to quickly find parts of your code. I recommend a top-down format that tackles styles as they appear in the source code. So, an example stylesheet might be ordered like this:</p>
<ol>
<li>Generic classes (body, a, p, h1, etc.)</li>
<li>#header</li>
<li>#nav-menu</li>
<li>#main-content</li>
</ol>
<p> <strong>&lt;editors-note></strong> Don&#8217;t forget to comment each section! <strong>&lt;/editors-note></strong></p>
<pre name="code" class="css">
/****** main content *********/

styles goes here...

/****** footer *********/

styles go here...
</pre>
<h3>6. Combine Elements</h3>
<p>Elements in a stylesheet sometimes share properties. Instead of re-writing previous code, why not just combine them? For example, your h1, h2, and h3 elements might all share the same font and color:</p>
<pre name="code" class="css">
	h1, h2, h3 {font-family: tahoma, color: #333}
</pre>
<p>We could add unique characteristics to each of these header styles if we wanted (ie. h1 {size: 2.1em}) later in the stylesheet.</p>
<h3>7. Create Your HTML First</h3>
<p>Many designers create their CSS at the same time they create the HTML. It seems logical to create both at the same time, but actually you&#8217;ll save even more time if you create the <em>entire</em> HTML mockup first. The reasoning behind this method is that you know all the elements of your site layout, but you don&#8217;t know what CSS you&#8217;ll need with your design. Creating the HTML layout first allows you to visualize the entire page as a whole, and allows you to think of your CSS in a more holistic, top-down manner.</p>
<h3>8. Use Multiple Classes</h3>
<p>Sometimes it&#8217;s beneficial to add multiple classes to an element. Let&#8217;s say that you have a &lt;div> &#8220;box&#8221; that you want to float right, and you&#8217;ve already got a class .right in your CSS that floats everything to the right. You can simply add an extra class in the declaration, like so:</p>
<pre name="code" class="css">
	&lt;div class="box right">&lt;/div>
</pre>
<p>You can add as many classes as you&#8217;d like (<strong>space</strong> separated) to any declaration.</p>
<p><strong>&lt;editors-note></strong> Be very careful when using ids and class-names like &#8220;left&#8221; and &#8220;right.&#8221; I will use them, but only for things such as blog posts. How come? Let&#8217;s imagine that, down the road, you decide that you&#8217;d rather see the box floated to the LEFT. In this case, you&#8217;d have to return to your HTML and change the class-name &#8211; all in order to adjust the <em>presentation</em> of the page. This is unsemantic. Remember &#8211; HTML is for markup and content. CSS is for presentation. </p>
<blockquote>
<p>
If you must return to your HTML to change the presentation (or styling) of the page, you&#8217;re doing it wrong!
</p>
</blockquote>
<p>
<strong>&lt;/editors-note></strong>
</p>
<h3>9. Use the Right Doctype</h3>
<p>The doctype declaration matters a whole lot on whether or not your markup and CSS will validate. In fact, the entire look and feel of your site can change greatly depending on the DOCTYPE that you declare.</p>
<p>Learn more about which DOCTYPE to use at <a href="http://www.alistapart.com/articles/doctype/" title="DOCTYPE">A List Apart</a>.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/438_cssBestPractices/doctype.png" alt="doctype" /></div>
<p>
<strong>&lt;editors-note></strong>
</p>
<p>I use HTML5&#8217;s doctype for all of my projects. </p>
<pre name="code" class="html">
&lt;!DOCTYPE html>
</pre>
<blockquote>
<p>
&#8220;What&#8217;s nice about this new DOCTYPE, especially, is that all current browsers (IE, FF, Opera, Safari) will look at it and switch the content into standards mode &#8211; even though they don&#8217;t implement HTML5. This means that you could start writing your web pages using HTML5 today and have them last for a very, very, long time.&#8221;
</p>
<div><em><a href="http://ejohn.org/blog/html5-doctype/">- John Resig </a></em></div>
</blockquote>
<p><strong>&lt;/editors-note></strong></p>
<h3>10. Use Shorthand</h3>
<p>You can shrink your code considerably by using shorthand when crafting your CSS. For elements like padding, margin, font and some others, you can combine styles in one line. For example, a div might have these styles:</p>
<pre name="code" class="css">
	#crayon {
		margin-left:	5px;
		margin-right:	7px;
		margin-top:	8px;
	}
</pre>
<p>You could combine those styles in one line, like so:</p>
<pre name="code" class="css">
	#crayon	{
		margin: 8px 7px 0px 5px; // top, right, bottom, and left values, respectively.
	}
</pre>
<p>If you need more help, here&#8217;s a <a href="http://www.dustindiaz.com/css-shorthand/">comprehensive guide on CSS shorthand properties</a>.</p>
<h3>11. Comment your CSS</h3>
<p>Just like any other language, it&#8217;s a great idea to comment your code in sections. To add a comment, simply add /* behind the comment, and */ to close it, like so:</p>
<pre name="code" class="css">
	/* Here's how you comment CSS */
</pre>
<h3>12. Understand the Difference Between Block vs. Inline Elements</h3>
<p>Block elements are elements that naturally clear each line after they&#8217;re declared, spanning the whole width of the available space. Inline elements take only as much space as they need, and don&#8217;t force a new line after they&#8217;re used.</p>
<p>Here are the lists of elements that are either inline or block:</p>
<pre name="code" class="css">span, a, strong, em, img, br, input, abbr, acronym</pre>
<p>	And the block elements:</p>
<pre name="code" class="css">div, h1...h6, p, ul, li, table, blockquote, pre, form</pre>
</p>
<h3>13. Alphabetize your Properties</h3>
<p>While this is more of a frivolous tip, it can come in handy for quick scanning.</p>
<pre name="code" class="css">
	#cotton-candy {
		color: #fff;
		float: left;
		font-weight:
		height: 200px;
		margin: 0;
		padding: 0;
		width: 150px;
	}
</pre>
<p><strong>&lt;editors-note></strong> Ehh&#8230; sacrifice speed for slightly improved readability? I&#8217;d pass &#8211; but decide for yourself! <strong>&lt;/editors-note></strong></p>
<h3>14. Use CSS Compressors</h3>
<p>CSS compressors help shrink CSS file size by removing line breaks, white spaces, and combining elements. This combination can greatly reduce the the file size, which speeds up browser loading. <a href="http://www.cssoptimiser.com">CSS Optimizer</a> and <a href="http://www.csscompressor.com">CSS Compressor</a> are two excellent online tools that can shrink CSS.</p>
<p>It should be noted that shrinking your CSS can provide gains in performance, but you lose some of the readability of your CSS.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/438_cssBestPractices/cssoptimize.png" alt="use css compressors" /></div>
<h3>15. Make Use of Generic Classes</h3>
<p>You&#8217;ll find that there are certain styles that you&#8217;re applying over and over. Instead of adding that particular style to each ID, you can create generic classes and add them to the IDs or other CSS classes (using tip #8).</p>
<p>For example, I find myself using float:right and float:left over an over in my designs. So I simply add the classes .left and .right to my stylesheet, and reference it in the elements.</p>
<pre name="code" class="css">
	.left {float:left}
	.right {float:right}

	&lt;div id="coolbox" class="left">...&lt;/div>
</pre>
<p>This way you don&#8217;t have to constantly add &#8220;float:left&#8221; to all the elements that need to be floated.</p>
<p> <strong>&lt;editors-note></strong> Please refer to editor&#8217;s notes for #8. <strong>&lt;/editors-note></strong></p>
<h3>16. Use &#8220;Margin: 0 auto&#8221; to Center Layouts</h3>
<p>Many beginners to CSS can&#8217;t figure out why you can&#8217;t simply use <em>float: center</em> to achieve that centered effect on block-level elements. If only it were that easy! Unfortunately, you&#8217;ll need to use</p>
<pre name="code" class="css">
	margin: 0 auto; // top, bottom - and left, right values, respectively.
</pre>
<p>to center divs, paragraphs or other elements in your layout.</p>
<p><strong>&lt;editors-note></strong> By declaring that both the left AND the right margins of an element must be identical, the have no choice but to center the element within its containing element. <strong>&lt;/editors-note></strong></p>
<h3>17. Don&#8217;t Just Wrap a DIV Around It</h3>
<p>When starting out, there&#8217;s a temptation to wrap a div with an ID or class around an element and create a style for it.</p>
<pre name="code" class="css">
	&lt;div class="header-text">&lt;h1>Header Text&lt;/h1>&lt;/div>
</pre>
<p>Sometimes it might seem easier to just create unique element styles like the above example, but you&#8217;ll start to clutter your stylesheet. This would have worked just fine:</p>
<pre name="code" class="css">
	&lt;h1>Header Text&lt;/h1>
</pre>
<p>Then you can easily add a style to the h1 instead of a parent div.</p>
<h3>18. Use Firebug</h3>
<p>If you haven&#8217;t downloaded <a href="http://www.getfirebug.com">Firebug</a> yet, stop and go do it. Seriously. This little tool is a <em>must have</em> for any web developer. Among the many features that come bundled with the Firefox extension (debug JavaScript, inspect HTML, find errors), you can also visually inspect, modify, and edit CSS in real-time. You can learn more about what Firebug does on the official <a href="http://www.getfirebug.com">Firebug website</a>.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/438_cssBestPractices/firebug.png" alt="use firebug" /></div>
<h3>19. Hack Less</h3>
<p>Avoid using as little browser-specific hacks if at all possible. There is a tremendous pressure to make sure that designs look consistent across all browsers, but using hacks only makes your designs harder to maintain in the future. Plus, using a reset file (see #4) can eliminate nearly all of the rendering irregularities between browsers.</p>
<h3>20. Use Absolute Positioning Sparingly</h3>
<p><a href="http://www.w3schools.com/Css/pr_class_position.asp">Absolute positioning</a> is a handy aspect of CSS that allows you to define where <em>exactly</em> an element should be positioned on a page to the exact pixel. However, because of absolute positioning&#8217;s disregard for other elements on the page, the layouts can get quite hairy if there are multiple absolutely positioned elements running around the layout.</p>
<h3>21. Use Text-transform</h3>
<p><a href="http://www.w3schools.com/Css/pr_text_text-transform.asp">Text-transform</a> is a highly-useful CSS property that allows you to &#8220;standardize&#8221; how text is formatted on your site. For example, say you&#8217;re wanting to create some headers that only have lowercase letters. Just add the text-transform property to the header style like so:</p>
<pre name="code" class="css">text-transform: lowercase;</pre>
<p>Now all of the letters in the header will be lowercase by default. Text-transform allows you to modify your text (first letter capitalized, all letters capitalized, or all lowercase) with a simple property.</p>
<h3>22. Don&#8217;t use Negative Margins to Hide Your h1</h3>
<p>Oftentimes people will use an image for their header text, and then either use display:none or a negative margin to float the h1 off the page. <a href="http://www.mattcutts.com">Matt Cutts</a>, the head of Google&#8217;s Webspam team, has officially said that this is a bad idea, as Google might think it&#8217;s spam.</p>
<div class="tutorial_image">
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/fBLvn_WkDJ4&#038;color1=0xb1b1b1&#038;color2=0xcfcfcf&#038;hl=en&#038;feature=player_embedded&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.youtube.com/v/fBLvn_WkDJ4&#038;color1=0xb1b1b1&#038;color2=0xcfcfcf&#038;hl=en&#038;feature=player_embedded&#038;fs=1" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="425" height="344"></embed></object>
</div>
<p>As Mr. Cutts explicitly says, avoid hiding your logo&#8217;s text with CSS. Just use the alt tag. While many claim that you can still use CSS to hide a h1 tag as long as the h1 is the same as the logo text, I prefer to err on the safe side.</p>
<h3>23. Validate Your CSS and XHTML</h3>
<p>Validating your CSS and XHTML does more than give a sense of pride: it helps you quickly spot errors in your code. If you&#8217;re working on a design and for some reason things just aren&#8217;t looking right, try running the <a href="http://validator.w3.org/">markup</a> and <a href="http://jigsaw.w3.org/css-validator/">CSS validator</a> and see what errors pop up. Usually you&#8217;ll find that you forgot to close a div somewhere, or a missed semi-colon in a CSS property.</p>
<h3>24. Ems vs. Pixels</h3>
<p>There&#8217;s always been a strong debate as to whether it&#8217;s better to use pixels (px) or ems (em) when defining font sizes. Pixels are a more static way to define font sizes, and ems are more scalable with different browser sizes and mobile devices. With the advent of many different types of web browsing (laptop, mobile, etc.), ems are increasingly becoming the default for font size measurements as they allow the greatest form of flexibility. You can read more about why you should use ems for font sizes in <a href="http://www.astahost.com/Sizes-Webdesign-Em-Vs-Px-t8926.html">this thoughtful forum thread</a>. About.com also has a great article on the <a href="http://webdesign.about.com/cs/typemeasurements/a/aa042803a.htm">differences between the measurement sizes</a>.</p>
<p> <strong>&lt;editors-note></strong>Don&#8217;t take me to the farm on this one &#8211; but I&#8217;d be willing to bet that, thanks to browser zooming, the majority of designers are defaulting to pixel based layouts. What do you think? <strong>&lt;/editors-note></strong></p>
<h3>25. Don&#8217;t Underestimate the List</h3>
<p>Lists are a great way to present data in a structured format that&#8217;s easy to modify the style. Thanks to the display property, you don&#8217;t have to just use the list as a text attribute. Lists are also great for creating navigation menus and things of the sort.</p>
<p>Many beginners use divs to make each element in the list because they don&#8217;t understand how to properly utilize them. It&#8217;s well worth the effort to use brush up on learning list elements to structure data in the future.</p>
<p> <strong>&lt;editors-note></strong> </p>
<p>You&#8217;ll often see navigation links like so: </p>
<pre name="code" class="css">
 <a href="#">Home</a>
 <a href="#">About</a>
 <a href="#">Services</a>
 <a href="#">Contact</a>
</pre>
<p>Though, technically, this will work just fine after a bit of CSS, it&#8217;s sloppy. If you&#8217;re adding a <em>list</em> of links, use an <em>unordered list</em>, silly goose! </p>
<p> <strong>&lt;/editors-note></strong></p>
<h3>26. Avoid Extra Selectors</h3>
<p>It&#8217;s easy to unknowingly add extra selectors to our CSS that clutters the stylesheet. One common example of adding extra selectors is with lists.</p>
<pre name="code" class="css">body #container .someclass ul li {....}</pre>
<p>In this instance, just the <em>.someclass li</em> would have worked just fine.</p>
<pre name="code" class="css">.someclass li {...}</pre>
<p>Adding extra selectors won&#8217;t bring Armageddon or anything of the sort, but they do keep your CSS from being as simple and clean as possible.</p>
<h3>27. Add Margins and Padding to All</h3>
<p>Different browsers render elements differently. IE renders certain elements differently than Firefox. IE 6 renders elements differently than IE 7 and IE 8. While the browsers are starting to adhere more closely to <a href="http://www.w3.org/">W3C standards</a>, they&#8217;re still not perfect (*cough IE cough*).</p>
<p>One of the main differences between versions of browsers is how padding and margins are rendered. If you&#8217;re not already using a reset, you might want to define the margin and padding for all elements on the page, to be on the safe side. You can do this quickly with a global reset, like so:</p>
<pre name="code" class="css">
	* {margin:0;padding:0;}
</pre>
<p>Now <em>all</em> elements have a padding and margin of 0, unless defined by another style in the stylesheet.</p>
<p> <strong>&lt;editors-note></strong> The problem is that, because EVERYTHING is zeroed out with this method, you&#8217;ll potentially cause yourself more harm than help. Are you sure that you want every single element&#8217;s margins and padding zeroed? If so &#8211; that&#8217;s perfectly acceptable. But at least consider it. <strong>&lt;/editors-note></strong></p>
<h3>28. When Ready, Try Object Oriented CSS</h3>
<p>Object Oriented programming is the separation of elements in the code so that they&#8217;re easier to maintain reuse. <a href="http://wiki.github.com/stubbornella/oocss">Object Oriented CSS</a> follows the same principle of separating different aspects of the stylesheet(s) into more logical sections, making your CSS more modular and reusable.</p>
<p>Here&#8217;s is a slide presentation of how Object Oriented CSS works:</p>
<div class="tutorial_image"><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=oocss-1233786987806904-3&#038;stripped_title=object-oriented-css" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=oocss-1233786987806904-3&#038;stripped_title=object-oriented-css" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">From <a style="text-decoration:underline;" href="http://www.slideshare.net/stubbornella">Nicole Sullivan</a>.</div>
</div>
<p>If you&#8217;re new to the CSS/XHTML game, OOCSS might be a bit of a challenge in the beginning. But the principles are great to understand for object oriented programming in general.</p>
<h3>29. Use Multiple Stylesheets</h3>
<p>Depending on the complexity of the design and the size of the site, it&#8217;s sometimes easier to make smaller, multiple stylesheets instead of one giant stylesheet. Aside from it being easier for the designer to manage, multiple stylesheets allow you to leave out CSS on certain pages that don&#8217;t need them.</p>
<p>For example, I might having a polling program that would have a unique set of styles. Instead of including the poll styles to the main stylesheet, I could just create a <em>poll.css</em> and the stylesheet only to the pages that show the poll.</p>
<p> <strong>&lt;editors-note></strong> However, be sure to consider the number of HTTP requests that are being made. Many designers prefer to develop with multiple stylesheets, and then combine them into one file. This reduces the number of HTTP requests to one. Also, the entire file will be cached on the user&#8217;s computer. <strong>&lt;/editors-note></strong></p>
<h3>30. Check for Closed Elements First When Debugging</h3>
<p>If you&#8217;re noticing that your design looks a tad wonky, there&#8217;s a good chance it&#8217;s because you&#8217;ve left off a closing <em>&lt;/div></em>. You can use the <a href="http://validator.w3.org/">XHTML validator</a> to help sniff out all sorts of errors too.</p>
<h3>You Might Also Enjoy&#8230;</h3>
<ul>
<li><a href="http://net.tutsplus.com/tutorials/html-css-techniques/20-html-forms-best-practices-for-beginners/">20 HTML Forms Best Practices for Beginners</a></li>
<li><a href="http://net.tutsplus.com/tutorials/php/30-php-best-practices-for-beginners/">30+ PHP Best Practices for Beginners</a></li>
<li><a href="http://net.tutsplus.com/tutorials/javascript-ajax/24-javascript-best-practices-for-beginners/">24 JavaScript Best Practices for Beginners</a></li>
<li><a href="http://net.tutsplus.com/tutorials/html-css-techniques/30-html-best-practices-for-beginners/">30 HTML Best Practices for Beginners</a></li>
</ul>
<ul class="webroundup">
<li>Follow us on <a href="http://www.twitter.com/nettuts">Twitter</a>, or subscribe to the <a href="http://feeds.feedburner.com/nettuts" title="Nettuts+ RSS Feed">Nettuts+ RSS Feed</a> for more daily web development tuts and articles.</li>
</ul>
<p>
<script type="text/javascript"><!--digg_url = "post permalink (not digg url)"; // -->
</script><br />
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://net.tutsplus.com/tutorials/html-css-techniques/30-css-best-practices-for-beginners/feed/</wfw:commentRss>
		<slash:comments>125</slash:comments>
		</item>
		<item>
		<title>11 Classic CSS Techniques Made Simple with CSS3</title>
		<link>http://net.tutsplus.com/tutorials/html-css-techniques/11-classic-css-techniques-made-simple-with-css3/</link>
		<comments>http://net.tutsplus.com/tutorials/html-css-techniques/11-classic-css-techniques-made-simple-with-css3/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 16:29:51 +0000</pubDate>
		<dc:creator>Vasili</dc:creator>
				<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[css 3]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[drop shadow]]></category>
		<category><![CDATA[opacity]]></category>
		<category><![CDATA[rounded corners]]></category>

		<guid isPermaLink="false">http://net.tutsplus.com/?p=6639</guid>
		<description><![CDATA[<img src="http://nettuts.s3.amazonaws.com/430_cssTips/200x200.jpg" alt="11 Classic CSS Techniques Made Simple With CSS3" />]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve all had to achieve some effect that required an extra handful of divs or PNGs. We shouldn&#8217;t be limited to these old techniques when there&#8217;s a new age coming. This new age includes the use of CSS3. In today&#8217;s tutorial, I&#8217;ll show you eleven different time-consuming effects that can be achieved quite easily with CSS3.</p>
<p><span id="more-6639"></span></p>
<div class="tutorial_image">
<a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/demo.zip"><img src="http://nettuts.com/wp-content/themes/nettuts/site_images/button_src_nm.jpg"></a><br />
<a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/index.html"><img src="http://nettuts.com/wp-content/themes/nettuts/site_images/button_demo_nm.jpg"></a>
</div>
<h3>CSS3? What&#8217;s that?!</h3>
<p>I&#8217;m sure you&#8217;ve heard of CSS in general. CSS3 isn&#8217;t that much different, in terms of syntax; however, the power of CSS3 is much greater. As you&#8217;ll see in these eleven techniques, you can have multiple backgrounds, dynamically resize those backgrounds, border radiuses, text shadows, and more!</p>
<p>Here&#8217;s what the official (or at least, what I consider official) website of CSS3, <a href="http://www.css3.info/">css3.info</a>, has to say about CSS3:</p>
<blockquote><p>CSS3 is the new kid in the stylesheet family. It offers exciting new possibilities to create an impact with your designs, allows you to use more diverse style sheets for a variety of occasions and lots more.</p>
</blockquote>
<h3>What We&#8217;ll Be Covering</h3>
<p>Here are the 11 techniques that I&#8217;ll be showing you how to recreate with CSS3. I&#8217;ll show you how to create them using CSS2 (or JavaScript), and then with CSS3 properties. <em>Remember &#8211; these effects will only work in modern browsers that implement these CSS3 features. Your best option is to view these with Safari 4.</em></p>
<ol>
<li><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/corners.html">Rounded Corners</a></li>
<li><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/shadow.html">Box Shadow</a></li>
<li><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/textshadow.html">Text Shadow</a></li>
<li><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/font.html">Fancy Font</a></li>
<li><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/opacity.html">Opacity</a></li>
<li><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/rgba.html">RGBA</a></li>
<li><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/bgsize.html">Background Size</a></li>
<li><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/bgs.html">Multiple Backgrounds</a></li>
<li><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/columns.html">Columns</a></li>
<li><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/border.html">Border Image</a></li>
<li><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/animation.html">Animations</a></li>
</ol>
<h3>1. Rounded Corners</h3>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/430_cssTips/images/corners.jpg" alt="Rounded Corners" /></div>
<p>Probably my favorite on of this list, rounded corners provide a developer with so many options. You can create a rounded corner button in seconds. My favorite thing to do is set a gradient background to repeat along the x-axis and then apply rounded corners to make a very nice looking Web 2.0 button.</p>
<p>You can simulate rounded corners by using <strong>four extra divs</strong> or by using JavaScript. Though, a user doesn&#8217;t see these smooth corners if they have JavaScript disabled, I think that&#8217;s fine as long as the website still functions as it should. You can <a href="http://cssglobe.com/post/3714/css-sprites-rounded-corners/">read up</a> on this method if you would prefer to use pure CSS in making rounded corners.</p>
<h3>Classic Way</h3>
<pre name="code" class="html">
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery.corners.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(function(){
	$('.box').corners('10px');
});
&lt;/script&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;!--CONTENT--&gt;
&lt;/div&gt;
</pre>
<p>The classical method uses jQuery along with a JavaScript plugin called <a href="http://plugins.jquery.com/project/corners">Corners</a>.</p>
<h3>CSS3 Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.box {
border-radius: 10px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
}
&lt;/style&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;!--CONTENT--&gt;
&lt;/div&gt;
</pre>
<p>As you can see, all that you need to do is specify three CSS3 properties. Eventually, it will only be one; you have to use three now because different browsers use different property names.</p>
<p><strong><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/corners.html">View the demo.</a></strong></p>
<h3>2. Box Shadow</h3>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/430_cssTips/images/shadow.jpg" alt="Box Shadow" /></div>
<p>Box shadows provide you with a very powerful tool. 99% of the time when I&#8217;m desigining, I find myself using drop shadows for something. Once again, I&#8217;ll be using a jQuery plugin to take care of the classic way because honestly, trying to figure it out with just CSS is confusing. Why should I waste my time fiddling with negative margins when there&#8217;s already something written that does the job for me? As long as my design looks fine when people have JavaScript disabled, I&#8217;m perfectly content using a jQuery plugin.</p>
<h3>Classic Way</h3>
<pre name="code" class="html">
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery.dropShadow.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(function(){
	$('.box').dropShadow({
		left: 5,
		top: 5,
		opacity: 1.0,
		color: 'black'
	});
});
&lt;/script&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;!--CONTENT--&gt;
&lt;/div&gt;
</pre>
<p>I&#8217;m using the <a href="http://eyebulb.com/dropshadow/">dropShadow</a> plugin. Simple and what I wanted; though I would prefer to use just CSS <img src='http://net.tutsplus.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<h3>CSS3 Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.box {
box-shadow: 5px 5px 2px black;
-moz-box-shadow: 5px 5px 2px black;
-webkit-box-shadow: 5px 5px 2px black;
}
&lt;/style&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;!--CONTENT--&gt;
&lt;/div&gt;
</pre>
<p>The values for the box shadow properties are: x-offset y-offset blur color. Much easier than importing two JavaScript files, and A LOT easier than messing around with background images and negative margins.</p>
<p><strong><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/shadow.html">View the demo.</a></strong></p>
<h3>3. Text Shadow</h3>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/430_cssTips/images/textshadow.jpg" alt="Text Shadow" /></div>
<p>If you&#8217;ve ever read a tutorial about how to make the letterpress effect in Photoshop, you&#8217;ll know that the drop shadow effect is used. It&#8217;s really simple to create letterpressed text, just follow <a href="http://line25.com/tutorials/create-a-letterpress-effect-with-css-text-shadow">this tutorial at Line25</a> by Chris Spooner.</p>
<h3>Classic Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.font {
font-size: 20px;
color: #2178d9;
}
.fonts {
position: relative;
}
.fonts .font {
position: absolute;
z-index: 200;
}
.fonts .second {
top: 1px;
color: #000;
z-index: 100;
}
&lt;/style&gt;

&lt;div class=&quot;fonts&quot;&gt;
	&lt;span class=&quot;font&quot;&gt;The quick brown fox jumps over the lazy dog.&lt;/span&gt;
	&lt;span class=&quot;font second&quot;&gt;The quick brown fox jumps over the lazy dog.&lt;/span&gt;
&lt;/div&gt;
</pre>
<p>Instead of using a jQuery plugin this time, I just used some simple CSS tricks to absolutely position the text behind the other copy of text. The only bad thing about not using CSS3 for this situation is that you will get two copies of the text if CSS is disabled.</p>
<h3>CSS3 Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.font {
font-size: 20px;
color: #2178d9;
}
.font {
text-shadow: 0 1px 0 #000;
}
&lt;/style&gt;

&lt;span class=&quot;font&quot;&gt;The quick brown fox jumps over the lazy dog.&lt;/span&gt;
</pre>
<p>If you&#8217;re planning on using text shadows that are blurred (the third &#8220;option&#8221; in the text-shadow property), I don&#8217;t know how you would accomplish that with pure CSS, and no images.</p>
<p><strong><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/textshadow.html">View the demo.</a></strong></p>
<h3>4. Fancy Font</h3>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/430_cssTips/images/font.jpg" alt="Fancy Font" /></div>
<p>We&#8217;ve dreamed about it for a long time now, but you can finally display &#8220;fancy fonts&#8221; on the web <strong>without</strong> relying on JavaScript. Of course this causes some issues with allowing paid fonts to be distributed over the internet. Anyhow, I present you with @font-face.</p>
<h3>Classic Way</h3>
<pre name="code" class="html">
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/cufon.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/loyal.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(function(){
	Cufon.replace('.classic .font');
});
&lt;/script&gt;
&lt;style type=&quot;text/css&quot;&gt;
.font {
font-size: 20px;
}
&lt;/style&gt;

&lt;span class=&quot;font&quot;&gt;The quick brown fox jumps over the lazy dog.&lt;/span&gt;
</pre>
<p>I decided to use Cufón to replace the text. I&#8217;m not going to explain how to use it because Jeffrey <a href="http://net.tutsplus.com/videos/screencasts/the-easiest-way-to-use-any-font-you-wish/">has an awesome screencast</a> already.</p>
<h3>CSS3 Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
@font-face {
font-family: 'Loyal';
src: url('loyal.ttf');
}
.font {
font-family: Loyal;
font-size: 20px;
}
&lt;/style&gt;

&lt;span class=&quot;font&quot;&gt;The quick brown fox jumps over the lazy dog.&lt;/span&gt;
</pre>
<p>We create a font family with @font-face and then use it as a value for font-family. Michael Owens wrote <a href="http://net.tutsplus.com/tutorials/html-css-techniques/six-ways-to-improve-your-web-typography/">an article</a> here about a month ago which explains @font-face quite well.</p>
<p><strong><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/font.html">View the demo.</a></strong></p>
<h3>5. Opacity</h3>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/430_cssTips/images/opacity.jpg" alt="Opacity" /></div>
<p>If you&#8217;ve visited the <a href="http://envato.com/">Envato website</a> redesign lately, you might have noticed that there are a lot of transparent elements. Though this is achieved with transparent PNGs, you can achieve a similar effect by using the opacity property. Now, the opacity property has been around for a while, but our beloved IE has its own properties.</p>
<h3>Classic Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.box {
opacity: .6; // all modern browsers (this is CSS3)
-ms-filter: &quot;progid:DXImageTransform.Microsoft.Alpha(Opacity=50)&quot;; // IE 8
filter: alpha(opacity=60); // IE 5-7
}
&lt;/style&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;!--CONTENT--&gt;
&lt;/div&gt;
</pre>
<h3>CSS3 Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.box {
opacity: .6;
}
&lt;/style&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;!--CONTENT--&gt;
&lt;/div&gt;
</pre>
<p>With CSS3, we just eliminated two properties that were specific to IE, isn&#8217;t that great?!</p>
<p><strong><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/opacity.html">View the demo.</a></strong></p>
<h3>6. RGBA</h3>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/430_cssTips/images/rgba.jpg" alt="RGBA" /></div>
<p>Everyone knows what RGB stands for (red, green, blue), but what does the A stand for? It stands for alpha, which refers to the transparency.</p>
<p>Other than rounded corners, RGBA is my next most used CSS3 property. Sometimes I just want to add a few light white/black background to navigation links when a user hover overs them. It&#8217;s much easier than creating a new image for each color; however, with a little PHP it makes things a lot easier.</p>
<h3>Classic Way</h3>
<p>Seeing as how this isn&#8217;t a PHP article, I won&#8217;t be going over the PHP. Just save this file as rgba.php and when you need a certain RGBA color, make the background &#8220;color&#8221; as url(rgba.php?r=R&amp;g=G&amp;b=B&amp;a=A).</p>
<pre name="code" class="html">
&lt;?php
$image = imagecreatetruecolor(1, 1);
$r = (int)$_GET['r'];
$g = (int)$_GET['g'];
$b = (int)$_GET['b'];
$a = (float)$_GET['a'];
$white = imagecolorallocate($image, 255, 255, 255);
$color = imagecolorallocatealpha($image, $r, $g, $b, 127*(1-$a));
imagefill($image, 0, 0, $white);
imagefilledrectangle($image, 0, 0, 1, 1, $color);

header('Content-type: image/png');
imagepng($image);
?&gt;
</pre>
<p>Now just apply that to a div&#8230;</p>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.box {
background: url(rgba.php?r=239&amp;g=182&amp;b=29&amp;a=.25);
}
&lt;/style&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;!--CONTENT--&gt;
&lt;/div&gt;
</pre>
<h3>CSS3 Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.box {
background: rgba(239, 182, 29, .25);
}
&lt;/style&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;!--CONTENT--&gt;
&lt;/div&gt;
</pre>
<p><strong><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/rgba.html">View the demo.</a></strong></p>
<h3>7. Background Size</h3>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/430_cssTips/images/bgsize.jpg" alt="Background Size" /></div>
<p>The background size property is an amazing thing to have on your tool belt when you&#8217;re creating a liquid layout. An example of this could be when you have a background image for a container that is for the sidebar. The classic way of doing this would require tweaking to have the image repeat along the y-axis, but with CSS3 it&#8217;s like adding another background property.</p>
<h3>Classic Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.box {
position: relative;
overflow: hidden;
}
	.box img {
	position: absolute;
	top: 0;
	left: 0;
	width: 50%;
	height: 50%;
	z-index: 100;
	}
	.box .content {
	position: absolute;
	z-index: 200;
	}
&lt;/style&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;div class=&quot;content&quot;&gt;
		&lt;!--CONTENT--&gt;
	&lt;/div&gt;
	&lt;img src=&quot;http://nettuts.s3.amazonaws.com/423_screenr/200x200.jpg&quot; alt=&quot;&quot; /&gt;
&lt;/div&gt;
</pre>
<h3>CSS3 Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.box {
background: #ccc url(http://nettuts.s3.amazonaws.com/423_screenr/200x200.jpg) no-repeat;
-webkit-background-size: 50%; /* Safari */
-o-background-size: 50%; /* Opera */
-khtml-background-size: 50%; /* Konqueror */
}
&lt;/style&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;!--CONTENT--&gt;
&lt;/div&gt;
</pre>
<p>Unfortunately, this property isn&#8217;t implemented into Firefox (V3.5.2) at the time of this writing.</p>
<p><strong><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/bgsize.html">View the demo.</a></strong></p>
<h3>8. Multiple Backgrounds</h3>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/430_cssTips/images/bgs.jpg" alt="Multiple Backgrounds" /></div>
<p>Ah, multiple backgrounds. Now this give developers a very powerful tool. I can think of so many things that require multiple divs just to have more than one background. The most common place I can see this being helpful is in a header section of the website, but that&#8217;s just the first thing I thought of.</p>
<h3>Classic Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.box {
width: 200px;
height: 150px;
background: url(images/bg.png) repeat-x;
}
	.box2 {
	width: 100%;
	height: 100%;
	background: url(images/text.png) center center no-repeat;
	}
&lt;/style&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;div class=&quot;box2&quot;&gt;
		&lt;!--CONTENT--&gt;
	&lt;/div&gt;
&lt;/div&gt;
</pre>
<p>The classic method is pretty obvious, just wrap the div with another div and so on for each background you need. Produces lovely looking code, doesn&#8217;t it?</p>
<h3>CSS3 Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.box {
width: 200px;
height: 150px;
background: url(images/text.png) center center no-repeat, url(images/bg.png) repeat-x;
}
&lt;/style&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;!--CONTENT--&gt;
&lt;/div&gt;
</pre>
<p>The syntax is really easy to pick up on this one. All you do for the multiple backgrounds is add a comma in between each one! However, once again, this is a limited property and is only in Safari.</p>
<p><strong><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/bgs.html">View the demo.</a></strong></p>
<h3>9. Columns</h3>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/430_cssTips/images/columns.jpg" alt="Columns" /></div>
<p>This is the most interesting CSS3 property I&#8217;ve come by. It&#8217;s not something you see a lot in web design. Myself, I&#8217;ve only seen newspaper-like columns once or twice; however, I love how the effect looks when done correctly. Normally you would separate the content into divs and float those divs, but I found a <a href="http://welcome.totheinter.net/columnizer-jquery-plugin/">jQuery plugin</a> which dynamically renders the columns.</p>
<h3>Classic Way</h3>
<pre name="code" class="html">
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery.columnize.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(function(){
	$('.columns').columnize({
		columns: 2
	});
});
&lt;/script&gt;
&lt;style type=&quot;text/css&quot;&gt;
.column {
padding-right: 10px;
}
.column.last {
padding: 0;
}
&lt;/style&gt;

&lt;div class=&quot;columns&quot;&gt;
	&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elementum accumsan mi. Maecenas id dui a magna tempor pretium. Quisque id enim. Proin id tortor. Curabitur sit amet enim vitae quam pharetra imperdiet. Nulla diam ante, pellentesque eu, vestibulum non, adipiscing nec, eros. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis a nunc. Donec non dui a velit pulvinar gravida. Nunc rutrum libero vel tortor. Duis sed mi eu metus tincidunt ullamcorper. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In purus lorem, aliquam ac, congue ac, vestibulum quis, felis. Aliquam non sapien.&lt;/p&gt;
	&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elementum accumsan mi. Maecenas id dui a magna tempor pretium. Quisque id enim. Proin id tortor. Curabitur sit amet enim vitae quam pharetra imperdiet. Nulla diam ante, pellentesque eu, vestibulum non, adipiscing nec, eros. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis a nunc. Donec non dui a velit pulvinar gravida. Nunc rutrum libero vel tortor. Duis sed mi eu metus tincidunt ullamcorper. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In purus lorem, aliquam ac, congue ac, vestibulum quis, felis. Aliquam non sapien.&lt;/p&gt;
&lt;/div&gt;
</pre>
<p>I&#8217;ve added a little padding to the columns just so the text isn&#8217;t smashed up against each other.</p>
<h3>CSS3 Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.columns {
-moz-column-count: 2;
-webkit-column-count: 2;
}
&lt;/style&gt;

&lt;div class=&quot;columns&quot;&gt;
	&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elementum accumsan mi. Maecenas id dui a magna tempor pretium. Quisque id enim. Proin id tortor. Curabitur sit amet enim vitae quam pharetra imperdiet. Nulla diam ante, pellentesque eu, vestibulum non, adipiscing nec, eros. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis a nunc. Donec non dui a velit pulvinar gravida. Nunc rutrum libero vel tortor. Duis sed mi eu metus tincidunt ullamcorper. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In purus lorem, aliquam ac, congue ac, vestibulum quis, felis. Aliquam non sapien.&lt;/p&gt;
	&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elementum accumsan mi. Maecenas id dui a magna tempor pretium. Quisque id enim. Proin id tortor. Curabitur sit amet enim vitae quam pharetra imperdiet. Nulla diam ante, pellentesque eu, vestibulum non, adipiscing nec, eros. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis a nunc. Donec non dui a velit pulvinar gravida. Nunc rutrum libero vel tortor. Duis sed mi eu metus tincidunt ullamcorper. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In purus lorem, aliquam ac, congue ac, vestibulum quis, felis. Aliquam non sapien.&lt;/p&gt;
&lt;/div&gt;
</pre>
<p>There are a handful of other CSS3 column properties that you can apply, but for demonstrative purposes, I only specified the number of columns. If you&#8217;d like to learn more about these other properties, check out <a href="http://www.css3.info/preview/multi-column-layout/">the multi-column page at CSS3.info</a>.</p>
<p><strong><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/columns.html">View the demo.</a></strong></p>
<h3>10. Border Image</h3>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/430_cssTips/images/border.jpg" alt="Border Image" /></div>
<p>I had no clue about the border image property until I upgraded to Firefox 3.5 and went to <a href="http://www.blog.spoongraphics.co.uk/">Chris Spooner&#8217;s website</a> and saw that his post images had image borders. I personally don&#8217;t have any interest in this property, but that&#8217;s not going to stop me from explaining how to achieve it.</p>
<h3>Classic Way</h3>
<pre name="code" class="html">
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery.borderImage.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(function(){
	$('.classic .box').borderImage('url(images/border.png) 27 27 27 27 round round');
});
&lt;/script&gt;
&lt;style type=&quot;text/css&quot;&gt;
.box {
border-width: 20px;
}
&lt;/style&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;!--CONTENT--&gt;
&lt;/div&gt;
</pre>
<p>Instead of spending the time creating multiple divs and repeating the background image around the div, I found a jQuery plugin that does the work for me. It&#8217;s called <a href="http://plugins.jquery.com/project/jbi">borderImage</a> and works just like it should.</p>
<h3>CSS3 Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.box {
border-width: 20px;
-webkit-border-image: url(images/border.png) 27 round;
-moz-border-image: url(images/border.png) 27 round;
border-image: url(images/border.png) 27 round;
}
&lt;/style&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;!--CONTENT--&gt;
&lt;/div&gt;
</pre>
<p>As you can see, the border image property is a bit odd. <a href="http://www.w3.org/TR/css3-background/#the-border-image">W3</a> explains how it gets calculated <strong>MUCH</strong> better.</p>
<p><strong><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/border.html">View the demo.</a></strong></p>
<h3>11. Animations</h3>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/430_cssTips/images/animation.jpg" alt="Animations" /></div>
<p>Alright, who doesn&#8217;t love to see some element gently slide to the left or fade in when you hover over something? Animations are great to increase user interface, but make sure that you don&#8217;t go over board! The only browsers that support CSS3 animations are Webkit based browsers. The only other way to display animations is to use JavaScript. I&#8217;ll be using jQuery because it&#8217;s my favorite JavaScript library (if you haven&#8217;t guessed by now).</p>
<h3>Classic Way</h3>
<pre name="code" class="html">
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(function(){
	$('.box').hover(function(){
		$(this).stop().animate({
			top: '20px'
		}, 300);
	}, function(){
		$(this).stop().animate({
			top: '0'
		}, 300);
	});
});
&lt;/script&gt;
&lt;style type=&quot;text/css&quot;&gt;
.box {
position: relative;
}
&lt;/style&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;!--CONTENT--&gt;
&lt;/div&gt;
</pre>
<h3>CSS3 Way</h3>
<pre name="code" class="html">
&lt;style type=&quot;text/css&quot;&gt;
.box {
position: relative;
-webkit-transition: top 300ms linear;
}
.box:hover {
top: 20px;
}
&lt;/style&gt;

&lt;div class=&quot;box&quot;&gt;
	&lt;!--CONTENT--&gt;
&lt;/div&gt;
</pre>
<p>Both of these code snippets do the same thing: slide the div 20 pixels down over the course of 300 ms. Remember, the CSS3 code only works in Webkit browsers!</p>
<p><strong><a href="http://nettuts.s3.amazonaws.com/430_cssTips/demo/animation.html">View the demo.</a></strong></p>
<h3>Conclusion</h3>
<p>There you have it: 11 CSS techniques that will become much easier with CSS3 in the (hopefully near) future. Obviously, it&#8217;ll be a while before we can 100% depend on these properties across all browsers. </p>
<p>Don&#8217;t forget to review these other Nettuts+ tutorials that discuss CSS3:</p>
<ul>
<li><a href="http://net.tutsplus.com/videos/screencasts/a-crash-course-in-advanced-css3-effects/">A Crash-Course in Advanced CSS3 Effects</a></li>
<li><a href="http://net.tutsplus.com/tutorials/html-css-techniques/5-techniques-to-acquaint-you-with-css-3/">5 Techniques to Acquaint You With CSS 3</a></li>
</ul>
<ul class="webroundup">
<li>Follow us on <a href="http://www.twitter.com/nettuts">Twitter</a>, or subscribe to the <a href="http://feeds.feedburner.com/nettuts" title="Nettuts+ RSS Feed">Nettuts+ RSS Feed</a> for more daily web development tuts and articles.</li>
</ul>
<p>
<script type="text/javascript"><!--digg_url = "post permalink (not digg url)"; // -->
</script><br />
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://net.tutsplus.com/tutorials/html-css-techniques/11-classic-css-techniques-made-simple-with-css3/feed/</wfw:commentRss>
		<slash:comments>100</slash:comments>
		</item>
		<item>
		<title>20+ HTML Forms Best Practices for Beginners &#8211; Basix</title>
		<link>http://net.tutsplus.com/tutorials/html-css-techniques/20-html-forms-best-practices-for-beginners/</link>
		<comments>http://net.tutsplus.com/tutorials/html-css-techniques/20-html-forms-best-practices-for-beginners/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 16:10:06 +0000</pubDate>
		<dc:creator>Andrew Burgess</dc:creator>
				<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[basix]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[html]]></category>

		<guid isPermaLink="false">http://net.tutsplus.com/?p=6593</guid>
		<description><![CDATA[<img src="http://nettuts.s3.amazonaws.com/426_formsBestPractices/200x200.jpg" alt="preview" width="200" height="200"/>]]></description>
			<content:encoded><![CDATA[<p>Working with XHTML forms can be somewhat daunting; they not only use some niche HTML elements, but also blur the line between static content and user interaction. Let&#8217;s review some things to remember when creating your next form.</p>
<p><span id="more-6593"></span></p>
<p>Good HTML forms require attention on at least four points:</p>
<ol>
<li>Semantics</li>
<li>Accessibility</li>
<li>Functionality</li>
<li>Design</li>
</ol>
<p>Forms can be difficult and sometimes even annoying for users;<br />
often, a form interrupts a user&#8217;s main focus and direction on a page:<br />
they&#8217;re intent on purchasing that gift, or trying out your new web app,<br />
not giving you their shipping address or coming up with yet another password.<br />
These tips will make forms easier for you as a developer/designer, and<br />
them as a user.</p>
<h3>Semantics</h3>
<h4>1 : Use fieldsets to encapsulate similar fields</h4>
<p>Generally, forms are made up of inputs inside form tags. When you&#8217;ve<br />
got a lot of fields that the user must fill out, it can be easier for<br />
both the user and you, the developer, to keep track of input by using<br />
fieldsets. The perennial example of this is using fieldsets to separate<br />
 a billing address and a shipping address.</p>
<pre name="code" class="html">
&lt;fieldset>
    &lt;span>Billing Address&lt;/span>&lt;input type="text" />
    &lt;span>City&lt;/span>&lt;input type="text" />
    &lt;span>Province&lt;/span>&lt;input type="text" />
    &lt;span>Postal Code&lt;/span>&lt;input type="text" />
&lt;/fieldset>

&lt;fieldset>
    &lt;span>Shipping Address&lt;/span>&lt;input type="text" />
    &lt;span>City&lt;/span>&lt;input type="text" />
    &lt;span>Province&lt;/span>&lt;input type="text" />
    &lt;span>Postal Code&lt;/span>&lt;input type="text" />
&lt;/fieldset>
</pre>
<h4>2 : Label Fieldsets with Legends</h4>
<p>It hardly makes sense to use a fieldset without giving it a clear name.<br />
We can improve the code above by using the legend element to title our<br />
fieldsets. The fieldset element has a border by default, and the legend<br />
will lay itself over that border.</p>
<pre name="code" class="html">
&lt;fieldset>
    &lt;legend>Billing Address&lt;/legend>
    &lt;span>Address&lt;/span>&lt;input type="text" />
    &lt;span>City&lt;/span>&lt;input type="text" />
    &lt;span>Province&lt;/span>&lt;input type="text" />
    &lt;span>Postal Code&lt;/span>&lt;input type="text" />
&lt;/fieldset>
</pre>
<p>This results in the following:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/426_formsBestPractices/legend.png" alt="legend" /></div>
<h4>3 : Name your Inputs</h4>
<p>If you want to pass form data to a script, each input element needs<br />
to have a name; if you are using PHP, these names will become the keys<br />
to a super global array, usually $_POST or $_GET. </p>
<pre name="code" class="html">
&lt;fieldset>
    &lt;span>Billing Address&lt;/span>&lt;input type="text" name="billAddress" />
    &lt;span>City&lt;/span>&lt;input type="text" name="billCity" />
    &lt;span>Province&lt;/span>&lt;input type="text" name="billProvince" />
    &lt;span>Postal Code&lt;/span>&lt;input type="text" name="billPC" />
&lt;/fieldset>
</pre>
<h4>4 : Use the Label Element</h4>
<p>Let&#8217;s continue improving that code; there&#8217;s nothing inherently wrong<br />
with using a span to label the inputs, but the label tag is a born match for inputs.</p>
<pre name="code" class="html">
&lt;fieldset>
    &lt;legend>Billing Affress&lt;/legend>
    &lt;label>Address&lt;/label>&lt;input type="text" name="billAddress" />
    &lt;label>City&lt;/label>&lt;input type="text" name="billCity" />
    &lt;label>Province&lt;/label>&lt;input type="text" name="billProvince" />
    &lt;label>Postal Code&lt;/label>&lt;input type="text" name="billPC" />
&lt;/fieldset>
</pre>
<h4>5 : Give Labels the For Attribute</h4>
<p>I really like the &#8216;for&#8217; attribute; it provides a way to bind a label to an<br />
input. The value of &#8216;for&#8217; should be the same as the id of the input you want<br />
to bind it to. </p>
<pre name="code" class="html">
&lt;fieldset>
    &lt;legend>Billing Affress&lt;/legend>
    &lt;label for="billAddress">Address&lt;/label>&lt;input type="text" id="billAddress" name="billAddress" />
    &lt;label for="billCity">City&lt;/label>&lt;input type="text" id="billCity" name="billCity" />
    &lt;label for="billProvince">Province&lt;/label>&lt;input type="text" id="billProvince" name="billProvince" />
    &lt;label for="billPC" >Postal Code&lt;/label>&lt;input type="text" id="billPC" name="billPC" />
&lt;/fieldset>
</pre>
<p>At first, this is one of those things that only seem to affect your<br />
code&#8217;s quality, but they do a special job in the visible content: when<br />
the for attribute is defined, the label becomes a &#8216;clickable&#8217; area that will<br />
 focus the input. For example, clicking the label of a text input will focus<br />
 your cursor in the box; clicking the label of a checkbox will check (or uncheck)<br />
 the box. </p>
<h4>6 : Use optgroup to categorize options</h4>
<p>If you have a lot of options in a select, it&#8217;s usually better to group<br />
them into optgroups. &lt;optgroup&gt; is a little-known element that will indent<br />
options and give them a title. Note that the label attribute is required.</p>
<pre name="code" class="html">
&lt;select>
&lt;optgroup label="USA">
    &lt;option>Alabama&lt;/option>
    &lt;option>Alaska&lt;/option>
    &lt;option>Arizona&lt;/option>
&lt;/optgroup>
&lt;optgroup label="Canada">
    &lt;option>Alberta&lt;/option>
    &lt;option>British Columbia&lt;/option>
    &lt;option>Manitoba&lt;/option>
&lt;/optgroup>
&lt;/select>
</pre>
<p>This gives us the following results:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/426_formsBestPractices/select.png" alt="select" /></div>
<h4>7 : Always assign complete attributes</h4>
<p>When working with forms particularly, it&#8217;s tempting to write like this:</p>
<pre name="code" class="html">
&lt;label for="live">Living?&lt;/label>
&lt;input name="live" id="live" type="checkbox" checked disabled />
</pre>
<p>Yes, this does what it&#8217;s supposed to do. No, you shouldn&#8217;t code like this!<br />
It isn&#8217;t standards-compliant. Whenever you are adding attributes to an element,<br />
don&#8217;t cut corners. </p>
<pre name="code" class="html">
&lt;label for="live">Living?&lt;/label>
&lt;input name="live" id="live" type="checkbox" checked="checked" disabled="disabled" />
</pre>
<h4>8 : Consider using Buttons instead of Submit Inputs</h4>
<p>Generally, &lt;input type=&#8221;submit&#8221; /> has been the universal submit<br />
button. But HTML has a &lt;button> element. Why would you use it? Well, it&#8217;s<br />
generally easier to style buttons; also, you can put images within a button,<br />
so it really offers more flexibility. You can read more in<br />
<a href="http://particletree.com/features/rediscovering-the-button-element/">these</a><br />
<a href="http://trevordavis.net/blog/tutorial/input-vs-button/">two</a> articles.</p>
<h3>Accessibility</h3>
<h4>9 : Put tabindices on your inputs</h4>
<p>It&#8217;s definitely easier to tab through a form than it is to use your mouse . . .<br />
however, by default, your user will tab through in the order they are written in<br />
the HTML. If this isn&#8217;t the order you want them to go through the  inputs, you can<br />
easily add the tabindex property to your inputs; tabindex takes a number value, and will<br />
hop to the input with the next highest value when you hit that tab key.</p>
<pre name="code" class="html">
&lt;input type="text" tabindex="2" />
&lt;input type="text" tabindex="1" />
&lt;input type="text" tabindex="3" />
</pre>
<h4>10 : Define accesskey when appropriate</h4>
<p>The accesskey attribute creates a keyboard shortcut that will focus that<br />
input: the shortcut is Alt (Option) + the accesskey value. Obviously, you wouldn&#8217;t<br />
put an accesskey on every input, but it would certainly be useful on, for example, a search box.<br />
Don&#8217;t forget to let users know about the shortcut; often this is done by underlining<br />
the letter, as it&#8217;s usually part of the label. </p>
<pre name="code" class="html">
&lt;label for="search">&lt;span class="shortcut">S&lt;/span>earch&lt;/label>
&lt;input type="text" name="s" id="search" accesskey="s" />
</pre>
<h4>11 : Use good focusing techniques</h4>
<p>You could argue that this point is as much on the side of design as it is accessibility.<br />
It&#8217;s always nice if a form field (usually a text box, in this case) changes colour when it&#8217;s focused, but for the visually<br />
impaired, it&#8217;s almost a requirement if they are to use the form successfully.<br />
To this end, you can use the hover psuedoclass in your CSS; this will work in all<br />
common browsers except IE7 and down. You can also use JavaScript for this;<br />
<a href="http://docs.jquery.com/Events/focus">jQuery has a hover<br />
event</a>.</p>
<pre name="code" class="css">
input[type=text]:hover {
    	background-color:#ffff66;
    	border-color:#999999;
}
</pre>
<h4>12 : Consider people using Screen Readers</h4>
<p>Since forms have the tendency to be so tedious, everyone likes a well-designed form.<br />
But don&#8217;t let a fancy form ignore screen readers: always make sure your inputs are<br />
clearly labeled. If you don&#8217;t want those labels to show (maybe you are labeling text<br />
inputs with values that disappear on focus), you can remove them from the visual presentation<br />
 (don&#8217;t use display: none, though; <a href="http://webaim.org/blog/hiding-content-for-screen-readers/"><br />
 there are better ways<a/>). Also, screen readers generally associate the text directly<br />
 before an input to be the label for the input. The exceptions to this are radio<br />
 buttons and checkboxes. </p>
<h3>Functionality</h3>
<h4>13 : Use the right Content Type</h4>
<p>In most cases you won&#8217;t need to put the enctype attribute on your form<br />
tag; it will default to &#8220;application/x-www-form-urlencoded.&#8221; However,<br />
when you have a file input, which will allow the user to upload the file,<br />
you need to use &#8220;multipart/form-data.&#8221;</p>
<pre name="code" class="html">
&lt;form action="verify.php" method="get" enctype="multipart/form-data">
    &tl;label for="avatar">Upload your Avatar : &lt;/label>
    &lt;input type="file" name="avatar" id="avatar" />
&lt;/form>
</pre>
<h4>14 : Know when to use Get and when to use Post</h4>
<p>A form can send its data by two methods: get and post; you define<br />
one in the method attribute on the form tag. What&#8217;s the difference,<br />
and when should you use them? Ignoring what goes on at the server,<br />
the main difference is the way the browser sends the information.<br />
With get, the form data is send as a query, visible in the url. So<br />
this form . . . </p>
<pre name="code" class="html">
&lt;form action="you.php" method="get">
    &tl;label for="fname">First Name&lt;/label>
    &lt;input type="text" name="fname" id="fname" value="Bill" />
    &tl;label for="lname">Last Name&lt;/label>
    &lt;input type="text" name="lname" id="lname" value="Gates" />
&lt;/form>
</pre>
<p>. . . would result in this URL when submitted: http://www.example.com/you.php?fname=Bill&amp;lname=Gates</p>
<p>When you use post, the data is sent in the HTTP request header.<br />
That way, it&#8217;s not visible to the average user. So which should you<br />
use when? Post is better for sensitive data (like passwords) and any<br />
data that will generally change something (e.g. add a record to<br />
the database). Also, post is your only option if you&#8217;re uploading<br />
a file. Get is great for querying the database, and other requests<br />
that don&#8217;t have a lasting affect on anything (&#8221;idempotent&#8221; the spec<br />
calls it). Really, I&#8217;ve just scratched the surface on the differences<br />
here: there are <a href="http://www.cs.tut.fi/~jkorpela/forms/methods.html">other</a><br />
<a href="http://www.diffen.com/difference/Get_vs_Post">articles</a> that go into this in-depth.</p>
<h4>15 : Validate on both the Client and Server</h4>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/426_formsBestPractices/validate.png" alt="validate" /></div>
<p>Validation is the bane of forms. But it&#8217;s best to check the input both on<br />
the client and on the server; validating in the browser allows you to warn<br />
the user of mistakes before they submit the form, which requires one less<br />
transaction with the server.  However, always be sure to validate on the server<br />
as well, for security&#8217;s sake. </p>
<h4>16 : Give your Users Smart Warnings</h4>
<p>This goes hand in hand with the previous best practice. Too many times I&#8217;ve<br />
submitted a form only to be told &#8220;Fields were not filled correctly.&#8221; Can you spell<br />
vague? Once you&#8217;ve determined that your user has made a mistake, let them know as<br />
soon and as clearly as possible. Put your error messages close to the bad field,<br />
and let your user know what&#8217;s wrong with their entry. I like using the jQuery&#8217;s blur()<br />
event for this: as soon a user hops to the next box, the previous one is validated. </p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/426_formsBestPractices/errors.png" alt="errors" /></div>
<h4>17 : Consider using AJAX to submit</h4>
<p>Many times, submiting a form results in a simple message: &#8220;Thank you,&#8221; &#8220;Check your<br />
email for confirmation,&#8221; or &#8220;We&#8217;ll get back to you when we can.&#8221; When that&#8217;s the case,<br />
what better place to use AJAX? You could just fade out the form, send the data with<br />
jQuery or (YOUR FAVOURITE LIBRARY), and fade in your message. </p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/426_formsBestPractices/ajax.png" alt="ajax" /></div>
<h4>18 : Make sure your form works without Javascript</h4>
<p>Maybe this should have gone under accessibility; although the last couple of tips need<br />
JavaScript to work at all, make sure your  form is completely functional without them. This means a regular<br />
form submit, server-side validation, and good errors after a page reload. </p>
<h3>Design</h3>
<h4>19 : Style Forms Consistently</h4>
<p>I&#8217;m no designer, and I don&#8217;t pretend to be, but I do know this much: don&#8217;t fling your<br />
form fields carelessly around. Your form should be consistent in its styling. Decide whether<br />
your labels will be to the left or right (or perhaps above or below) of the fields, and stick<br />
with it. Make all your text inputs and textareas the same width. Space all your fields equally.<br />
Keep at least one edge of all boxes aligned. The tuts+ comment forms are great examples of well-styled forms.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/426_formsBestPractices/comments.png" alt="comments" /></div>
<h4>20 : Consider using JavaScript to Consistently Style Forms over Different Platforms</h4>
<p>With upteen browsers/operating system combinations, form element consistency is hardly<br />
possible . . . without the help of a bit of JavaScript. If you want your forms<br />
 to look the same on almost every browser, check out the<br />
<a href="http://www.dfc-e.com/metiers/multimedia/opensource/jqtransform/">jqTransform jQuery plugin</a>,<br />
a plugin aimed directly at this compatibility problem. Simply include it, call it, and adjust the included css file<br />
to your taste; it works with IE6+, Safari 2+, Firefox 2+, and Chrome. </p>
<h4>21 : Be inspired by others</h4>
<p>If you&#8217;re having trouble coming up with that unique form design for your site, go for a<br />
little inspiration! Smashing Magazine has a<br />
<a href="http://www.smashingmagazine.com/2008/04/17/web-form-design-modern-solutions-and-creative-ideas/">great roundup of forms</a>,<br />
and Smileycat&#8217;s &#8220;Elements of Design&#8221; Gallery has a bunch of<br />
<a href="http://www.smileycat.com/design_elements/blog_comment_forms/">Blog Comment Forms</a> worth checking out.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/426_formsBestPractices/smashmag.jpg" /></div>
<h3>Conclusion</h3>
<h4>22: Look forward to HTML 5 Forms</h4>
<p>HTML 5 has some great features for web forms. Two of the most exciting ones are new types<br />
for inputs (like url, email, and date) and the datalist element, which can be used for easy<br />
autocomplete. When these and other parts of the spec get implemented, dynamic forms will be<br />
much easier!</p>
<p>Website forms can be challenging, but I hope these tips will help you make your forms stand<br />
out from the rest. Have a good tip that I didn&#8217;t mention? Let&#8217;s hear it in the comments!</p>
<h3>You Also Might Like</h3>
<ul>
<li><a href="http://net.tutsplus.com/tutorials/php/30-php-best-practices-for-beginners/">30+ PHP Best Practices for Beginners</a></li>
<li><a href="http://net.tutsplus.com/tutorials/javascript-ajax/24-javascript-best-practices-for-beginners/">24 JavaScript Best Practices for Beginners</a></li>
<li><a href="http://net.tutsplus.com/tutorials/html-css-techniques/30-html-best-practices-for-beginners/">30 HTML Best Practices for Beginners</a></li>
</ul>
<ul class="webroundup">
<li>Follow us on <a href="http://www.twitter.com/nettuts">Twitter</a>, or subscribe to the <a href="http://feeds.feedburner.com/nettuts" title="Nettuts+ RSS Feed">Nettuts+ RSS Feed</a> for more daily web development tuts and articles.</li>
</ul>
<p>
<script type="text/javascript"><!--digg_url = "post permalink (not digg url)"; // -->
</script><br />
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://net.tutsplus.com/tutorials/html-css-techniques/20-html-forms-best-practices-for-beginners/feed/</wfw:commentRss>
		<slash:comments>88</slash:comments>
		</item>
		<item>
		<title>How to Design and Code a Flexible Website</title>
		<link>http://net.tutsplus.com/tutorials/html-css-techniques/how-to-design-and-code-a-flexible-website/</link>
		<comments>http://net.tutsplus.com/tutorials/html-css-techniques/how-to-design-and-code-a-flexible-website/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 16:18:05 +0000</pubDate>
		<dc:creator>Tessa Thornton</dc:creator>
				<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[design a website]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://net.tutsplus.com/?p=6526</guid>
		<description><![CDATA[<img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/preview.jpg" alt="How to Design and Code a Flexible Website" width="200" height="200"/>]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, we&#8217;re going to be designing and coding a simple blog-style web-page. We&#8217;ll pay special attention to making our design flexible and accessible by using clean and simple XHTML and CSS. This tutorial is aimed at beginners, and anyone looking to improve the accessibility of their web designs.</p>
<p><span id="more-6526"></span></p>
<div class="tutorial_image">
<a href="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/source.zip"><img src="http://nettuts.com/wp-content/themes/nettuts/site_images/button_src_nm.jpg"></a><br />
<a href="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/source/index.html"><img src="http://nettuts.com/wp-content/themes/nettuts/site_images/button_demo_nm.jpg"></a>
</div>
<h3>Tutorial Details</h3>
<ul>
<li><b>Difficulty:</b> Easy</li>
<li><b>Estimated Completion Time:</b> 1.5-2hrs </li>
</ul>
<h3>Step 1</h3>
<p>So our example web page is going to be based on a simple blog theme, with a WordPress blog-like structure similar to that of the nettuts homepage. It&#8217;s going to look something like this: </p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/1.jpg" border="0" /></div>
<p>The idea here is not for you to reproduce my example, but for you to be able to follow along and apply the techniques to your own designs, hopefully learning a thing or two about the underlying concepts. </p>
<h3>Step 2 &#8211; Photoshop</h3>
<p>We&#8217;re going to keep the Photoshop use here to a bare minimum, Usually I mockup an entire design in Photoshop before coding, but here I&#8217;m just going to generate a basic layout, and worry about the content later.  This is an example of a different workflow, it will make more sense as we go along. </p>
<p><strong>Note:</strong> Some readers have asked about using the GIMP. I haven&#8217;t used it personally, but I&#8217;m sure you can accomplish the following steps in GIMP just as easily, because all we&#8217;re using is basic shapes and gradients. </p>
<h4>Page Layout</h4>
<p>I decided to make the page 900px wide, so my document is 1000px wide and 1200px long (don&#8217;t know why I gave myself so little room, make yours wider if you like). Place guides at 50px and 950px to get a 900px wide area. We&#8217;re going to have a content area and a sidebar, and the content area is going to be 600px wide, so place another guide at 650px. </p>
<h4>Backgrounds</h4>
<p>The header background is just three rectangles for the top links, main header, and navigation area. I used shape layers and added gradients to the layer styles. There are also 1px borders between the top bar and header area, and between the header and navigation area.</p>
<p>The footer background is another gradient, but this time with a 2px border at the top. </p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/2.jpg" alt="2" /></div>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/3.jpg" alt="3" /></div>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/4.jpg" alt="4" /></div>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/5.jpg" alt="5" /></div>
<p>Next add a background for the sidebar, I chose #d8ddd1. </p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/6.jpg" alt="6" /></div>
<h4>Type Tool</h4>
<p>Next we Grab the type tool (T) and add in a logo and tagline, and some basic navigation links. Fonts: </p>
<p>Blog Title:</p>
<ul>
<li>Font: Myriad Pro</li>
<li>Size: 48pt</li>
<li>Color: #ffffff (white)</li>
</ul>
<p>Blog Description:</p>
<ul>
<li>Font: Myriad Pro</li>
<li>Size: 24pt</li>
<li>Color: #ffffff</li>
</ul>
<p>Main Navigation Links:</p>
<ul>
<li>Font: Arial</li>
<li>Size: 18pt</li>
<li>Color: #2b2b2b</li>
</ul>
<p>&#8220;welcome, guest&#8221; and &#8220;stay updated&#8221;:</p>
<ul>
<li>Font: Arial</li>
<li>Size: 12pt</li>
<li>Color: #fffff</li>
</ul>
<p>&#8220;login, Sign Up&#8221; and &#8220;subscribe via&#8230;&#8221;:</p>
<ul>
<li>Color: #a5bf8d</li>
<li>Style: underline</li>
</ul>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/7.jpg" alt="7" /></div>
<h4>Content</h4>
<p>We&#8217;re only going to include one sample &#8220;post&#8221; in our Photoshop mockup, because I find working with type in Photoshop is a real pain, but we&#8217;ll get into more detail about styling the content section later.  The fonts I&#8217;m using for the dummy post are: </p>
<p>Post Title:</p>
<ul>
<li>Font: Arial</li>
<li>Size: 24pt</li>
<li>Color: #3c3f40</li>
<li>Style: Bold</li>
</ul>
<p>Date, category and author info:</p>
<ul>
<li>Size: 11pt</li>
</ul>
<p>Paragraphs:</p>
<ul>
<li>Size: 12pt</li>
</ul>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/8.jpg" alt="8" /></div>
<p>Okay, we&#8217;re pretty much done with our mockup. All we need to do now is slice it up and save for the web. </p>
<p>Select the slice tool (C) and cut out skinny slices of each of the background rectangles: the top bar, the header area, the navigation, and the footer. Don&#8217;t include the borders, we&#8217;re going to add those in with CSS. Try zooming in really close to make sure you get the right parts. The slices I cut are about 5px wide, but the width isn&#8217;t terribly important at this point. </p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/9.jpg" alt="9" /></div>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/10.jpg" alt="10" /></div>
<p>Select &#8216;File/Save for web and devices&#8230;&#8217; Hold down &#8220;shift&#8221; and click to select each slice. From the dropdown menu &#8220;presets&#8221; select &#8220;jpeg . Uncheck &#8220;convert to sRGB&#8221;" (I find that the conversion dulls the colors). All other settings should be left at defaults. Click &#8220;Save.&#8221;  In the popup window, make sure &#8220;selected slices&#8221; is displayed in the &#8220;slices&#8221; dropdown menu, and click save. </p>
<p>GIMP users: I&#8217;m not sure if gimp has a tool like slice, but you just need to make rectangular selections, save them to separate documents, crop them down, and save them as optimized jpegs. </p>
<p>For a more in depth look at the slicing and saving process, see <a href="http://net.tutsplus.com/tutorials/html-css-techniques/design-and-code-your-first-website-in-easy-to-understand-steps/">My previous tutorial</a>. </p>
<h3>Step 3 &#8211; HTML Setup</h3>
<p>If you&#8217;re unfamiliar with the process of organizing files and folders for a webpage, again, see my other tutorial linked above. </p>
<p>Open up your favorite code editor, and create a new file called index.html.<br />
We&#8217;re going to try to use as few div tags as possible to keep our markup meaningful and semantic. That being said, to maintain a flexible, resizable layout, we need to enclose some elements in multiple divs. More on that later.
</p>
<p>All elements in our page will be contained within two divs, called &#8220;main&#8221; and &#8220;footer&#8221;. Within the &#8220;main&#8221; div, we&#8217;re going to have a divs for the top bar, the header, and the content area. The footer is going to contain an inner div for the written content. </p>
<pre name="code" class="html">
	&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	&lt;head>
		&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		&lt;title>My Blog&lt;/title>
	&lt;/head>
	&lt;body>

		&lt;div id="main">

			&lt;div id="top_bar">
			&lt;/div>&lt;!-- end top bar -->

			&lt;div id="header">
			&lt;/div>&lt;!-- end header -->

			&lt;div id="content">
			&lt;/div>&lt;!-- end content -->

		&lt;/div>&lt;!-- end main -->

		&lt;div id="footer">

			&lt;div id="footer_content">
			&lt;/div>&lt;!-- end footer content -->

		&lt;/div>&lt;!-- end footer -->
	&lt;/body>
&lt;/html>
</pre>
<h4>Top Bar</h4>
<p>The background of the blue bar at the top stretches the entire width of the page, but the two text areas need to be within the 900px of the page. To achieve this, we need the content to be contained within another div, which will have a class of &#8220;container&#8221;.<br />
Within our bar at the top, we&#8217;re going to have two paragraphs, one for the login, one for the subscription options. Since they&#8217;re going to be floated, each needs to be given a unique id. Actually, if we wanted to be totally semantic, we could code these two paragraphs as an unordered list with two list items. Try it both ways, see if you can make it work.
</p>
<pre name="code" class="html">
&lt;div id="main">

	&lt;div id="top_bar">
		&lt;div class="container">
			&lt;p id="login">Welcome, Guest &lt;a href="#">Login&lt;/a> &lt;a href="#">Sign Up&lt;/a>&lt;/p>
			&lt;p id="subscribe"> Stay Updated: &lt;a href="#">Subscribe via RSS&lt;/a> &lt;a href="#">Email Updates&lt;/a>&lt;/p>
		&lt;/div>&lt;!-- end bar container -->
	&lt;/div>&lt;!-- end top bar -->

	&lt;div id="header">
		&lt;/div>&lt;!-- end header -->

	&lt;div id="content">
	&lt;/div>&lt;!-- end content -->

&lt;/div>&lt;!-- end main -->
</pre>
<h3>Step 5 &#8211; Header</h3>
<p>We face the same issue here as we did with the last step, the background image needs to stretch out indefinitely. To contain the content, we need to place it within another div. Since the header will also be within our centered, 900px wide page, we can re-use the &#8220;container&#8221; class. The title of the blog will be wrapped in an &lt;h1> tag, and the description/tagline will be a paragraph with a unique id. </p>
<pre name="code" class="html">

&lt;div id="header">
	&lt;div id="branding" class="container">
		&lt;h1>My Blog&lt;/h1>
		&lt;p id="desc">Description of My Blog&lt;/p>
	&lt;/div>&lt;!-- end branding -->
&lt;/div>&lt;!-- end header -->
</pre>
<h4>Navigation</h4>
<p>Also inside the header is the navigation menu, which will be wrapped in an unordered list with the id of &#8220;menu&#8221;, which will all be wrapped within another div with the id of &#8220;navigation&#8221;.<br />
Because we want the navigation menu to be centered, we can add our &#8220;container&#8221; class to the ul as well.</p>
<pre name="code" class="html">
&lt;div id="header">

	&lt;div id="branding" class="container">
		&lt;h1>My Blog&lt;/h1>
		&lt;p class="desc">Description of My Blog&lt;/p>
	&lt;/div>&lt;!-- end branding -->

	&lt;div id="navigation">
		&lt;ul id="menu" class="container">
			&lt;li>&lt;a href="#">Home&lt;/a>&lt;/li>
			&lt;li>&lt;a href="#">About&lt;/a>&lt;/li>
			&lt;li>&lt;a href="#">Blog&lt;/a>&lt;/li>
			&lt;li>&lt;a href="#">Contact&lt;/a>&lt;/li>
		&lt;/ul>
	&lt;/div>&lt;!-- end navigation -->

&lt;/div>&lt;!-- end header -->
</pre>
<p>We&#8217;re writing the navigation links in lowercase here, but in our CSS file we will transform them to uppercase. You could write them in capitals here, but my caps lock key is broken, and this way makes for cleaner markup.</p>
<h3>Step 6 &#8211; Content</h3>
<p>The content area doesn&#8217;t have a background image or color, so we don&#8217;t need to enclose it in an additional div. To center it, we can give the content div a class of &#8220;container&#8221; as well. Inside the content div, we have two more divs, one for the blog posts, and one for the sidebar. </p>
<p>When designing a blog, we need to take into account the fact that the content is going to change, and may include any number of elements, including lists, images, quotes, headings and emphasized text. To prepare, we need to style every possible element that might appear., so our sample content needs to include <em>Everything</em>. This is sometimes called the &#8220;Sandbox method.&#8221;  To get dummy content, I suggest visiting <a href="http://html-ipsum.com/">HTML Ipsum</a>.</p>
<p>We&#8217;re going to separate our sample content into a couple different posts, with titles wrapped in &lt;h2> tags. Each post will also have standard information about date, author, etc, contained within a &lt;small> tag. </p>
<pre name="code" class="html">
&lt;div id="content" class="container">
	&lt;div id="posts">
		&lt;h2>Don't Cancel Chuck!&lt;/h2>
		&lt;small>on July 01 in &lt;a href="#">General&lt;/a> tagged &lt;a href="#">petitions&lt;/a> by &lt;a href="#">admin&lt;/a>&lt;/small>
		&lt;p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
		Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
		Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
		Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
		&lt;/p>&lt;blockquote>&lt;p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue.
		Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus,
		at luctus turpis elit sit amet quam. Vivamus pretium ornare est.&lt;/p>&lt;/blockquote>
		&lt;p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
		&lt;a href="#">Read More&lt;/a>&lt;/p>

		&lt;h2>Alien Spacecraft found in New Mexico&lt;/h2>
		&lt;small>on July 01 in &lt;a href="#">General&lt;/a> tagged &lt;a href="#">petitions&lt;/a> by &lt;a href="#">admin&lt;/a>&lt;/small>
		&lt;p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
		Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper.
		Aenean ultricies mi vitae est. Mauris placerat eleifend leo.&lt;/p>
		&lt;ul>
   			&lt;li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.&lt;/li>
  			&lt;li>Aliquam tincidunt mauris eu risus.&lt;/li>
 			&lt;li>Vestibulum auctor dapibus neque.&lt;/li>
		&lt;/ul>
		&lt;p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
		Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p>

		&lt;h2>Ghostly Goo in your Kitchen Sink?&lt;/h2>
	    &lt;small>on July 01 in &lt;a href="#">General&lt;/a> tagged &lt;a href="#">petitions&lt;/a> by &lt;a href="#">admin&lt;/a>&lt;/small>
	 	&lt;p>&lt;strong>Pellentesque habitant morbi tristique&lt;/strong> senectus et netus et malesuada fames ac turpis egestas.
	    Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper.
	    &lt;em>Aenean ultricies mi vitae est.&lt;/em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, &lt;code>commodo vitae&lt;/code>,
	    ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. &lt;a href="#">Donec non enim&lt;/a> in turpis pulvinar facilisis. Ut felis.&lt;/p>
		&lt;h4>Header Level 4&lt;/h4>
	    &lt;ol>
   			&lt;li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.&lt;/li>
   			&lt;li>Aliquam tincidunt mauris eu risus.&lt;/li>
		&lt;/ol>
		&lt;blockquote>&lt;p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue.
		Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus,
		at luctus turpis elit sit amet quam. Vivamus pretium ornare est.&lt;/p>&lt;/blockquote>
		&lt;h3>Header Level 3&lt;/h3>
		&lt;ul>
			&lt;li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.&lt;/li>
   			&lt;li>Aliquam tincidunt mauris eu risus.&lt;/li>
		&lt;/ul>
		&lt;ol>
			&lt;li>Lorem ipsum dolor sit amet&lt;/li>
			&lt;li>Consectature vestiblum&lt;/li>
		&lt;/ol>

	&lt;/div>&lt;!-- end posts -->

&lt;/div>&lt;!-- end content -->
</pre>
<h3>Step 7 &#8211; Sidebar</h3>
<p>In a typical blog, the sidebar holds various widgets, which display links to other pages or articles, and each widget is usually an unordered list of anchor tags. Here, we&#8217;re going to have &#8220;categories&#8221;, &#8220;recent posts&#8221; and &#8220;archives&#8221; widgets. So our sidebar div will contain three lists, each with a title wrapped in an &lt;h3> tag. </p>
<pre name="code" class="html">
&lt;div id="sidebar">

	&lt;h3>Categories&lt;/h3>
    &lt;ul>
   		&lt;li>&lt;a href="#">General&lt;/a>&lt;/li>
    	&lt;li>&lt;a href="#">Ghost Sightings&lt;/a>&lt;/li>
    	&lt;li>&lt;a href="#">UFO Crashes&lt;/a>&lt;/li>
    	&lt;li>&lt;a href="#">Government Coverups&lt;/a>&lt;/li>
    	&lt;li>&lt;a href="#">International Conspiracies&lt;/a>&lt;/li>
    &lt;/ul>

    &lt;h3>Recent Posts&lt;/h3>
	&lt;ul>
		&lt;li>&lt;a href="#">Ghost Sighting in Mansion&lt;/a>&lt;/li>
		&lt;li>&lt;a href="#">UFO picked up by satelites&lt;/a>&lt;/li>
		&lt;li>&lt;a href="#">Mutants amoung us?&lt;/a>&lt;/li>
		&lt;li>&lt;a href="#">Bigfoot: Fact or Fiction?&lt;/a>&lt;/li>
	&lt;/ul>

	&lt;h3>Archives&lt;/h3>
	&lt;ul>
    	&lt;li>&lt;a href="">June 2008&lt;/a>&lt;/li>
        &lt;li>&lt;a href="">May 2008&lt;/a>&lt;/li>
        &lt;li>&lt;a href="">April 2008&lt;/a>&lt;/li>
        &lt;li>&lt;a href="">March 2008&lt;/a>&lt;/li>
	&lt;/ul> 

	&lt;/div>&lt;!-- end sidebar -->

&lt;/div>&lt;!-- end content -->

&lt;/div>&lt;!-- end main -->
</pre>
<h3>Step 8 &#8211; Footer</h3>
<p>The footer will work like the top bar, header, and navigation worked, with an outer div to hold a repeating background, and an inner div to centre the content and contain it within the 900px of our page. To do this, we just need to add the &#8220;container&#8221; class to our &#8220;footer content&#8221; div. </p>
<p>Our footer is going to have three columns: copyright info, links, and subscription options. Each will have to be contained within its own div. </p>
<pre name="code" class="html">
&lt;div id="footer">

	&lt;div id="footer_content" class="container">

		&lt;div id="copyright">
			&lt;p>Copyright &copy; 2009.&lt;br /> All Rights Reserved.&lt;/p>
		&lt;/div>

		&lt;div id="links">
			&lt;h4>Links&lt;/h4>
			&lt;ul>
				&lt;li>&lt;a href="#">PSDtuts - Photosohp tutorials&lt;/a>&lt;/li>
				&lt;li>&lt;a href="#">NetTtuts - Web development and design tutorials&lt;/a>&lt;/li>
				&lt;li>&lt;a href="#">VectorTuts - Illustrator and vector tutorials&lt;/a>&lt;/li>
				&lt;li>&lt;a href="#">FlashTuts - Adobe Flash tutorials&lt;/a>&lt;/li>
			&lt;/ul>
		&lt;/div> 

		&lt;div id="feeds">
			&lt;h4>Follow our updates&lt;/h4>
			&lt;ul>
				&lt;li>&lt;a href="#">Subscribe to RSS Feed&lt;/a>&lt;/li>
				&lt;li>&lt;a href="#">What is RSS?&lt;/a>&lt;/li>
				&lt;li>&lt;a href="#">Email Updates&lt;/a>&lt;/li>
			&lt;/ul>
		&lt;/div>

	&lt;/div>&lt;!-- end footer content -->

&lt;/div>&lt;!-- end footer -->
</pre>
<p>Okay, we&#8217;re done our markup! Lets take a look in the browser (I&#8217;m using Safari 4, which is awesome, by the way)</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/11.jpg" alt="11" /></div>
<p>It doesn&#8217;t look like much, but it has all our content laid out in a logical, readable way. It&#8217;s important that an un-styled web page contains all the information needed for a reader to be able to understand and navigate the page easily. This ensures that anyone browsing with CSS disabled, or using a specialized browser (like a screen reader for visually impaired people), will still be able to access and understand content. Keeping this in mind will also ensure a logical layout, which will be easier to modify later.</p>
<h3>Step 9 &#8211; CSS</h3>
<p>Now comes the fun part: giving our page some style. Prepare yourself &#8211; to achieve the layout we want, we&#8217;re going to have to face some confusing little CSS headaches. I&#8217;m going to try to explain the underlying concepts that lead to these problems, so that you not only learn how to solve them, but also gain a better understanding of how CSS works and how to deal with any problems you might come up against. Let&#8217;s get started!</p>
<p>Create a new document &#8220;style.css&#8221; and link to it in the head of your html document:</p>
<pre name="code" class="html">
&lt;head>
		&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		&lt;title>My Blog&lt;/title>
		&lt;link href="style.css" rel="stylesheet" type="text/css" media="screen" />
&lt;/head>
</pre>
<h3>Step 10 &#8211; Basic Clean-up</h3>
<p>First off, we need to know what we&#8217;re working with, which means getting rid of default browser styles. </p>
<p>We&#8217;re going to use a simple css reset to get rid of those pesky margins and styles: </p>
<pre name="code" class="html">
body, div, h1, h2, h3, h4, h5, h6, p, ul, ol, dl, dt, dd, img, form, fieldset, blockquote {
    margin:0px; padding:0px; border:0px;
}
</pre>
<p>We also have the wrong font for our whole page, lets fix that: </p>
<pre name="code" class="html">
body {font-family: Arial, helvetica, sans-serif; }
</pre>
<p>The next step is kinda neat: remember how we added the &#8220;container&#8221; class to all divs that had text content? It&#8217;s time to get that container to contain our content! Like I said before, we&#8217;re making our page 900px wide, and we need out content to be centered. </p>
<pre name="code" class="html">
.container {width: 900px; margin: 0 auto; }
</pre>
<p>And just like that, we have a nice 900px wide, centered webpage.</p>
<h3>Step 11 &#8211; Starting at the Top</h3>
<p>We&#8217;re going to start at the top with the small blueish bar containing the subscription and login links. Okay, the first thing we notice about our two paragraphs at the top is that they&#8217;re supposed to be beside each other, not on top.  To do this, we need to float the elements.</p>
<h4>How floats work</h4>
<p>When we look at our webpage in the browser, we see a bunch of elements of different widths and heights. The browser, however, sees only a bunch of boxes stacked on top of one another, with each element taking up the entire width of the container.  The webpage has a very simple &#8220;flow&#8221;: each element is pushed down the page by the element above it.<br />
To get two elements to sit next to each other, we need to take them out of the normal &#8220;flow&#8221; of the page.
</p>
<p>When an element is floated, a few things happen: it gets stuck to the side of the page (or another element) and it is removed from the normal flow of elements &#8211; that is, instead of taking up the entire width of the page, it only takes up the space it directly occupies, allowing the elements below it to move up beside. </p>
<p>Lets try it with our two little paragraphs: </p>
<pre name="code" class="css">
p#login {float: left; }

p#subscribe {float: right; }
</pre>
<p>Take a look in the browser, and we have a problem! The h1 logo has moved itself up between the two floated items. When you float elements, it&#8217;s like you&#8217;re telling them to &#8220;break the rules&#8221;, the only problem is, when you let some elements break the rules, other elements start doing it too! What we need is a way to tell the browser that these two paragraphs, and these two paragraphs ONLY, are allowed to break the rules, so after the floated elements, the normal flow is restored. To do this, we need to add a rule to the parent div of the two floated elements, which lets the two paragraphs inside it float without affecting the rest of the page.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/12.jpg" alt="12" /></div>
<p>To do this, we need to add a property to the containing div of &#8220;overflow&#8221; and set the value to &#8220;hidden&#8221;. </p>
<pre name="code" class="css">
#top_bar {overflow: hidden; }
</pre>
<p>It&#8217;s not terribly important to understand what&#8217;s overflowing, or where it&#8217;s hiding, so long as you understand that the overflow: hidden rule controls the behavior of floats within the div. Now, I don&#8217;t want to confuse or scare anybody, but this technique doesn&#8217;t always work in all browsers. It works here, but always test in IE for your own designs. There are a number of other techniques worth noting, all of them have their advantages. Peter-Paul Koch describes a method similar to the one I use here in his article <a href="http://www.quirksmode.org/css/clearing.html">Clearing floats</a>. Steve Smith describes his method of &#8220;Floating (Nearly) Anything&#8221; in his post <a href="http://orderedlist.com/articles/clearing-floats-the-fne-method">Clearing Floats: The FnE Method</a>. Also, it&#8217;s a bit more of an advanced technique, but Tony Aslett has pioneered a rather ingenious and devious technique, described in the article <a href="http://www.positioniseverything.net/easyclearing.html">How To Clear Floats Without Structural Markup</a>. Choose the method that works best for you, and remember to test your browsers!</p>
<p>One more important thing about floats is that you should always specify a width. Your CSS will still validate if you don&#8217;t (you will get a warning) but it&#8217;s best practice and you sometimes wind up with unexpected results in some browsers if you don&#8217;t. </p>
<p>Since we want our layout to be easily resizable, we&#8217;re not going to use pixel values, we&#8217;ll use percentages instead. We may as well let each element take up 50% of the width. Because we&#8217;ve given it so much space, the subscribe paragraph has moved to the left. We can get it to stick to the right again by specifying text-align: right. </p>
<pre name="code" class="css">
p#login {float: left; width: 50%;}

p#subscribe {float: right; width: 50%; text-align: right; }
</pre>
<h4>Styling the paragraphs</h4>
<p>Before adding a background image, I like to style the fonts so that I can see how much space I have to deal with. </p>
<h4>Font sizes using ems</h4>
<p>We want to make our web page as accessible and flexible as possible, to reach the largest possible audience, right? That includes allowing readers to resize text to a comfortable size. Now, different browsers deal with resizing in different ways, but as usual, the problem browser is IE/Win. In Internet Explorer, if your text size is set in pixels, you can&#8217;t resize it, so your reader is stuck with whatever font size you&#8217;ve specified. That&#8217;s not very nice, especially for readers with poor vision. To correct this problem, we need to use a different unit &#8211; ems.</p>
<p>Ems are a relative size unit &#8211; it means the width of the &#8220;m&#8221; in whatever font-size is specified. The default size set by browsers is 16px, so 1em would equal a font-size of 16px. </p>
<p>To make our web page fully scalable, we can convert all our font sizes to ems. An easy way to do this is with the web application <a href="http://riddle.pl/emcalc/">Em Calculator</a> (works best in FireFox). </p>
<p>However, to avoid doing any confusing math, there&#8217;s an easier way. Since ems are relative to the default font-size of the page, if we change the default, the em values will be different. </p>
<p>To make the math easier, we can give ourselves a base of 10, by setting the default font to 10px instead of 16. To do this, we just specify in our CSS file that we want our fonts to be 62.5% of the default. </p>
<pre name="code" class="css">
body {font-family: Arial, helvetica, sans-serif; font-size: 62.5%; }
</pre>
<p>Now we just need to divide each font size in pixels by 10, and we have our em value. To start, the font size for our two &lt;p> tags at the top of the page is 12px, which works out to 1.2ems. </p>
<pre name="code" class="css">
#top_bar p {font-size: 1.2em; color: #ebf0e8;}

#top_bar a {font-size: 1.2em; color: #a5bf8d;}
</pre>
<h4>Expandable backgrounds</h4>
<p>Next, we add in the repeating background image we sliced from our PSD: </p>
<pre name="code" class="css">
#top_bar {overflow: hidden; background: url(images/bar_slice.jpg) repeat-x; }
</pre>
<p>We need to add a bit of padding to the top and bottom of the paragraphs to make the image stretch to its full height. To get the right values, we need to go back to our PSD and measure the height of the bar with the ruler tool: mine turns out to be 26px tall. Since our text is 12px tall, the total padding will be 26-12 or 14px. This means we add 7px of padding to the top, and 7px to the bottom. (These values are sometimes off by a pixel or two, but are a good place to start, just keep checking in your browser)</p>
<pre name="code" class="css">
#top_bar {
	overflow: hidden;
	background: url(images/bar_slice.jpg) repeat-x;
	padding-top: 7px;
	padding-bottom: 7px;
}
</pre>
<p>We could also get the div to stretch to its full height by specifying a height of 26px, but you should always avoid specifying a height for your elements as much as possible, to allow for maximum flexibility. If you restrict your element to a specific height, you aren&#8217;t allowing for larger text or additional content.</p>
<p>Check it out, it looks just like our PSD. But here&#8217;s where things get tricky: try re-sizing the text in your browser. When we increase the size of the text, the ratios change, we lose our bottom padding, and the text eventually overflows beyond its background. To make a more &#8220;indestructible&#8221; web site, we need to get the background to stretch as the text grows or as more content is added, so that there is always 7px of padding at the top and bottom, no matter how big the text is. Since our background image is only 26px tall, we need to have something else to stretch out further. What we&#8217;re going to do is basically put a solid color behind the image, so that when the text gets bigger and the image can&#8217;t contain it, the color behind it shows through. The color at the bottom of our gradient is #08413c. So lets add that to our background. To specify that we want the image to always stick to the top of the element, so that the color stretches out from the bottom, we add a background-position value after the image url. </p>
<pre name="code" class="html">background: #08413c url(images/bar_slice.jpg) top repeat-x; </pre>
<p>Now try resizing the text: the background grows with it, and it looks pretty much the same as it gets bigger. This also lets us know that if we wanted to add a second line of content later on, we wouldn&#8217;t have to change anything in our css. The ability of an element to expand for more or larger text is often overlooked in web development, and this causes pages to break down when text is resized. Just check out my <a href="http://www.utoronto.ca/">University&#8217;s Homepage</a>, try making the text one step bigger, and you lose a navigation link!</p>
<p>One more thing, we just need to add that 1px border to the bottom of our bar: </p>
<pre name="code" class="css">border-bottom: 1px solid #7ab7b7;
</pre>
<p>So here&#8217;s where we&#8217;re at so far: </p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/13.jpg" alt="13" /></div>
<h3>Step 12 &#8211; Header</h3>
<p>Since we&#8217;re going to be making the text of our blog name and description white, lets add in the background first. We probably won&#8217;t need this image to stretch out, but just in case, we&#8217;ll repeat the same process of adding a background color and position: </p>
<pre name="code" class="css">
#header {
	background: #01835f url(images/header_slice2.jpg) top repeat-x;
}
</pre>
<p>Now lets do our font styles: </p>
<pre name="code" class="css">
h1 {
	font-family: "Myriad Pro", arial, sans-serif;
	color: #fff;
	font-weight: normal;
	font-size: 4.8em;
	padding-top: 25px;
}

p.desc {
	font-family: "Myriad Pro", arial, sans-serif;
	color: #fff;
	font-size: 2.4em;
}
</pre>
<p>Alright, so now we need to get our description out beside our logo.  This can be achieved by floating, but I tried it and ran into problems with specifying widths, and I managed to get a much better result using absolute positioning. Plus it gives me a chance to explain an important concept! </p>
<h4>Absolute Positioning</h4>
<p>If we want to position an element outside the &#8220;flow&#8221; of the page without using floats, we can use absolute positioning, which basically allows you to position an element anywhere within a div regardless of other elements within the div. This means that when you specify a position of, say, left: 10px, it will position the element 10px to the left of the side of the div, whether or not there is another element there.<br />
First, in order to absolutely position an element, we need to set the position of the parent div to relative, because the absolute positioned element is positioned <em>relative</em> to the parent div.
</p>
<pre name="code" class="css">
#branding {
	overflow: hidden;
	margin-bottom: 30px;
	position: relative;
}
</pre>
<p>Now we can set the absolute position of our description. When position: absolute is used, we can specify its position in terms of left, right, top and bottom, using pixels, percentages or ems. First, the top &#8211; the description is almost exactly 50% from the top of the header, so lets specify that: </p>
<pre name="code" class="css">
p.desc {
	font-family: "Myriad Pro", arial, sans-serif;
	color: #fff;
	font-size: 2.4em;
	position: absolute;
	top: 50%;
}
</pre>
<p>Now we need to push it out to the right, by giving it a value for left:<br />
if we use pixels, then when the text is resized, it will get closer to the h1, and eventually overlap it, so scrap that method. We get the same problem with percentages, only not as dramatically. The best thing to do is position it with ems, which as you remember get bigger as text size gets bigger, so the space between the h1 and description will remain when the text is resized.
</p>
<pre name="code" class="css">
position: absolute;
top: 50%;  left: 8em;
</pre>
<p>And it looks great!</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/14.jpg" alt="14" /></div>
<h3>Step 13 &#8211; Navigation</h3>
<p>Lets clean up this navigation menu, and get rid of the list style and text decoration, and add font styles. </p>
<pre name="code" class="css">
ul#menu {list-style: none; }

ul#menu li a {
	font-size: 1.8em;
	text-decoration: none;
	color: #2b2b2b;
	text-transform: uppercase;
}
</pre>
<p>To get our links to line up horizontally, we&#8217;re going to set the list items to float: left, so that each list item is &#8220;stuck&#8221; to the one to its left. </p>
<pre name="code" class="css">
ul#menu li {float: left; }
</pre>
<p>We of course wind up with the same problem as with all floats, but all we have to do is add in overflow: hidden to the navigation div, and we&#8217;re good to go.</p>
<p>Each list item is about 45px apart, so lets add 45px of padding to the right of each item. </p>
<pre name="code" class="css">
ul#menu li {
	float: left;
	padding-right: 45px;
}
</pre>
<p><strong>Note:</strong> All these padding values can be written shorthand as: </p>
<pre name="code" class="css">padding:  11px 45px 11px 0px; </pre>
<p>The shorthand for padding (and margins) is the values for top-right-bottom-left all in one line. I find the best way to remember the order is to think of the compass directions: n-e-s-w. </p>
<p>Next, lets add the background image, employing the same technique as before to ensure our background stretches when text is resized: </p>
<pre name="code" class="css">
#navigation {background: #8ec196 url(images/nav_slice.jpg) top repeat-x; overflow: hidden; }
</pre>
<p>To find the padding value, once again just measure the height of the bar (mine is 40px) and subtract the size of the text (18px) to get the total padding, and divide by two: 40-18=22px.</p>
<pre name="code" class="css">
ul#menu li {float: left; padding-top: 11px; padding-bottom: 11px; }
</pre>
<p>To move our navigation menu down a bit, the easiest thing to do is just add a margin to the bottom of the branding div.</p>
<pre name="code" class="css">
#branding {overflow: hidden; margin-bottom: 30px; }
</pre>
<p>And finally, the 1px border at the top of the bar:</p>
<pre name="code" class="css">border-top: 1px solid #9cebda;</pre>
<p>That&#8217;s it! We&#8217;re done with the header! Let&#8217;s take a look:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/15.jpg" alt="15" /></div>
<h3>Step 14 &#8211; Content</h3>
<p>The first thing we need to do here is to create the two columns &#8211; one for the posts, one for the sidebar. By now this should be simple: just float one to the left, one to the right.</p>
<pre name="code" class="css">
#posts {float: left; }

#sidebar {float: right; }
</pre>
<p>And&#8230; Nothing happened.  At least it looks that way, scroll down and you&#8217;ll find your sidebar stuck to the right side beneath the posts. Before the sidebar can move in beside the posts, we need to specify how much space the posts div can take up, and specify a width for the sidebar as well. Again, we&#8217;re going to use percentages instead of pixels, so that if the text is resized, the sidebar stays at the side. </p>
<pre name="code" class="css">
#posts {float: left; width: 67%; }

#sidebar {float: right; width: 33%; }
</pre>
<p>And we need to hide the overflow of our containing div: </p>
<pre name="code" class="css">
#content {overflow: hidden; }
</pre>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/16.jpg" alt="16" /></div>
<p>Great, we have a nice little two-column layout!</p>
<h3>Step 15 &#8211; Styling the Posts</h3>
<p>Since we used a CSS reset, the content of our post section has no style at all, and we need to go through the tedious task of styling every single possible element. My workflow is a bit different for this than with the rest of the page, as I don&#8217;t really refer to the Photoshop document. I find font styling in Photoshop to be a real pain, so I usually skip it. To get the styles for my posts, I go through a process of trial and error. I usually start with settings similar to default browser styles, which you can find at <a href="http://css-class.com/test/css/defaults/UA-style-sheet-defaults.htm">This Link</a>, and tweak them to my liking. I&#8217;m not going to go through the whole process step by step, but this is what I came up with in the end: </p>
<pre name="code" class="css">

#posts h2 {margin: 7px 0 4px 0; font-size: 2.4em; }

#posts h2, h3, h4, h5, h6 {color: #3c3f40; }

#posts p {line-height: 1.3em; padding: 7px 0; font-size: 1.2em; }

#posts small {font-size: 1.1em; }

#posts a {color: #327800; font-weight: bold; text-decoration: none; }

#posts blockquote {margin: 0.7em 3em; border-left: 2px solid #327800; padding-left: 10px; }

#posts ol, ul, dl {font-size: 1.2em; margin: 4px 0 4px 40px; }

#posts h3, h4, h5, h6 {padding: 4px 0; }

#posts strong {font-weight: bolder; }

#posts em {font-style: italic; }

#posts code {font-size: 1.2em; }

#posts h3 {font-size: 1.8em;}

#posts h4 {font-size: 1.4em; }
</pre>
<p>Which should look something like this:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/17.jpg" alt="17" /></div>
<p>Now there&#8217;s about 35px at the top of our post section, but our h2 tags already have a margin of 7px at the top, so lets add a margin of 28px to the top of the posts div. </p>
<pre name="code" class="css">
#posts {
	float: left;
	width: 67%;
	margin-top: 28px;
}
</pre>
<p>And that&#8217;s about it for the posts section. On to the sidebar!</p>
<h3>Step 16 &#8211; Sidebar</h3>
<p>First off, lets add in that background color: </p>
<pre name="code" class="css">
#sidebar {float: right; background: #d8ddd1;}
</pre>
<p>And fix the font styles: </p>
<pre name="code" class="css">
#sidebar h3 {
	font-size: 1.6em;
	color: #3c3f40;
	margin-top: 10px
}

#sidebar ul {list-style: none; margin-left: 20px; }

#sidebar ul li {font-size: 1.2em; }

#sidebar ul li a {text-decoration: none;color: #525254; }
</pre>
<p>We also need to push the sidebar down by 25px (35px &#8211; the 10px margin at the top of the h3 tags). This time, however, we can&#8217;t use the margin-top property, because this will move the entire sidebar, including the background, down the page. We want the background to start right beneath the navigation bar, but the content to start 35px below that, so we need to use the padding-top property. We also need padding on the left, right and bottom, and 25px sounds about right, so we can declare it all in one padding value: </p>
<pre name="code" class="css">
#sidebar {
	float: right;
	background: #d8ddd1;
	padding: 25px;
	width: 33%;
}
</pre>
<p>But oh no! Our sidebar is no longer at the side! This is because we added padding to the sides of the sidebar. When you add padding to an element, you are actually making it bigger. We just made our sidebar 50px wider, so now the widths of the floated elements add up to more than 100%. To fix this, lets first convert our 25px of padding to a percentage of 900px. It works out to about 2.7%, but I&#8217;m rounding up to 3%. </p>
<pre name="code" class="css">padding: 25px 3%; </pre>
<p><strong>Note:</strong> this is another shorthand value, it means that the top <em>and</em> bottom are both 25px, and that the left <em>and</em> right are both 3%. </p>
<p>Our sidebar is now 33+6% wide, so it&#8217;s still too wide, but all we need to do now is subtract the 6% from the original 33% </p>
<pre name="code" class="html">width:  27%; </pre>
<p>I thought the sidebar looked a bit wide at this point, so I reduced it to 25%.</p>
<p>The important thing to remember is that all the padding, margin, and even border values add to the width of the element, so you need to adjust the width property to compensate every time you add padding, margins or borders. </p>
<p>And there we go, no matter how big you make the text, our sidebar stays at the side, and the ratio between the post area and sidebar remain the same. It&#8217;s an excessively plain sidebar, but I&#8217;m not going to bother styling it any more, just because its not really important to the goals of this tutorial.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/18.jpg" alt="18" /></div>
<h3>Step 17 &#8211; Footer</h3>
<p>First lets make the font a bit bigger and add some styles:</p>
<pre name="code" class="css">
#footer p {color: #a5bf8d;}

#footer h4 {color:	#a5bf8d; font-size: 1.4em; padding-top: 0; }

#footer ul {list-style: none; margin-left: 0; }

#footer ul li a {text-decoration: none; color: #fff; }
</pre>
<p>Next we can add in our background image:</p>
<pre name="code" class="css">
#footer {font-size: 1.2em; background: url(images/footer_slice.jpg) repeat-x; }
</pre>
<p>And since we want it to be able to stretch, we can add in the color at the bottom of the gradient to our background value just like we did before: </p>
<pre name="code" class="html">background: #093b2b url(images/footer_slice.jpg) top repeat-x; </pre>
<p>Next add in that 2px border at the top: </p>
<pre name="code" class="html">border-top: 2px solid #1e956c; </pre>
<p>Lets add a margin and some padding to the top and bottom:</p>
<pre name="code" class="css">
#footer {
	font-size: 1.2em;
	background: #093b2b url(images/footer_slice.jpg) top repeat-x;
	margin-top: 30px;
	padding-top: 20px;
	padding-bottom: 20px;
}
</pre>
<p>Next, we&#8217;re going to create three columns by floating. Floating three elements works pretty much the same as floating two, we&#8217;re going to float each column to the left. We need to declare the widths of each element being floated, using percentage values to allow for expansion. </p>
<pre name="code" class="css">
#copyright {float: left; width: 20%; }

#links {float: left; width: 40%; }

#feeds {float: left; width: 40%; }
</pre>
<p>Just remember that we have to set the overflow to hidden in the parent div.</p>
<pre name="code" class="html">#footer {overflow: hidden;}</pre>
<p>At this point, I decided that it would actually look better if the links and RSS divs came first, and the copyright info was at the far right, so I changed the order of the divs, and set the copyright div to float: right, and aligned the text to the right as well. </p>
<pre name="code" class="css">
#copyright {
	float: right;
	text-align: right;
	width: 20%;
}
</pre>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/422_flexibleWebsite/images/19.jpg" alt="19" width="600" height="124"/></div>
<p>And there we have it, we&#8217;re done coding and styling our page!</p>
<h3>Step 18 &#8211; Accesibility testing</h3>
<p>Througout the tutorial, I&#8217;ve been testing my page in Safari 4. Since Safari 4 is probably the most standards-compliant and modern browser, it displays web pages the most predictably. If only everyone used a standards-compliant browser&#8230; Unfortunately, there are still people out there using outdated browsers, and we need to prepare for that.</p>
<p>Let&#8217;s start with the most problematic browser first: Internet Explorer 6. I use a mac, and have yet to find an effective (free) way to test my web pages in Internet Explorer. If anyone reading this knows of some sort of magical technique, please, let me know. Anyways, time to haul out the ol&#8217; family Dell. To install multiple versions of Internet Explorer at once, google &#8220;Multiple IE&#8221; and download the .zip file.</p>
<p>Surprisingly, it works out just fine in IE6! IE7 is okay too. Text is resizeable! I also tested this page in Firefox, Opera and Camino. I had trouble installing Google Chrome on the PC, so I couldn&#8217;t tell you if it works, but I think it should, bceause of the simplicity of the markup and style.</p>
<h3>Conclusion</h3>
<p>That&#8217;s it! Hopefully you&#8217;ve learned a thing or two about coding flexible websites. Check it out in any browser, make the text bigger, make it smaller, and our layout adapts just fine. Disable CSS, and it still makes sense! I hope you can see how easy it is to make your websites less fallible! This page was very simple, without many complicated challenges. When your layouts get more complicated, it will be a bit tougher to maintain flexibility. </p>
<ul class="webroundup">
<li>Follow us on <a href="http://www.twitter.com/nettuts">Twitter</a>, or subscribe to the <a href="http://feeds.feedburner.com/nettuts" title="Nettuts+ RSS Feed">Nettuts+ RSS Feed</a> for more daily web development tuts and articles.</li>
</ul>
<p>
<script type="text/javascript"><!--digg_url = "post permalink (not digg url)"; // -->
</script><br />
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://net.tutsplus.com/tutorials/html-css-techniques/how-to-design-and-code-a-flexible-website/feed/</wfw:commentRss>
		<slash:comments>124</slash:comments>
		</item>
		<item>
		<title>Using Compass and Sass for CSS in your Next Project</title>
		<link>http://net.tutsplus.com/tutorials/html-css-techniques/using-compass-and-sass-for-css-in-your-next-project/</link>
		<comments>http://net.tutsplus.com/tutorials/html-css-techniques/using-compass-and-sass-for-css-in-your-next-project/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 16:16:57 +0000</pubDate>
		<dc:creator>Alex Coomans</dc:creator>
				<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[compass]]></category>
		<category><![CDATA[sass]]></category>

		<guid isPermaLink="false">http://net.tutsplus.com/?p=6520</guid>
		<description><![CDATA[<img src="http://nettuts.s3.amazonaws.com/421_sass/200x200.png" alt="Using Compass and Sass for CSS in your Next Project" />]]></description>
			<content:encoded><![CDATA[<p>Sass could potentially be called CSS 2.0; it has some really nifty features that allow you to write your code in less time and with more flexibility. In today&#8217;s article, we will be working the basics!
</p>
<p><span id="more-6520"></span></p>
<div class="tutorial_image">
<a href="http://nettuts.s3.amazonaws.com/421_sass/demo.zip"><img src="http://nettuts.com/wp-content/themes/nettuts/site_images/button_src_nm.jpg"></a>
</div>
<h3>What is Sass?</h3>
<p>Sass is simply a different way to work with CSS. It allows you to keep your CSS code very simple and helps increase readability. It takes something like this:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/421_sass/1.png" border="0" /></div>
<p>And turns it into:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/421_sass/2.png" border="0" /></div>
<p>You can check out Sass at <a href="http://sass-lang.com/">http://sass-lang.com/</a></p>
<h3>What is Compass?</h3>
<p><a href="http://compass-style.org">Compass</a> makes working with Sass even easier. The author, Chris Eppstein, has also included some converted frameworks such as Blueprint, YUI, and 960.gs. It also easily integrates with Ruby based projects, but is just as easy to add to any other project. Compass is open-source and is extremely well documented. Check out the <a href="http://github.com/chriseppstein/compass/tree/master">source</a> and <a href="http://wiki.github.com/chriseppstein/compass/">documentation</a>.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/421_sass/3.png" border="0" /></div>
<h3>Installation</h3>
<p>Both Sass (which is part of the Haml Project) and Compass are installed through RubyGems. If you dont have any clue what I am talking about, check out &quot;Step 1 &#8211; Installing Ruby on Rails&quot; of my previous article <a href="http://net.tutsplus.com/tutorials/ruby/ruby-on-rails-for-designers/">Ruby on Rails for Designers</a>, and follow the steps down until you hit the section named &quot;Installing Rails&quot;.</p>
<p>To install both of these gems, we can just run a single command:</p>
<pre name="code" class="html">(sudo) gem install haml chriseppstein-compass</pre>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/421_sass/4.png" border="0" /></div>
<p>As long as it says that it successfully installed both gems, you are good to go!</p>
<h3>Project Setup</h3>
<p>If you are working with a Ruby-based project then check out the documentation as it will explain how to get it working with your specific framework, but I&#8217;ll assume we are working with a simple HTML or the like project.</p>
<p>The compass command has a lot of options, and if you run compass &#8211;help, you might see:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/421_sass/5.png" border="0" /></div>
<p><small>FYI: The next to top line that says &quot;Loading haml-edge gem.&quot; is because I use the latest version, so you don&#8217;t need to worry about it if yours says something different.</small></p>
<p>Now we are going to start our Compass project. To create run the following command (including the trailing period, that tells compass where we want to make our project!)</p>
<pre name="code" class="html">compass  --sass-dir=sass .</pre>
<p>And you should see:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/421_sass/6.png" border="0" /></div>
<p>Where initializing, Compass would normally default to having the Sass in a folder name src, but I wanted to change that so I added the option. If you look at the folder, you should see a file named config.rb and two other folders.</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/421_sass/7.png" border="0" /></div>
<p>The config.rb is the configuration that Compass looks at, but most of the time you wont need to mess with this. Also, like the output from the creation command says, we have three ways of updating our CSS from our Sass:</p>
<li><code>compass</code> &#8211; using this option, you would have to be in the correct directory, and then Compass would compile your Sass once.</li>
<li><code>compass -u path/to/project</code> &#8211; this is about the same as the command as above, but you do not have to be in the project directory, and rather pass in it with the command.</li>
<li><code>compass --watch [path/to/project]</code> &#8211; this command is pretty awesome in that it watches for any changes to your Sass files and will automatically recompile them whenever a change is detected.</li>
<p>Now that you know how to setup a project, I&#8217;ll explain some of the &quot;rules&quot; of working with Sass</p>
<h3>Sass Syntax</h3>
<p>Normally when writing CSS, you would write something like:</p>
<pre name="code" class="css">#header {width: 900px; background:#111;}
#header a {color:#000; text-decoration:none;}</pre>
<p>One of the problems with that is that you are repeating the same name many times. Lets make this into Sass. I am going to be working in the screen.sass file, so delete everything and your Sass would like:</p>
<pre name="code" class="css">
#header
  :width 900px
  :background #111
  a
    :color #000
    :text-decoration none</pre>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/421_sass/8.png" border="0" /></div>
<p>Personally, that it much easier to read and write this way, no curly braces or semicolons. The way that Sass understand the nesting is through indentation.</p>
<p>The first selector is not indented at all so the final CSS with start with that. Also, all properties, (so color, height width, etc) begin with a colon. So for the properties for #header they are indented. It does not matter if you use tabs or spaces, but you must remain consistent, otherwise you will see an error (which I&#8217;ll show in just a minute).</p>
<p>So now that you have your properties, we have our next selector. Since this is indented to the same as the properties, the CSS output will be <code>#header a ...</code></p>
<p>Now that we are this far, lets try compiling: (in your project directory)</p>
<pre name="code" class="html">compass</pre>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/421_sass/9.png" border="0" /></div>
<p>And voila!</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/421_sass/10.png" border="0" /></div>
<p>Let&#8217;s say that you didn&#8217;t indent everything the same, Compass would error:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/421_sass/11.png" border="0" /></div>
<p>Now, once you know CSS, Sass wont be very much of a learning curve, as the main difference when getting started is the different formatting rules. Next, we will get working with some of the more advanced (but cooler!) parts of Sass.</p>
<h3>Features of Sass</h3>
<h4>Variables</h4>
<p>One awesome feature of Sass is it&#8217;s ability to use variables. An example:</p>
<pre name="code" class="css">
!link_color = #fff
#header
  :width 900px
  :background #111
  a
    :color = !link_color
    :text-decoration none</pre>
<p>Compiling this, would give you:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/421_sass/12.png" border="0" /></div>
<h4>Math</h4>
<p>You may be laughing at this title, but its true, you can do math in Sass! For this demonstration, we will be using Sass&#8217;s interactive mode, so run:</p>
<pre name="code" class="html">sass -i</pre>
<p>And a little messing around would give you:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/421_sass/13.png" border="0" /></div>
<p>Or a more visual approach:</p>
<p>		<span style="padding:5px; margin:2px;background:#111; color:#fff;">#111</span> &#8211; <span style="padding:5px; margin:2px;background:#999;">#999</span> = <span style="padding:5px; margin:2px;background:#000; color:#fff;">#000</span>
<div>&nbsp;</div>
<p>		<span style="padding:5px; margin:2px;background:#111; color:#fff;">#111</span> + <span style="padding:5px; margin:2px;background:#999;">#999</span> = <span style="padding:5px; margin:2px;background:#aaa;">#aaa</span>
<div>&nbsp;</div>
<p>		<span style="padding:5px; margin:2px;background:#398191;">#398191</span> + <span style="padding:5px; margin:2px;background:#111;color:#fff;">#111</span> = <span style="padding:5px; margin:2px;background:#4a92a2;">#4a92a2</span>
<div>&nbsp;</div>
<p>		<span style="padding:5px; margin:2px;background:#398191;">#398191</span> &#8211; <span style="padding:5px; margin:2px;background:#111;color:#fff;">#111</span> = <span style="padding:5px; margin:2px;background:#287080;">#287080</span>
<div>&nbsp;</div>
<p>Apart from addition and subtraction, you can also multiply and divide:</p>
<p>		<span style="padding:5px; margin:2px;background:#398191;">#398191</span> / 2 = <span style="padding:5px; margin:2px;background:#1c4048;">#1c4048</span>
<div>&nbsp;</div>
<p>		<span style="padding:5px; margin:2px;background:#398191;">#398191</span> * 2 = <span style="padding:5px; margin:2px;background:#72ffff;">#72ffff</span>
<div>&nbsp;</div>
<p><small>To exit the Sass interactive mode, keypress Control-C, and then type &quot;end&quot;, and press enter.</small></p>
<h4>Mixins</h4>
<p>If you have ever heard of keeping your code DRY, DRY means &quot;don&#x27;t repeat yourself.&quot; Mixins allow you to do just that. For example, with the gaining popularity of rounded corners, you may want to create a mixin to handle that, so somewhere, (not under a selector) you would add:</p>
<pre name="code" class="css">
=rounded
  :border-radius 4px
  :-moz-border-radius 4px
  :-webkit-border-radius 4px</pre>
<p>And to use, you would do something like:</p>
<pre name="code" class="css">
#header
  :width 900px
  :background #111
  +rounded</pre>
<p>And when compiled:</p>
<div class="tutorial_image"><img src="http://nettuts.s3.amazonaws.com/421_sass/14.png" border="0" /></div>
<p>Lets say though, you may want the border radius to be variable &#8211; well you can have a mixin expect to be passed some values. Changing our mixin, it would look like:</p>
<pre name="code" class="css">
=rounded(!radius = 4px)
  :border-radius = !radius
  :-moz-border-radius = !radius
  :-webkit-border-radius = !radius</pre>
<p>And then to use you could use what we did before, and then the value would default to 4px, otherwise:</p>
<pre name="code" class="css">
#header
  :width 900px
  :background #111
  +rounded(8px)</pre>
<h4>Importing</h4>
<p>Sass also has the ability to import other Sass files, so lets say you wanted to import a file (in the same as this sass file), you would add:</p>
<pre name="code" class="html">@import reset.sass</pre>
<p>This feature is really nice in giving you the ability the keep extraneous styles outside your main file. For example, you could also keep a mixins sass file that you copied around projects and then a simple import would add that code it &#8211; no copy and paste.</p>
<h3>Conclusion</h3>
<p>I hope you understand the basics of using Sass and Compass and will possibly use it in your next project! Now, if you are like I was when I found Sass, and said &quot;I don&#8217;t need this!&quot;, give it a try. I&#8217;ve switched over to it for all of my recent projects and I really enjoy working with it.</p>
<h3>Links</h3>
<p>Here are a few links that may help you along the way:</p>
<ul>
<li>The <a href="http://sass-lang.com/docs/yardoc/SASS_REFERENCE.md.html">Sass Reference</a> contains what I talked about today and much much more. Definitely worth a look if you are serious about Sass.</li>
<li>The <a href="http://github.com/seaofclouds/sass-textmate-bundle/tree/master">TextMate</a> bundle for Sass is great and I use it often.</li>
<li>The <a href="http://wiki.github.com/chriseppstein/compass/">official screencast</a> for Compass, while long, covers about every base with Compass and Sass.</li>
</ul>
<ul class="webroundup">
<li>Follow us on <a href="http://www.twitter.com/nettuts">Twitter</a>, or subscribe to the <a href="http://feeds.feedburner.com/nettuts" title="Nettuts+ RSS Feed">Nettuts+ RSS Feed</a> for more daily web development tuts and articles.</li>
</ul>
<p>
<script type="text/javascript"><!--digg_url = "post permalink (not digg url)"; // -->
</script><br />
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://net.tutsplus.com/tutorials/html-css-techniques/using-compass-and-sass-for-css-in-your-next-project/feed/</wfw:commentRss>
		<slash:comments>101</slash:comments>
		</item>
	</channel>
</rss>
<!--
This site's performance optimized by W3 Total Cache:

W3 Total Cache improves the user experience of your blog by caching
frequent operations, reducing the weight of various files and providing
transparent content delivery network integration.

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Page Caching using memcached
Database Caching 8/27 queries in 0.020 seconds using memcached
Content Delivery Network via 

Served from: psdtutsplus.com @ 2009-11-21 11:15:11 -->