A few good practices for optimising your CSS

Written by That Web Guy on 28th December 2009. 2 comments

A few good practices for optimising your CSS
  • Avant Innovations
  • Rusty Lime
  • Advertise Here

Like any programming language, CSS can be used in many ways while still achieving the same result. And although sometimes there isn't necessarily just a single 'correct' way to achieve a certain goal, one thing we can all agree on is that clean optimal code is always better than bloated code.

With this in mind I thought I'd share some of the techniques that if you aren't already practising them, maybe this article will inspire you to start.

Use CSS Shorthand

This is the art of combining all the properties of an element into a single line. Some examples:

h1 { 
font-weight: bold;
font-size: 3em;
line-height: 3.2em;
font-family: Arial;
}

...can be shortened to:

 h1 { font: bold 3em/3.2em Arial }

Likewise,

body {
background-image:url(myimage.png);
background-position:top;
background-repeat:no-repeat;
background-color:#ffffff;
}

...can be shortened to:

body { background:#fff url(myimage.png) no-repeat top; }

Tip: Hex colours can be shortened by dividing them in 'half'. Examples:

#cccccc can be #ccc
#ff0000 can be #f00

Padding and margins can also be shortened:

#header {
padding-top:10px;
padding-right:20px;
padding-bottom:30px;
padding-left:40px;
margin-top:10px;
margin:right:10px;
margin-bottom:10px;
margin-left:10px;
}

...in this manner:

#header {
padding: 10px 20px 30px 40px; /* Just remember the order is clockwise - top right bottom left */
margin:10px;
}

Grouping

When repeating the same CSS for multiple classes you can combine them into a single group using comma separated declarations. Example:

.div01 {
background:#fff url(myimage.png) no-repeat top;
}
.div02 {
background:#fff url(myimage.png) no-repeat top;
}
.div03 {
background:#fff url(myimage.png) no-repeat top;
}

...can be shortened to:

.div01, .div02, .div03 {
background:#fff url(myimage.png) no-repeat top;
}

Serve up an optimised version of your style sheet

There are a stack of good CSS compressors online and they are very good at doing the same thing - changing your style sheet to be as optimal as possible. In some cases I've seen style sheets reduced by nearly 50% of the original size.

The only problem is that optimised style sheets can be insanely difficult to read, especially once the white space is removed. But if you want your server side site to be as slick as possible it's OK to serve up an optimised style sheet and keep your hand coded version local for easier editing.

When you're ready to upload your modified CSS, just remember to run it through a CSS compressor again first. Here's a few:

If anyone has any other tips for optimising CSS, feel free to share them below.

Is this worth sharing?

That Web Guy

About That Web Guy

That Web Guy (Mikey to his friends) is a veteran web designer based in Perth, Western Australia, and currently Senior Designer at Titan Interactive. Mikey's work has been featured on many CSS showcase web sites and when he's not XHTML'ing or messing around in Photoshop, he can usually be found preaching web standards evangelism upon unsuspecting victims.

Feel free to send That Web Guy a message some time, follow him on Twitter, or make a donation.

Comments

Jasmine

Jasmine

What a cool way to do font styles.

Another thing I do is padding: 20px 10px instead of padding: 20px 10px 20px 10px;

Monday 28th December 2009 | 10:06 AM Reply Comment URL Profile Back to top

That Web Guy

That Web Guy

Responding to this comment by Jasmine

Another good shorthand method thanks Jasmine.

Monday 28th December 2009 | 10:15 AM Reply Comment URL Profile Back to top

FYI: You are currently not logged in. It's cool though - you can still comment. But only members get a profile page, access to the download section and they can pimp their own web sites. Feel free to register or Login now!

Your name:

Your comments:

Note: HTML tags are automatically stripped from comments.

Are you human?
Turing

Sorry, I have to ask. So what sort of animal is this? (Hint: you don't have to be perfectly specific)

Back to top

Login to That Web Guy Blog

Login

Not registered? | Forgot your Password? Cancel Login