spacer i am dustin diaz

a JavaScriptr...

boosh.

don't worry about it.

CSS Shorthand Guide

Sunday Oct 23 2005

Ok. Let's set the record straight. There is no official guide for each and every CSS shorthand property value. So let's work together and put one together shall we? Ok. Straight to the business. Anytime I've ran into a specification (besides the confusing mess at the W3C), it turns into showing off a couple of examples and you're supposed to be set on your way. Well well. Over the years, I've found quite some interesting unknown quirky facts about these shorthands... hence this Guide was born.

Background

Backgrounds can be tricky. Nevertheless, effective when condensed correctly. The syntax for declaring the background shorthand values are as follows:

background properties

element { 

  background-color: color || #hex || (rgb / % || 0-255);

  "normal" out of the box, you may need to pay a little more close attention when you're styling elements that come with default browser styles like <h1> - <h6> or <strong> and <em>. For example, styling the strong element:



strong element styled with font

strong {

  font:12px verdana;

}
By writing the above into your style sheet, you will be unexplicitly removing the font-weight:bold default browser style that is applied to strong elements. Last but not least (for -font- that is), a real world example:

font shorthand property example (unexplicit)

p {

  font:bold 1em/1.2em georgia,"times new roman",serif;

}
This would be the same as declaring the following properties:

the font shorthand property (explicit)

p {

  font-style:normal;

  font-variant:normal;

  font-weight:bold;

  em;

  line-.2em;

  font-family:georgia,"times new roman",serif;

}

Border

Let's not waste time discussing the warnings. The same rules apply from here on out. This is all you need to know

border properties

element {

  border-number+unit;

  border-style: (numerous);

  border-color: color || #hex || (rgb / % || 0-255);

}
becomes this:

the border shorthand propertie

element {

  border:

    4px

    groove

    linen

}
Don't ask me how that would look. The fact that "linen" is in there, things could get scary. Nevermind the matter, here is where 'border' gets funny.

border examples

p {

  border:solid blue;

}

/* will create a '3px' solid blue border... 

   who knows where 3px came from?? */



p {

  border:5px solid;

}

/* will create 5px solid 'black' border... 

    default must be black?? */



p {

  border:dashed;

}

/* will create a '3px' dashed 'black' border...

    3px black lines unite! */





p { border:10px red; }

p { border:10px; }

p { border:red; }

/* these just don't even work */

One thing to specially take note about declaring a border without a color, the default will be 'black' unless otherwise noted through an explicit or inherited 'color' property. See the following examples:

border color examples



p {

  border:dotted;

  color:red;

}

/* will create a 3px dotted red border */

/* ----------------------------- */

body {

  color:blue;

}

body p {

  border:5px solid;

}

/* will create a 5px solid blue border */

/* ----------------------------- */
Get it? Got it. Good! (isn't that a song?) Anyway. On with this

Margin and Padding

These are by far the easiest. Just think about the hands of a clock starting at noon, and follow the hour. For the sake of brevity, we'll be working with margin (since it's a shorter word). So for all cases of margin, the same rules apply to padding.

margin properties.

element {

  margin-top: number+unit;

  margin-right: number+unit;

  margin-bottom: number+unit;

  margin-left: number+unit;

}
... combined into the margin superpowers:

the margin shorthand property

/* top right bottom left */

element {

  margin: auto auto auto auto;

}
Of course, you may declare your margin with one, two, three, or four values. Here is how each scenario will be played out:

margin fun

/* adds a 10px margin to all four sides */

element {

  margin:10px;

}



/* adds a 20px margin to top and bottom 

    and a 5px margin to left and right */

element {

  margin:20px 5px;

}



/* adds a 50px margin to top

    and a 10px margin to left and right

    and a 300px margin to bottom */

element {

  margin:50px 10px 300px;

}

Understood? Let's keep going. This is fun isn't it! (whatever, you like it).

Outline

Quite frankly, this property has dropped off the existence of the design radar. Mainly because of lack of browsers supporting this CSS 2.1 standard (yep, it's an actual property), but nonetheless, it too has a shorthand property. This property follows the exact same (or same exact - they mean the same thing) specification as the 'border' shorthand property. But, for purposes of this being a Guide, it must be here. So:

outline properties

element {

  outline-number+unit;

  outline-style: (numerous);

  outline-color: color || #hex || (rgb / % || 0-255);

}
Outline written as shorthand:

outline shorthand property

element {

  outline:3px dotted gray;

}
For purposes of trying to keep things from repeating, please see the border shorthand section on this document to understand the odds, ends, and quirks of the outline property.

List-style

This is it. The last one. It's rarely used frequently. Hence rarely. That is why I kept it until the end (sorry, the best was first in my own opinion). Here is the list-style properties:

list-style properties

element { 

  list-style-type: (numerous);

  list-style-position:inside || outside;

  list-style-image:url(image.png);

}
Here is the defaults:

list-style property defaults

element {

  list-style-type:disc;

  list-style-position:outside;

  list-style-image:none;

}
And for the sake of final brevity. Here is a simple example:

list-style shorthand property example

ul li {

  list-style:square inside url(image.png);

}

/* in this particular case if image.png is not available

    then a square will be provided as secondary */

That's it!

I hope this provides years and years of referencing for all your CSS shorthand needs. When CSS3 is finally outside its working draft, expect to see this guide updated as necessary.

follow me

recent

  • Matador: The Obvious MVC Framework for Node
  • Sandboxing JavaScript
  • Crouching Ender, hidden command
  • Ender.js - The open submodule library
  • Qwery - The Tiny Selector Engine
  • Klass
  • Smallest DOMReady code, ever.
  • $script.js - Another JavaScript loader
  • About that slowness on Twitter...
  • Autocomplete Fuzzy Matching
  • JavaScript Cache Provider
  • JavaScript Animate
  • Asynchronous method queue chaining in JavaScript
  • Something changed
  • Unofficial Twitter Widget Documentation

Get JS Design Patterns

  • spacer

spacer

this is who i am

Hi, my name is Dustin Diaz and I'm an Engineer starting my own company. Previously @Medium, @Twitter, @Google, and @Yahoo, author of Strobist® Info co-author of JavaScript Design Patterns, co-creator of the Ender JavaScript Framework, a Photographer, and an amateur Mixologist. This is my website. Welcome!

On this site I write about JavaScript. You can also follow along with my open-source work on Github.

This site is optimized and works best in Microsoft Internet Explorer 6.

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.