CSS Shorthand

CSS Shorthand is a very useful thing to know as it will keep your style sheets more organized and also keep the file sizes down.

Proper CSS Shorthand

One of the nicest features supported in CSS2 is the ability to use shorthand as well as longhand. While most people familiar with cascading style sheets use shorthand to some extent, there are several lesser known properties in which shorthand can be applied.

The Purpose

The main reason I use CSS shorthand is because it's easier to manage a style sheet when it's half the size of a similar one using longhand. While file size isn't the most important thing, mainly because style sheets are cached in the visitors browser, squeezing every last byte out of one can nonetheless save a lot of bandwidth on high traffic sites.

Onto Business

  1. Margin & Padding
  2. Fonts
  3. Backgrounds
  4. Borders
  5. Lists
  6. Outlines

Margins & Padding

There are several ways to define margin and padding, but both use the same shorthand. Instead of using multiple properties, like margin-top or padding-right, you can combine them all using the CSS properties margin and padding. Look at the following examples:

  1. margin-top: 10px;
  2. margin-right: 15px;
  3. margin-bottom: 5px;
  4. margin-left: 0px;
  1. margin: 10px 15px 5px 0;

Both snippets of code do exactly the same thing.

But what happens when some or all of the sides have the same measurements? Let's take a look:

  1. margin: 10px;

This means that every side will have a margin of 10 pixels.

  1. padding-top: 10px
  2. padding-right: 5px;
  3. padding-bottom: 10px;
  4. padding-left: 5px;

can also be written as

  1. padding: 10px 5px;

So we've discussed one, two and four variables in the margin and padding properties, but not many know that you can also use three variables. The first stands for top, the second for right and left, and the third for bottom. The next two blocks of code will render an object's margin uniformly:

  1. margin: 0 10px 5px 10px;
  1. margin: 0 10px 5px;

Go back to the shorthand index.

Fonts

The font property, unlike margin and padding, gives you the option of omitting most of the values. The only two requirements are the font size and the font family. All of the properties below are in the order in which they must appear in the font property.

  1. font-style: italic;
  2. font-variant: small-caps;
  3. font-weight: 500;
  4. font-size: 1em;
  5. line-height: 24px;
  6. font-family: arial,verdana,sans-serif;

Now let's take a look at the same instructions written in shorthand:

  1. font: italic small-caps 500 1em/24px arial,verdana,sans-serif;

This chunk of code will produce a font size of 12 pixels, a line height of 28 pixels, and a sans-serif typeface:

  1. font: 12px/28px sans-serif;

All of the other values that were omitted are automatically set to normal (unless a value is inherited).

Go back to the shorthand index.

Backgrounds

This very useful property allows you to condense several values into one: the background property.

  1. background-color: #456;
  2. background-image: url(backdrop.gif);
  3. background-repeat: repeat-y;
  4. background-attachment: fixed;
  5. background-position: 50% 50%;

and in shorthand:

  1. background: #456 url(backdrop.gif) repeat-y fixed 50% 50%;

The background property, like font, uses the default values when they aren't present in the statement. Either the background color or the background image must be defined. The default values are listed below:

Perhaps the most common mistakes people make when using the background property is switching the position values.

  1. background-position: top left;

it should be

  1. background-position: left top;

This mistake becomes apparent when you substitute the layman's terms for other values like pixels or percentages. In the spirit of shorthand, however, left top is more easily written as 0 0 whereas right bottom can be written as 100% 100%.

Another shorthand technique too insignificant to merit its own section is used here for the color. If you'll notice, I set the background color to #456. Expanded, this hex value is #445566. It's nothing life-changing, but every byte counts!

Go back to the shorthand index.

Borders

Two very useful properties are border and border-width. Using longhand, borders can easily become 10 or more statements. Let's take a look at how borders can work:

  1. border-left-width: 2px;
  2. border-left-style: solid;
  3. border-left-color: #000;
  4. border-right-width: 2px;
  5. border-right-style: solid;
  6. border-top-color: #000;
  7. border-top-width: 3px;
  8. border-top-style: solid;
  9. border-top-color: #000;

My favorite way of using border and border-width in leu of the previous code is as follows:

  1. border: solid #000;
  2. border-width: 3px 2px 0;

If all of the border widths are the same, you can simply write this:

  1. border: 3px solid #000;

With the many border properties, there is a myriad of ways to achieve the same effect, but border and border-width will help a smaller chunk of code go a long way.

Go back to the shorthand index.

Lists

While this property is rarely used fully in the mainstream, it's still a good thing to be familiar with. Generally, list-style is simply set to none to remove any bullets or numbers.

  1. list-style-type: circle;
  2. list-style-position: inside;
  3. list-style-image: url(bullet.gif);

can be condensed to:

  1. list-style: inside circle url(bullet.gif);

Go back to the shorthand index.

Outlines

Also rarely used, the outline property can be slightly abbreviated. Be careful using this property, though, as it's not supported in all browsers.

  1. outline-color: #fff;
  2. outline-style: dotted;
  3. outline-width: 1px;

is longhand for

  1. outline: #fff dotted 1px;

Go back to the shorthand index.

Navigation

Web Design Resources and Articles