Subscribe to RSS

Detailed guide to using shorthand CSS in your cutups

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.

.class {
background-color: #ffffff;
background-image: url(myCSS.jpg);
background-position: center top;
background-repeat: no-repeat;
}

As you can see your CSS can get quite lengthy when you display every element out like this. Now lets take a look at this exact same CSS statement using the shorthand method.

.class {background: #fff url(myCSS.jpg) no-repeat center top;}

See how much of a difference it can make? It is easy to switch over to the shorthand CSS methods. Especially when you are using the background property, the reason for this is that it doesn’t require you to put the attributes in any order so you can mix and match without having to worry what place which CSS attribute goes.

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.

.class {
font-variant: normal;
font-style: uppercase;
font-weight: bold;
font-size: 12px;
line-height: 1.2em;
font-family: Arial, Helvetica, sans-serif;
}

Now as you can see it can all get very lengthy to use the font element the regular way, it is safe to say however that most of the time you really aren’t going to be declaring all of these things on every element but it could easily come up. I know that I usually use line-height, font-family and font-size on pretty much everything so there are times when I actually don’t use shorthand for the font tag because of the confusion it can present in simple matters.

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.

.class {normal uppercase bold 12px/1.2em Arial, Helvetica, sans-serif;}

I don’t generally use the font property in shorthand to be quite honest, but if you find yourself writing too much when it comes to fonts this information can come in handy. Especially knowing the order to list when using the font property in shorthand CSS.

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.

.class {
padding-top: 20px;
padding-right: 10px;
padding-bottom: 5px;
padding-left: 10px;
}

Next I will display a couple of ways to do this exact same thing using shorthand CSS. The first example is the most applicable for this specific example.

.class {padding: 20px 10px 5px 10px;}

Then you could also do the following and it would work just the same but would best be left for other instances or preference.

.class {padding: 20px 10px 5px;}

You can easily group duplicate sizes in margin and padding. They both work identical except for their application. Just for clarification, padding is inside of the box and margin is outside of the box.

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.

.class {
border-color: #000;
border-width: 1px;
border-style: solid;
}

The following example is simply using the border for all sides of your box, you must specific a pixel size, a border type and a border color for this to work properly. With the border in CSS shorthand you can use them in any order. So it is very easy to implement once you have built the habbit.

.class {border: 1px solid #000;}

You can also only assign it to a specific side if you would like, as I had mentioned above.

.class {border-bottom: 1px solid #000;}

Using list-style in CSS shorthand

This method of using shorthand CSS isn’t quite as popular as some of the others but I do tend to use it sparingly. I often forget it because it isn’t often that I want to use all of the properties available, but it can be handy.

Using the longer version of CSS for this style.

ul li {
list-style: none;
list-style-position: inside;
list-style-image: url(myimage.jpg);
}

Now to simplify this CSS example, we would do the following.

ul li {list-style: none inside url(myimage.jpg);}

Thats it for list-style, it is pretty basic and only comes in handy when you are doing navigation. I often forget about it but after writing all of this I’m sure I will remember to use it more often in my stylesheets.

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 delicious bookmark this page and subscribe to my RSS feed if you would like to continue reading more web design news articles like this one.

Popularity: 31%

Whats Next?

  • Save to Delicious! Save the page

15 Responses to “Detailed guide to using shorthand CSS in your cutups”

  1. gangadhar Says:

    pictorial may be helpful to students.

  2. Jacob Says:

    Fantastic article, I love all of the CSS help this sites offers for beginning web designers.

  3. Chihn Says:

    That doesn’t make any sense, not only is it not possible to automatically generate your CSS because every design is going to be different but you are just making shit up.

    Sounds like you are using hard-coded tables if you don’t have to use CSS.

  4. she Says:

    i dont like the shorthand notation
    but in fact, i dont even use hard-coded css anymore

    I use a pseudo language that sets the properties and generates the css as needed.

  5. Dustin Brewer Says:

    Thank you Niall, I'm very pleased with how it turned out. There are still a few things I plan on tweaking and some features I will be adding on soon. Whenever I can find the time, that is.

  6. Niall Doherty Says:

    I had never seen the list-style shorthand before. Thanks for sharing. Loving your site design, by the way.

  7. Piotr Says:

    Thanks for that! Nice reading!

  8. Dustin Brewer Says:

    Thanks for catching that, I wrote that article in one sitting not thinking it would be that long. I should have gone back over it a little more carefully.

    Everything is going well, looks like you redesigned your site. It looks good, can't wait to see you actually start blogging, haha.

  9. JZ Says:

    You’ve confused a couple of properties in the font section. This block:

    .class {
    font-variant: normal;
    font-style: uppercase;
    font-weight: bold;
    font-size: 12px;
    line-height: 1.2em;
    font-family: Arial, Helvetica, sans-serif;
    }

    Should be more like this:


    .class {
    font-variant: normal;
    text-transform: uppercase;
    font-style: italic;
    font-weight: bold;
    font-size: 12px;
    line-height: 1.2em;
    font-family: Arial, Helvetica, sans-serif;
    }

    Here are a couple of links that go into more detail:
    Font-style property

    Text-transform property

    Good article, though. Hope things are going well for you.

  10. Jacob Says:

    Great guide, lengthy.

  11. Ben Krotz Says:

    Awesome guide, bookmarked!

  12. Edward Turtle Says:

    Very handy (Pressing Ctrl+D) Thanks. I didn’t know about the background one but I do now.

  13. Jordan Says:

    Thanks for the helpful css guide, I didn’t realize that you could do this with css either. this will save me a ton of time trying to type out all of the long selectors that i type. it will help a lot with updating also.

  14. suraj naik Says:

    for border on only 3 sides you can us this

    .class {border:solid #FF0000; border-width:0 1px 1px;}

    instead of writing

    .class {border-left:1px solid #FF0000; border-right:1px solid #FF0000; border-bottom:1px solid #FF0000;}

    for border left, right & bottom

  15. flowerBeauty Says:

    nice 2 share, thanks !

Leave a Reply


Recent comments

I’ve been on a vacation, normality will happen next week

On August 28, 2008, holiday travel wrote:

well this is useful… (at least for me) very thanks ————&# 8212;———R 12;—–...

Top 10 search engine optimization techniques

On August 26, 2008, ибп wrote:

А что, неплохо!

Styling the first post of your Wordpress blog

On August 21, 2008, cheap website design wrote:

good point you have in there! thanks for sharing with us

CSS Hack:Getting Safari to behave

On August 21, 2008, Glass wrote:

You can also target Safari with the following: body:first-of-type .class {} That is: just prepend css the rule with body:first-of-type. Simple.

Styling the first post of your Wordpress blog

On August 20, 2008, jeeremie wrote:

Hi, This solution didn’t work for me. Instead, I use this one »