Shorthand properties

Shorthand properties are CSS properties that let you set the values of multiple other CSS properties in one declaration. Using a shorthand property, you can write more concise (and often more readable) style sheets, saving time and energy.

The CSS specification defines shorthand properties to group the definition of common properties acting on the same theme. For example, the CSS background property is a shorthand property that's able to define the values of background-color, background-image, background-repeat, and background-position.

Tricky edge cases

There are a few edge cases to keep in mind when using shorthand properties.

Omitting properties

A value which is not specified is set to a default value defined by the shorthand, which may differ from the property's initial value.

That means that it overrides previously set values. For example:

css
p {
  background-color: red;
  background: url("images/bg.gif") no-repeat left top;
}

This will not set the color of the background to red but to the default value for background-color, which is transparent.

Only the individual properties values can inherit. As missing values are replaced by their initial value, it is impossible to allow inheritance of individual properties by omitting them. The keyword inherit can be applied to a property, but only as a whole, not as a keyword for one value or another. That means that the only way to make some specific value to be inherited is to use the longhand property with the keyword inherit.

Ordering properties

Shorthand properties try not to force a specific order for the values of the properties they replace. This works well when these properties use values of different types, as the order has no importance, but this does not work as easily when several properties can have identical values.

Two important cases here are:

Sides of the box

Shorthands handling properties related to the sides of the box, like border-style, margin or padding, always use a consistent 1-to-4-value syntax representing those sides:

Corners of the box

Similarly, shorthands handling properties related to the corners of the box, like border-radius, always use a consistent 1-to-4-value syntax representing those corners:

Background properties

Consider a background with the following properties

css
background-color: black;
background-image: url("images/bg.gif");
background-repeat: no-repeat;
background-position: left top;

These four declarations can be shortened to just one:

css
background: black url("images/bg.gif") no-repeat left top;

(The shorthand form is actually the equivalent of the longhand properties above plus background-attachment: scroll and, in CSS3, some additional properties.)

See background for more detailed information, including CSS3 properties.

Font properties

Consider the following declarations:

css
font-style: italic;
font-weight: bold;
font-size: 0.8em;
line-height: 1.2;
font-family: "Arial", sans-serif;

These 5 statements can be shortened to the following:

css
font:
  italic bold 0.8em/1.2 "Arial",
  sans-serif;

This shorthand declaration is actually equivalent to the longhand declarations above plus font-variant: normal, font-size-adjust: none, and font-stretch: normal.

Border properties

With borders, the width, color, and style can be simplified into one declaration. For example, consider the following CSS:

css
border-width: 1px;
border-style: solid;
border-color: black;

It can be simplified as:

css
border: 1px solid black;

Margin and padding properties

Shorthand versions of margin and padding values work similarly; the margin property allows for shorthand values to be specified using one, two, three, or four values. Consider the following CSS declarations:

css
margin-top: 10px;
margin-right: 5px;
margin-bottom: 10px;
margin-left: 5px;

They are the same as the following declaration using the four value shorthand. Note that the values are in clockwise order, beginning at the top: top, right, bottom, then left (TRBL, the consonants in "trouble").

css
margin: 10px 5px 10px 5px;

Margin shorthand rules for one, two, three and four value declarations are:

Position properties

With position, the shorthand versions of top, right, bottom and left can be simplified into one declaration. For example, consider the following CSS:

css
top: 0;
right: 20px;
bottom: 0;
left: 20px;

It can be simplified as:

css
inset: 0 20px 0 20px;

Just like margins and paddings, the inset values are ordered clockwise - top, right, bottom, then left (TRBL).

The universal shorthand property

CSS provides a universal shorthand property, all, which applies its value to every property in the document. Its purpose is to change the properties' inheritance model.

See Handling conflicts or Introducing the CSS Cascade for more information about how inheritance works in CSS.

Shorthand properties

See also