Feature
Post

Category
Design


How’s your CSS Shorthand?

As a child I was always amazed by people who could write in shorthand. Pages of what looked like mere squiggles to me, which were then transcribed perfectly into proper english- it was something I thought I’d like to learn when I grew up.

Fast forward some twenty years, of course- and now I wonder whether anyone uses traditional shorthand anymore. What I do use- almost on a daily basis, being a web designer- is CSS shorthand. Like traditional shorthand, CSS shorthand saves time. Which is why I want to post about it today.

For a long time, I didn’t use CSS shorthand. I thought it was confusing to learn, and that sticking to the old way of writing each line out was better. I was wrong, of course. For example, here’s the “old way” of defining an element’s background:

background-color: #fff;
background-image: url(images/bg.gif);
background-repeat: repeat-y;
background-position: top left;
background-attachment: scroll;

And here it is in CSS shorthand:

background: #fff url(images/bg.gif) repeat-y top left scroll;

See how much space that saves? Another place I love using CSS shorthand is when I define borders. Let’s say I want box with different border thicknesses. In the old days, I would have done something like this:

border-top: 1px solid #ccc;
border-right: 2px solid #ccc;
border-bottom: 1px solid #ccc;
border-left: 4px solid #ccc;

With shorthand, all I have to do is:

border:1px solid #ccc;
border-width:1px 2px 1px 4px;

Yes, you will have to learn the various properties you can use, and their default values. But if, like me, you spend a lot of time designing with CSS, it’ll be worth it. I promise.

For more depth into CSS shorthand, I highly recommend Dustin Diaz’s guide. Do you use CSS shorthand in your code?


  1. By Shane posted on June 3, 2009 at 8:39 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Isn’t the standard background practice to put horizontal position before vertical?

  2. By Bryan Buchs posted on June 3, 2009 at 9:13 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    @Shane – if you use left/right/top/bottom, the order is irrelevant, but pixel values must be specified as position: left top

  3. By Tom posted on June 4, 2009 at 8:36 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Another great short hand is available for margin and padding.
    margin-top: 10px;
    margin-right: 5px;
    margin-bottom: 20px;
    margin-right: 12px;

    in short hand: margin: 10px 5px 20px 12px;

  4. By web2000 posted on June 4, 2009 at 1:03 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Hey, great post Lorraine! Also remember

    TRouBLe

    when declaring margins/borders/padding etc.. in one property:

    { Top Right Bottom Left }
    {10px 5px 20px 12px }

  5. By David Perel posted on June 4, 2009 at 4:53 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    What is the short hand order for font styles? I always get it wrong.

  6. By Lorraine Nepomuceno posted on June 5, 2009 at 9:47 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Hi David, font styles are:
    { font-style font-variant font-weight font-size line-height font-family }

    for example
    element {italic normal bold 11px 145% verdana, arial, sans-serif;}

  7. By David Perel posted on June 5, 2009 at 9:52 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Thanks Lorraine! I have always got the order wrong and always had to revert to the long way of doing it (which is like a sin in todays CSS practice).

    Appreciate it.

  8. By Scott Radcliff posted on June 5, 2009 at 5:41 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    David, if you haven’t yet you might want to use the w3schools website for css styles. They are a great resource, I use them all the time.

    CSS – Font Shorthand

  9. By Pieter de Jong posted on June 6, 2009 at 8:00 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    I write in shorthand all the time, i use it the most when i need some killer margin :P

  10. By VPS Web Hosting posted on June 7, 2009 at 2:22 am
    Want an avatar? Get a gravatar! • You can link to this comment

    That’s great… I need to work on my shorthand, sometimes I do it and sometimes i just skip it. It would be nice if DreamWeaver did a better job of suggesting it… but then again maybe I shouldn’t be using that as a crutch.

  11. By Chris Valleskey posted on June 11, 2009 at 4:45 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Although the order when using left/right, top/bottom, and center doesn’t change the appearance, I have found that it is good practice to have the horizontal value (I.E. left and right) before the vertical value.

    Also, if you have a border with different thicknesses on each side and wanted to save a little more space, you could remove “1px” from the first line. :)

  12. By Victoria Blount posted on June 12, 2009 at 8:01 am
    Want an avatar? Get a gravatar! • You can link to this comment

    This is a brilliant article , i had no idea that you could do css shorthand, i will definately look into this, is there an article anywhere that details the most common and useful css shorthand to know.

  13. Trackback