Compressing and Optimizing CSS Code

As you know, optimizing your site’s response time is one of the most popular topics regarding website design and development. And one of the elements that can be optimized in a site is CSS. Let’s take a look and see what ways of optimizing and compressing CSS code are available.

For this purpose of showing the ways to optimize CSS code I picked up a CSS file from Dave Shea’s CSS Zen Garden. It was the best choice because the CSS code was somehow in the standard coding style way. So, based on the following CSS code we will try to see how can we improve our website performance by optimizing and compressing CSS code.

1. Standard CSS Code

The following CSS example is in a way like a standard way of writing CSS code. Keep in mind that this is just a fracture of a CSS file. Let’s see how it is the code that we will try to optimize.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
html {
	margin: 0;
	padding: 0;
	}
 
body {
	font: 75% georgia, sans-serif;
	line-height: 1.88889;
	color: #555753;
	background: #fff url(/001/blossoms.jpg) no-repeat bottom right;
	margin: 0;
	padding: 0;
	}
 
p {
	margin-top: 0;
	text-align: justify;
	}
 
h3 {
	font: italic normal 1.4em georgia, sans-serif;
	letter-spacing: 1px;
	margin-bottom: 0;
	color: #7D775C;
	}
 
a:link {
	font-weight: bold;
	text-decoration: none;
	color: #B7A5DF;
	}

***The test file that I will use has 3.39 KB. For this article purpose I will use CleanCSS.***

Pros: The advantage is that you can see very easy your code and you can identify really fast the selectors.
Cons: To code has many break lines and a lot of space is wasted. The size of this CSS code is big.

2. Basic Optimization

The first thing you can do to your CSS file is to remove the semicolon (;) placed after the last property in every selector. Also you can loose the indentation and the unnecessary space placed in front of the properties as well as the single space placed in front of the properties’ values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
html {
margin:0;
padding:0
}
 
body {
background:#fff url(/001/blossoms.jpg) no-repeat bottom right;
color:#555753;
font:75% georgia, sans-serif;
line-height:1.88889;
margin:0;
padding:0
}
 
p {
margin-top:0;
text-align:justify
}
 
h3 {
color:#7D775C;
font:italic normal 1.4em georgia, sans-serif;
letter-spacing:1px;
margin-bottom:0
}
 
a:link {
color:#B7A5DF;
font-weight:bold;
text-decoration:none
}

***New file size: 3.02 KB.***

Pros:An average decrease of size of 15%, that means the file will be loaded faster.
Cons:A minor decrease in readability but the code remains easy to read and find.

3. One Line per Selector

To optimize further the size of the CSS file, the user can make every selector declaration to be displayed in one line. In this case the depending on the file size, the gain can be significant.

1
2
3
4
5
6
7
html{margin:0;padding:0}
body{background:#fff url(/001/blossoms.jpg) no-repeat bottom right;color:#555753;
font:75% georgia, sans-serif;line-height:1.88889;margin:0;padding:0}
p{margin-top:0;text-align:justify}
h3{color:#7D775C;font:italic normal 1.4em georgia, sans-serif;letter-spacing:1px;
margin-bottom:0}
a:link{color:#B7A5DF;font-weight:bold;text-decoration:none}

***New file size: 2.57 KB.***

Pros:The decrease percentage is now at around 25%.
Cons:The readability was severely altered and not many are able to read these lines.

4. One Line CSS File

The next way is the way that I recommend to use it in production. This is how all the big names on the Internet use CSS. Also you will have to keep a clean and readable CSS file for development purpose.

1
html{margin:0;padding:0}body{background:#fff url(/001/blossoms.jpg) no-repeat...

New file size: 2.53 KB.

Pros:This is the most optimized CSS code form.
Cons:The readability is next to 0. Without “Find” capabilities in your code editor you won’t do much.

5. Guidelines for Optimizing CSS while coding

Next I will present some of the best guidelines that you can use in your coding process to optimize your CSS code.

1. Use space only when you need it.
2. Use shorthand CSS.
3. Don’t put semicolon after the last property in a selector.
4. Use ’700′ instead of ‘bold’.
5. Use ’400′ instead of ‘normal’.
6. Use 0 instead of ’0px’ or ’0em’
7. Merge selectors with the same properties

This article was created to explain the ways you can optimize a CSS file. In this test, a great help was the CleanCSS optimizer, which is an automated CSS service that can optimize your code based on different options. As you can see, you can decrease your CSS file size with 25%-30%. That is because our sample code was well written. For bad CSS coding it is possible for the decrease percentage to be higher.

So, code CSS well, choose your optimization type and follow the standards. Cheers!

Sponsors

Community Links

  • Create the Toyota logoHi there. In this tutorial you can learn to create the Toyota logo. It is all about circles and the Pathfinder panel. You will have to create a series of circles of exact dimensions. Regarding that,the grid and the snap to grid option will ease your work.
  • 60+ Creative And Well Designed PagesWhen doing my research for 404 error pages, I was surprised to see that three quarters of the websites I visited did not have custom 404 error pages.
  • Design an animated banner in PhotoshopIn this tutorial I will show you how to use Photoshop to create an animated leaderboard banner.
  • Top 10 Websites To Brush Up Your jQuery SkillsTo brush up your skills in jQuery, we present you Top 10 Websites For Free jQuery Tutorials that have been providing some cool demos and step by step guidelines, such that a beginner can master his skills of jQuery easily
  • Top 8 Principles of Effective Website DesigningEverything is guided by certain set of standards, rules and regulations. If the task is not driven by some guidelines, then it could face difficulties for its successful completion.
  • 61 Incredible Logo Designs #3Here’s yet another roundup of amazing logo designs. This is a series that we do frequently, so make sure to check out the past logo articles.
  • 20+ Amazing Product PSD files found on DeviantARTThese are a selection of over twenty PSD files which I have found over on DeviantART, specifically of various electronic products.
  • Create a documents icon in IllustratorHi there. In the next tutorial you’ll learn to create a pretty common icon. To reach this result you need to know how to use the Rectangle Tool, the Pen Tool or the Type Tool.
  • 10 Downing Tweets10 Downing Tweets is a Flash and Twitter based website created by twentysix Leeds. The site includes a twitter vote, a daily poll and an insight on the twitter noise the election is receiving. Will the final results on our site echo who gets elected?
  • 20 Clever Futuristic Free FontsFuturistic fonts were always one of my favorite font styles. I was never that much into grunge and similar styles. I rather keep it clean, but still cool looking. If you like this style, take a look at the collection of 20 free futuristic fonts I put toge