Late Night Rabbit

Simple Organism

Trim the CSS

Before we move on, this is a good time to clean up the mess we've made with our document. No, it's not your fault. I intentionally coded the CSS without any structure in order to show that if we kept going, this document could get quite confusing to read not only for the author but for any other developers that need to use it. The first thing we need to do is cut all the code that is between the open and closing stylesheet tags in the document head.

Before (Note: cut all the code in black)...

<head>
    <title></title>
    <style type="text/css>
    body {text-align: center;margin: 10px;background-color: #20323C;background-image: url(body.gif)}
    #container {width: 760px; text-align: left;margin: auto;border: 1px #000000 solid;background: #ffffff}
    #masthead {background-color: #20323C;background-image: url(masthead.gif);height: 100px}
    #main-menu { }
    #page-contents {padding: 14px;}
    #footer {padding: 20px;background-color: #9F2513;background-image: url(footer.gif);text-align: center;}
    h1 {margin: 0;color: #ffffff;text-align: center;font-size: 30px;font-family: trebuchet ms, sans-serif;font-weight: normal;padding-top: 34px
    h2 {font-size: 22px; font-family: trebuchet ms, sans-serif;font-weight: normal}
    p {font-weight: 12px;font-family: arial, sans-serif}
    .text-small {font-size: 10px;font-family:arial, sans-serif;color: #ffffff;}

    </style>
</head>

And after...

<head>
    <title></title>
    <style type="text/css>
    </style>

</head>

Now take the code we just cut from our document and paste it into a new document. Save this document in the same folder that the "index.html" file resides and name it "default.css".

Next, we need to replace the <style> tags with a line of code that calls the external CSS file into our document. (Note: delete the code in black)

<head>
    <title></title>
    <style type="text/css>
    </style>

</head>

Replace the code we just removed with...

<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="default.css">
</head>

Awesome, now we should have two documents named "index.html" and "default.css" in a single folder. Also, make sure all the images we've been using are in the same folder with these documents. If you lost track, feel free to download the complete folder.

Alright, now let's trim down this CSS using some shorthand properties. Instead of going one by one with this, I'll just give examples and the final folder to download. It's pretty straighforward so I doubt you'll have any trouble with this. CSS shorthand is simply grouping related properties like font-size, font-weight, font-family into one line. We'll take on the h1 selector for example. Right now it should look like this...

h1 {margin: 0;color: #ffffff;text-align: center;font-size: 30px;font-family: trebuchet ms, sans-serif;font-weight: normal;padding-top: 34px

That's a mouthful, so to trim all the font properties using shorthand to...

h1 {margin: 0;color: #ffffff;text-align: center;font: normal 30px trebuchet ms, sans-serif;padding-top: 34px

Easy right? Just make sure you keep the values in that particular order or it may not render correctly on some browsers.

How about the margins and padding? As you see there's only one value for the top padding these in our h1 selector. What if we wanted different padding for the top, right, bottom and left of the h1. It would look like this:

h1 {padding-top: 34px;padding-right: 22px;padding-bottom: 14px;padding-left: 3px}

We can change that to be simplier as well. The values are in clockwise order starting from top (top, right, bottom, left). So the shorthand will look like this:

h1 {padding: 34px 22px 14px 3px}

It's getting cleaner right? The last one we'll take a crack at is the "background" properties. So here's how it goes using the body selector...

We go from this...

body {background-color: #20323C;background-image: url(body.gif)}

To this...

body {background: #20323C;url(body.gif)}

If you did your homework and checked out background positioning you could go from this...

body {background-color: #20323C;background-image: url(body.gif);background-position: top left;background-repeat: repeat-x}

To this...

body {background: #20323C url(body.gif) top left repeat-x}

Hexidecimal color values can also be shortened. So if you have the value #ffffff, it can be shortened to #fff. Another example is #99ccff can be shortened to #9cf. You get the idea. Inconsistent values like #20323C can be shortened as well but I've never found it very necessary since you'll use RGB instead of hexidecimal colors. If the value doesn't follow the #AABBCC pattern, just use the full value. You'll avoid more headaches than you need.

Now we're starting to clean house. Still there are those code nazis out there whining that we ain't clean enough. Stop crying, here's a tissue. You first want to try and be consistent with the order of your CSS selectors. So either create your own order or feel free to use the one I feel comfortable with...

selector {margin: value;padding: value;border: value;background: value;font: value;color: value;text-align: value;width: value;height: value}

Obviously you're not going to have use all of these properties, it's just an example. Use only what's necessary.

If you thought that wasn't anal enough I'll take it a step farther. Time to give each property with values it's own line to make it more readable. I also like to add the semi-colon (;) to the end of every property: value no matter if it's not the last one used. That way, when I have to add more of these to the selector. I won't have to remember to add a semicolumn to the preceeding value. If I didn't mention it already, all property: value must be separated by a semicolon property: value;property: value; but I thought you knew that already.

selector
{
margin: value;
padding: value;
border: value;
background: value;
font: value;
color: value;
text-align: value;
width: value;
height: value;
}

That should do it, but no! Coders love their heirarchy in CSS as well as HTML so it's time to add some indents. After the selector, "tab" one space in with your text editor and add the curly brace ({). Everything that follows before the next selector should all be tabbed in once ending in another tabbed in curly brace (}).

selector
	{
	margin: value;
	padding: value;
	border: value;
	background: value;
	font: value;
	color: value;
	text-align: value;
	width: value;
	height: value;

	}

selector
	{
	margin: value;
	padding: value;
	border: value;
	background: value;
	font: value;
	color: value;
	text-align: value;
	width: value;
	height: value;
	}

Righty-O! You think you're out of the woods but oh hell no. This clean CSS coding went even further in the past few years with commenting the code. Not only was commenting up for discussion but the format and indentation as well. Personally I dig it, but it's up to you whether or not to use it. Everything under the comment should be tabbed in once more making the properties tabbed in twice. Here's how it looks:

/* YOUR COMMENT
----------------------------------------------- */

	selector
		{
		margin: value;
		padding: value;
		border: value;
		background: value;
		font: value;
		color: value;
		text-align: value;
		width: value;
		height: value;
		}

If you're still awake I give you a ton of credit. Trust me I know this is the boring part but in the end you'll thank me for it. Feel free to double-check your web page and download the sample.

A great resource on squeaky clean CSS can be found at:
http://www.huddletogether.com/2006/02/16/practical-web-development-tips/