
A Guide to Cleaner and Faster Coding
As a web developer I am always looking for ways to speed up my workflow. One of the simplest ways to speed up and clean up your coding is to learn CSS Shorthand. In order to fully grasp shorthand, it is helpful to already have a decent knowledge of CSS. I have put together a simple guide to bring you into the world of CSS Shorthand.
CSS Syntax – The Basics
The basic CSS syntax contains a minimum of three parts, a selector, a property and a value
/* Basic Syntax */
selector { property: value }
/* Actual Example */
p { color: #444444; }
Background Property – The Long Way
There are five properties for the background property, but coding all these out separately is a waste of time and space.
#example {
background-color: #cccccc;
background-image: url(logo.png);
background-repeat: no-repeat;
background-position: 10px 10px;
background-attachment: scroll;
}
Background Property – The Short Way
Combining the five properties into one declaration will speed up your code and it will take up less space in your stylesheet.
#example { background: #ccc url(logo.png) no-repeat 0px 0px fixed; }
Border Property – The Long Way
There are three properties for the border property which consist of the width of the border, the style and the color.
#example {
border-width: 2px;
border-style: solid;
border-color: #333;
}
Border Property – The Short Way
Combining these three attributes is one of the easiest to remember in my opinion, and since I use a lot of borders it saves me a lot of valuable time.
#example {
border: 1px solid #333;
}
Margin and Padding – The Long Way
Margin and padding have four possible properties each.
/* Margin Example */
#example {
margin-top: 5px;
margin-right: 10px;
margin-bottom: 10px;
margin-left: 5px
}
/* Padding Example */
#example {
padding-top: 5px;
padding-right: 10px;
padding-bottom: 10px;
padding-left: 5px
}
Margin and Padding – The Short Way
It is easy to use shorthand for margin and padding. All you have to remember is think clockwise just like a clock. Start with top, then the right, then the bottom, and lastly the left.
/* Margin Example */
#example {
margin: 5px 10px 10px 5px;
}
/* Padding Example */
#example {
padding: 5px 10px 10px 5px;
}
There is another way that padding and margin can be written and that is with just two values, the first value being top and bottom, and the second value being left and right.
/* Margin Example */
#example {
margin: 20px 0px;
}
/* Padding Example */
#example {
padding: 10px 20px;
}
Colors in General
Throughout this tutorial I have been been short handing the color property (I wonder if you noticed) and here is the correct syntax for that. Any color values such as #333333 (dark gray), can be abbreviated to #333, because all of the values are the same. If a value reads something like #2bb7da it cannot be abbreviated. Of course this is not a huge deal but when dealing with a big project every second counts.
/* Long Example */ color: #333333 /* Short Example */ color: #333;
That’s All Folks
Hopefully this guide has been helpful to you and will continue to be helpful in the future. I will be sure to update this article as technology changes and CSS3 ushers it’s way into the foreground! By the way subscribe, retweet, digg, stumble, or bump if you enjoyed this guide!


September 29th, 2009 at 6:27 pm
Nice tricks!
Do you know more of this kinde of “shortcuts” ?
Thanks
September 29th, 2009 at 7:05 pm
Color shorthand has a little more to it. The Color attribute hex is formatted #RRGGBB. where RR is the red code, GG is the green code and BB is the blue code. If all pairs of digits are the same (ie. #334455) It can be shorted in the format #RGB. So, they don’t have to be the same all the way across…. just in pairs.
#000088 can be shortened to #008, as #AA77DD can be shortenened to #A7D
BUT all pairs of digits must be the same. If there is even one one pair that isn’t the same, then it cannot be shortened.
$000093 can NOT be shortened to #0093, but must stay the way it is
September 29th, 2009 at 7:11 pm
Great beginner tutorial, just a couple things…
“Start with top, then the right, then the bottom, and lastly the top.” Should say “lastly the left”.
Margin and padding can be written several other ways:
// equivalent to margin: 10px 10px 10px 10px;
margin: 10px;
// equivalent to margin: 10px 5px 10px 5px;
margin: 10px 5px;
//equivalent to margin: 10px 5px 15px 5px;
margin: 10px 5px 15px;
September 29th, 2009 at 7:12 pm
@topher
I appreciate the clarification I added color in at the last minute and just didn’t go as deep as I would have liked. I will update the post with these techniques very soon. Thanks for visiting!
September 29th, 2009 at 7:16 pm
How about shorthand for the font property? That one is picky about the order but can save a lot of bytes.
September 29th, 2009 at 11:36 pm
[...] CSS Shorthand – A Guide to Cleaner and Faster Coding | Three Styles (tags: css tutorial) [...]
September 30th, 2009 at 7:05 am
Excellent…
September 30th, 2009 at 9:01 am
Hi. Great article! Thanks for sharing.
ps. please change this main font on the blog couse i can’t read it. Letters are so indistinct. Pls do something with it
September 30th, 2009 at 3:17 pm
FONT
– Short Way
#example{font-weight font-style font-variant font-size/line-height font-family}
or real life values
#example{bold italic small-caps 12px/1.5 georgia,”times new roman”,serif}
October 3rd, 2009 at 7:37 am
Great tips, thank you. Please provide more and more contents on this css tutorial
October 4th, 2009 at 2:05 pm
[...] 7. CSS Shorthand – A Guide to Cleaner and Faster Coding [...]
October 5th, 2009 at 7:29 am
Great tips, keep up the great work.
October 6th, 2009 at 9:27 am
Nice one… thanks this is also called as shorthand css
October 26th, 2009 at 6:27 pm
[...] Goto Tutorial… [...]
November 16th, 2009 at 6:38 pm
Useful info. for beginners. One quick note: you introduce the shortening of the color property within your discussion of background, before you’ve explained the color property. That could be confusing.
Five Minute Argument´s last blog ..BBC self-deprecation?
January 18th, 2010 at 12:13 pm
Great tutorial.
January 27th, 2010 at 4:59 pm
Muchas gracias por este tutorial, me fue de mucha ayuda.
Thks for this tutorial is was very helpful
=)