By Lee Sykes
March 2006
This article is intended for people who are familiar and comfortable with CSS.
We are going to look at some of the techniques that are available within CSS to minimise the size of our CSS stylesheets and to write more efficient code.
Why?
Reduce your file size:
Granted this is usually by a small amount, but every byte counts, especially for users with a low speed internet connection. It also benefits busy high bandwidth sites by reducing the amount of bandwidth used per page.
Speed of coding:
Speed up the time of entering your CSS code by reducing four lines of code to one line.
Easier to read and edit:
Once you are familiar with the shorthand formatting you will find your documents are much easier to read and quick to edit.
Shorthand Examples:
Here are some of the common shorthand formats:
font: [style] [variant] [weight] size/[line-height] family
The values in square brackets are optional. The first three values can be specified in any order.
Eg:
font-style: normal;
font-weight: bold;
font-size: 12px;
line-height: 1.2em;font-family: Tahoma, Arial, Helvetica, sans-serif;
Becomes:
font: normal bold 12px/1.2em Tahoma, Arial, Helvetica, sans-serif;
Font shorthand W3C specifications
background: (Can specify any of the background elements in any order, separated by spaces)
Eg:
background-color: #333;
background-image: url(image.gif);
background-position: 0 15px;
background-repeat: no-repeat;
Becomes:
background: #333 url(image.gif) 0 15px no-repeat;
Background W3C specifications
margin:
(top, right, bottom, left) (See the clock tip below) or
(top and bottom, left and right) or
(top, left and right, bottom) or
(one value for all four sides)
Eg:
margin-top: 2px;
margin-right: 3px;
margin-bottom: 4px;
margin-left: 5px;
Becomes:
margin: 2px 3px 4px 5px;
Eg:
margin-top: 5px;
margin-right: 10px;
margin-bottom: 5px;
margin-left: 10px;
Becomes:
margin: 5px 10px;
Eg:
margin-top: 5px;
margin-right: 7px;
margin-bottom: 10px;
margin-left: 7px;
Becomes:
margin: 5px 7px 10px;
Eg:
margin-top: 5px;
margin-right: 5px;
margin-bottom: 5px;
margin-left: 5px
Becomes:
margin: 5px;
Margin W3C specifications
padding:
(top, right, bottom, left) (See the clock tip below) or
(top and bottom, left and right) or
(top, left and right, bottom) or
(one value for all four sides)
Padding works in the same way as the margin examples above.
Padding W3C specifications
border:
The three elements below work in exactly the same way as the padding and margin shorthand methods:
border-width W3C specifications
border-color W3C specifications
border-style W3C specifications
border: [style] [width] [color]
The values in square brackets are optional and can be specified in any order.
Eg:
border-top: solid black 2px;
border-right: solid black 2px;
border-bottom: solid black 2px;
border-left: solid black 2px;
Becomes:
border: solid black 2px;
If for instance you wish to use the same style and color but specify a different width for each side you can use:
border: solid black 2px;border-width: 5px 7px 3px 0;
Note: Within the CSS values, each value should be separated by a space and each value should have a unit of measurement assigned to it, eg: px, em, %, etc.
Notice that the value 0 does not have a unit of measurement. The reason for this is that no matter which type of measurement you use, eg: px, em, % etc. a zero unit is a zero amount in any unit.
Within the W3C specifications it is optional to include the measurement for a unit of zero, we have chosen to remove px from the zero.
Border W3C specifications
The Clock
A tip for remembering the order of the box sides for the margin, padding, and borders properties is to imagine a clock face.
12 o’clock – Top of the box – 1st value
3 o’clock – Right side of the box – 2nd value
6 o’clock – Bottom of the box – 3rd value
9 o’clock – Left side of the box – 4th value
Multiple Declarations of CSS values
It’s possible to declare the same CSS values for several classes at the same time, simply separate each class with a comma.
Eg.
.one, .two, .three
{
font: bold 12px Tahoma, Arial, Helvetica;
}
.one
{
color: Navy;
}
.two
{
color: Gray;
}
.three
{
color: Green;
}
Here we have specified the same font for classes one, two, three and then specified a different color for each class separately.
Colours Shorthand:
If you use Hex values for specifying your colours within your website, eg:
#ffffff you can also use shorthand values.
If the Hex value consists of three pairs, eg:
#ff00ff, you can shorten this to a single character for each pair, eg:
#f0f
Here are some further examples:
#ffffff #fff
#000000 #000
#ff0000 #f00
#c0c0c0 #c0c0c0
#808000 #808000
Conclusion:
We have covered a number of ways in which you can minimise and create efficient CSS code. These techniques will help you to reduce your file sizes while also making it easy for you to read and edit your code.