By Dustin Brewer
Using shorthand CSS can save some 1’s and 0’s in your stylesheets which add up if you have a busy web site. Just think, all those extra lines of codes taking up valuable bytes are adding up on that server. It may seem minimal at first but it does take up some space once you have enough sites on your server. Even if you don’t care that much about bandwidth or server stress- you are wasting valuable keystrokes by typing extra attributes and properties in your CSS.
Shorthand CSS is a productivity tool
It is a good habit to be in if you are using shorthand CSS, you can save a lot of time by combining attributes like font, border and background. I know those are the three I spend the most amount of time with. Especially with the background property. When you are not using shorthand CSS you are having to type out background-image, background-color, background-repeat, background-position, and so much more. Shorthand CSS will clean up your markup tremendously.
Using shorthand CSS on the background property
This is how your normal background element looks when you aren’t using shorthand, you will see how much of a difference using shorthand CSS will make on your stylesheet complexity.
Code: Select all
.class {
background-color: #ffffff;
background-image: url(mycss.jpg);
background-position: center top;
background-repeat: no-repeat;
}Code: Select all
.class {background: #fff url(mycss.jpg) no-repeat center top;}Using shorthand CSS on the font property
When using the font property in shorthand things can get a little confusing so you want to be sure you pay close attention to this section. You must put all of your attributes in order if you want to use font in the shorthand CSS method. The reason for this is that various browsers assign different defaults to different elements and because of this you can easily specify something like the font size and color and accidentally erase the bold on a strong tag or something equally as traumatic. Maybe not traumatic, but it could turn out a bit frustrating. Like that time you broke your mouse, accidentally.
Here is an example of using the font property the lengthy way.
Code: Select all
.class {
font-variant: normal;
font-style: uppercase;
font-weight: bold;
font-size: 12px;
line-height: 1.2em;
font-family: Arial, Helvetica, sans-serif;
}Here is an example using the font property the lengthy way, notice that the order in which it goes is font-style, font-variant, font-weight, font-size, line-height and then finally font-family.
Code: Select all
.class {normal uppercase bold 12px/1.2em Arial, Helvetica, sans-serif;}Using padding and margin in shorthand CSS
These two are really simple for the most part, most of the time you are going to be declaring a mix between the all 4 or just top/bottom and left/right. It saves a ton of time to not type out padding-right, padding-left, padding-top, padding-bottom and so on. Same goes for using margin, it is an unnecessary bunch of stuff that will make your whole stylesheet look like garbage.
The most basic rule for using padding and margin is to know that it goes clockwise around your browser starting at the top. So, for example the following will illustrate the difference between longhand and using padding and margin for shorthand.
Code: Select all
.class {
padding-top: 20px;
padding-right: 10px;
padding-bottom: 5px;
padding-left: 10px;
}Code: Select all
.class {padding: 20px 10px 5px 10px;}Code: Select all
.class {padding: 20px 10px 5px;}Using borders in shorthand CSS
The border for shorthand is a little different then the rest but can still be used much easier then the font property can. There are somethings that I wish the border property would do that it doesn’t but it is still efficient for what it is.
You can easily assign border for all sides or be more specific with border-top, border-left, border-right and border-bottom. It just depends on what you are doing for when you would use them.
This is an example of using the border property the long way, which takes quite a bit more work for less results.
Code: Select all
.class {
border-color: #000;
border-width: 1px;
border-style: solid;
}Code: Select all
.class {border: 1px solid #000;}Code: Select all
.class {border-bottom: 1px solid #000;}
Using list-style in CSS shorthandUsing the longer version of CSS for this style.
Code: Select all
ul li {
list-style: none;
list-style-position: inside;
list-style-image: url(myimage.jpg);
}Code: Select all
ul li {list-style: none inside url(myimage.jpg);}The end to my guide to CSS shorthand for your stylesheets!
When I started writing this I think I forgot how much you can really use shorthand, I write my articles in OpenOffice before I publish them and it is currently telling me that I am on page 5, nearly at the end of it. Before anyone goes crazy about the fact that you can use outline in shorthand, I know you can and I didn’t include it because it isn’t really supported much and it has even less functional use in web design. If it has a renewal worth my time I may add it but you pretty much use it identically to the way you use the border for shorthand CSS.
I know I have missed a few CSS Monday’s but I’m sure that this one (even though it is late) is well worth the missed ones. This has a ton of information that I’m sure would be helpful to seasoned designers and beginners alike. So definitely take your time to [url=\"http://del.icio.us/post?url=http://dustinbrewer.com/index.php?a=78\"]delicious bookmark[/url] this page and subscribe to my [url=\"http://feeds.feedburner.com/dustinbrewer\"]RSS feed[/url] if you would like to continue reading more web design news articles like this one.
About The Author
Dustin Brewer is a web and graphic designer with a passionate interest in everything that is technology. He currently is the webmaster and editor for [url=\"http://techfilter.net/\"]tech filter[/url], a technology news web site, as well as a writer for Gadgetell-- a similar web site about technology news with a strong focus on new gadgets.
[url=\"http://www.designnewz.com/2007/11/19/using-shorthand-css-in-your-cutups/\"]source[/url]


