XML technologies / CSS / CSS Cookbook / General topics / Shorthand properties

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

You want to combine declarations for individual properties using shorthand properties.

Solution

Start with a correctly marked-up HTML section:

<h3>Kurzschrift-Eigenschaften</h3>
<p>Mit Hilfe von Kurzschrift-Eigenschaften können Sie sich einiges an Tipparbeit sparen und gleichzeitig die Dateigröße klein halten. Außerdem können Sie auf diese Weise die Leserlichkeit Ihrer Stylesheets erhöhen.</p>

Instead of separate rules for the properties font-style, font-size and font-family, combine them in the shorthand property font:

h3 {
  font: italic 18pt verdana, arial, sans-serif;
}
p {
  border: 2pt solid black;
}

Discussion

Many individual CSS properties can be combined using so-called shorthand properties.

For example, border is a shorthand property combining the rules for the following three properties:

  • border-color

  • border-width

  • border-style

The shorthand property font also combines several properties. Values can be specified for the following individual properties:

  • font-style

  • font-size/line-height

  • font-family

  • font-weight

  • font-variant

Apart from font-family and font-size/line-height, specify values as you would for the individual properties. The values for font-family must, when using font, be listed in order of priority and separated by commas.

If you want to specify values for both font-size and line-height, separate them with a slash:

h3 {
  font: italic 18pt/20pt verdana, arial, sans-serif
}

The following table lists the available shorthand properties.

Table: Shorthand properties.

Property

Value

Example

background

background-color background-image background-repeat background-attachment background-position

background: url(book.gif) #999 no-repeat top;

border

border-width

border: thin solid #000;

border-left

border-style

border-right

border-color

border-top

border-bottom

font

font-style font-variant font-weight font-size/line-height font-family caption icon menu message-box small-caption status-bar

font: 14px italic Verdana, Arial, sans-serif;

list-style

list-style-type list-style-position list-style-image

list-style: circle inside;

margin

margin-top margin-right margin-bottom margin-left

margin: 5px 0px 5px 10px; margin: 5px;

padding

padding-top padding-right padding-bottom padding-left

padding: 5px 10%;

See also

  

 

 

 

back to top