XML technologies / CSS / Advanced websites with CSS / Behind the scenes / Using shorthand properties

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

Most CSS properties receive individual values. When several values are defined for one property, such as alternative typefaces for font-family, separate them with commas. A value name containing spaces or CSS syntax characters, such as a semicolon or colon, must be quoted.

There are also shorthand properties (English: shorthand property), which define several CSS properties in one property definition. This sounds more complicated than it is.

The best-known shorthand property is font. CSS beginners usually define one font property after another:

h1 {
  font-weight: bold;
  font-size: 90%;
  line-height: 1.8em;
  font-family: Helvetica, Arial, sans-serif;
}

The shorthand CSS property font defines all these font properties more efficiently:

h1 {
  font: bold 90%/1.8em Helvetica, Arial, sans-serif;
}

The same approach works for the property padding:

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

We can replace this definition with the following shorthand:

h1 {
  padding: 10px 20px 10px 5px;
}

Values are given clockwise: top, right, bottom, then left.

All CSS shorthand properties are listed in CSS property reference.

  

 

 

 

back to top