CSS Optimization Tips and Techniques

your source of informations about css tips and techniques


24

CSS tips

no. of css tips

Last tip added

29 Nov 2011




Before we start

optimizing

The whole point of optimizing CSS is to make you CSS file smaller and your HTTP requests at minimum. That means less characters in a file that you can possibly make it and using some techniques for reducing HTTP requests. Here are some examples how you can optimize your CSS.

Note: These tips and techniques for CSS Optimization are not sorted in any way.

1. Aggregate Multiple Properties

css optimization tips and techniques

When coding CSS you can combine multiple properties.

For example, instead of:

  • margin-top: 15px;
  • margin-right: 5px;
  • margin-bottom: 10px;
  • margin-left: 0px;

You can use:

  • margin: 15px 5px 10px 0;

Also this technique you can use for background, font, padding etc.

Note: Every value that is 0 (zero), you can write it without any units like px, pt, em or %.

2. Group Common Styles

css optimization tips and techniques

This is maybe one of the best technique that will save you a loot of space if you have big CSS styles. Let's say that you have next example:

  • h1{
  • font-size: 16px;
  • color: #FFF;
  • font-family: Arial, Helvetica, sans-serif;
  • margin: 0px;
  • }
  • h2{
  • font-size: 12px;
  • color: #FFF;
  • font-family: Arial, Helvetica, sans-serif;
  • margin: 0px;
  • }

Instead of writing the same properties for the different elements you can write something like this:

  • h1,h2{
  • color: #FFF;
  • font-family: Arial, Helvetica, sans-serif;
  • margin: 0px;
  • }
  • h1{
  • font-size: 16px;
  • }
  • h2{
  • font-size: 12px;
  • }

3. Use Short Hexadecimal values

css optimization tips and techniques

Creating beautiful websites wouldn't be posible without colors. These are some examples on how you can replace existing hexadecimal values with shortened version of the same.

  • #000000 -> #000
    #ffffff -> #fff
    #cccccc -> #ccc
    #666666 -> #666
    #999999 -> #999
    #ff0000 -> red

4. Make CSS external

css optimization tips and techniques

While putting your CSS in external file you optimize your HTML code and the best part is that this way CSS gets cached, otherwise your browser will download CSS styles every time.

Note: Don't use inline CSS!

5. Combined all CSS files into single file

css optimization tips and techniques

This is one of the examples for reducing HTTP request. If you have more that one CSS file it is recommended that you put all your CSS into one file.

Before:

  • < link rel="stylesheet" type="text/css" href="style.css" / >
    < link rel="stylesheet" type="text/css" href="slideshow.css" / >
    < link rel="stylesheet" type="text/css" href="reset.css" / >

After:

  • < link rel="stylesheet" type="text/css" href="style.css" / >

6. Strip white spaces (caution)

css optimization tips and techniques

This is also one of the good tips but this could be dangerous. A loot of white spaces are unnecessary and they don't have any purpose.

For this optimization I sugest that you use some software like our CSS Optimizer if you are not sure which white spaces are safe to remove.

7. Strip new lines

css optimization tips and techniques

New Lines are also one of the big problem when it comes to space.

This is two way street. Striping new lines leaves you with poor format but saves you a lot of space.

8. Put CSS in the HEAD of your document

css optimization tips and techniques

By Yahoo putting CSS in the HEAD of your document makes pages appear to be loading faster.

Also, putting stylesheet in HEAD can avoid two problems:

- the blank white screen and
- flash of unstyled content.

9. Don't use big, ugly comments

css optimization tips and techniques

We all like when we see beautiful code format but you have to ask yourself: "Is it worth it?".

Instead of:

  • /***********************************/
  • /************ RESET CSS ***********/
  • /***********************************/

use:

  • /*RESET CSS*/
Note: The best way is not to use comments at all if that is possible.

10. Avoid attaching style to elements

css optimization tips and techniques

Always specify styles in CSS. Don't add styles directly to elements of an document.

Instead of:

  • < p style="font-size: 14pt ;font-family: Arial; text-decoration: underline;">Home< /p>

use:

  • < p class="myClass">Home< /p>

11. Use CSS Sprites

css optimization tips and techniques

We all know that using a lot of images from CSS is a common thing. With advantage of CSS Sprites we can reduce the number of HTTP requests. Combining more images into one image is a great way to save bandwith.

Showing segments of images are done through background property.

12. Small but valuable information

css optimization tips and techniques

You may not know this, although it is small change it still count.

Instead of closing style element like this:

  • color:#CCC; }

close it like this:

  • color:#CCC }

Without ";".

13. Native values

css optimization tips and techniques

When coding a CSS file try to avoid using property values that are native to the specific element. Like on this next example:

  • h1 { font-weight: bold; }

14. Link vs. @import

css optimization tips and techniques

A detailed testing and analysis showed that using @import can slow your website performance.

Note: Link CSS, dont't use @import.

15. CSS before image

css optimization tips and techniques

Avoid using pictures if you can replace them with pure CSS.

16. Only reset what you use

css optimization tips and techniques

When you create a CSS reset, add only those elements that you actualy use for the website. This will save a few bytes.

17. Microsoft proprietary CSS filters

css optimization tips and techniques

Don't use them if it isn't really necessary. These are tightly connected with Direct X and users with older machines will have some issues when it comes to OS or video card.

18. Remove unused classes

css optimization tips and techniques

Sometimes during development there is more css classes than we actually need. This often happens as a result of changing the design and so on. T ry to remove the classes that you do not need.

19. Avoid using inline styles

css optimization tips and techniques

Note: Arguable.

20. Extra zeros

css optimization tips and techniques

When you write numerical values, do not include any extra zeros. Instead:

  • padding: 0.1em;
  • margin: 10.0em;

Use:

  • padding: .1em;
  • margin: 10.em;

21. Validate your CSS

Why validate?

22. If horizontal and vertical values differ

css optimization tips and techniques

If horizontal and vertical values differ, like this:

  • padding: 4px 8px 4px 8px;

You can replace it with:

  • padding: 4px 8px;

23. IE specific styles

css optimization tips and techniques

If you have version specific styles for IE, use conditional comments to load these as external stylesheets like this:

  • < ! - -[if IE 6]>
  • Special instructions for IE 6 here
  • < ![endif]-->

Note: Remove unnecessary white space from the code that we added for correct viewing purposes!

24. CSS vs table-based layouts

css optimization tips and techniques

There are many reasons why is CSS superior to tables but here, we are interested in only one. Speed.

Table-based layouts have big amount of junk markup so try to avoid using tables when you don't need them.

More css tips and techniqes on the way

We ain't stopping

"Visit us every day for some new exciting informations.

All the best."

- Lukic Milos