Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Custom Properties, Cascade and Global Keywords

Shorthand properties

How CSS shorthand properties work: expansion to longhands, omitted values resetting to initial, the 1-2-3-4 value pattern, and common traps, with live examples.

A shorthand property sets several related longhand properties in one declaration. margin sets margin-top, margin-right, margin-bottom and margin-left; border sets nine longhands (width, style and color for each side); background sets eight or more.

The shorthand is not only shorter. It also has one rule that catches people out: any longhand you leave out is reset to its initial value, not left alone.

Try it

Shorthand resets omitted values

The 1-2-3-4 pattern

Box shorthands such as margin, padding, border-width, border-color, border-style, inset and scroll-margin take one to four values and go clockwise from the top:

Values Meaning
a all four sides
a b top and bottom = a; left and right = b
a b c top = a; left and right = b; bottom = c
a b c d top, right, bottom, left

Corner shorthands like border-radius follow the same idea starting at the top-left corner. A slash separates horizontal and vertical radii.

Usage notes

  • Order often matters less than you think: border: 1px solid red, border: red solid 1px and border: solid 1px red mean the same, because the value types are distinct. Where types overlap (for example font, background-position with background-size) the syntax is stricter: font needs size before family, and background-size follows / after position.
  • font resets a lot: it also resets line-height, font-variant subproperties, font-stretch and others unless you give them.
  • Cascade effect. A shorthand later in the cascade overrides the earlier longhands it covers, as in the demo. Put shorthands before longhands, not after, if you want both.
  • all is the extreme shorthand: it resets every property except direction, unicode-bidi and custom properties.
  • Invalid values drop the whole declaration. One bad value in a shorthand means nothing is applied.
  • Logical shorthands: margin-inline, padding-block, inset-inline and friends are shorthands for logical longhands.
  • Reading values in JavaScript: getComputedStyle(el).margin may be an empty string in some browsers when the four sides differ; read the longhands (marginTop) for reliable results.
  • Animation and transition shorthands set many longhands at once, including animation-delay; a missing item resets to its initial value.