Favorites

CSS Shorthand Properties

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

Why shorthand properties matter

Every CSS rule is made of properties (what you want to change, such as margin) and values (how you want to change it, such as 20px). Many properties come in families. For example, there are separate properties for the top, right, bottom and left margins of an element.

Writing each one separately is called using longhand properties. A shorthand property lets you set a whole family in a single line. The result looks the same in the browser; you simply write less.

/* Longhand: four lines */
margin-top: 20px;
margin-right: 15px;
margin-bottom: 25px;
margin-left: 5px;

/* Shorthand: one line, same result */
margin: 20px 15px 25px 5px;

Shorter stylesheets are a little quicker to download, but these days that saving is tiny. The bigger benefit is readability: a shorthand puts related settings side by side, so you can understand a rule at a glance.

In this post we'll cover the classic shorthands for padding and margins, borders, backgrounds, fonts and lists. Then we'll look at some newer ones used in modern layouts.

Key rule: shorthands aren't always the best choice. Any part you leave out is reset to its default value, which can quietly undo styles you set elsewhere. We'll look at this trap in the final section.

Margin and padding

Margin is the space outside an element's border, pushing other elements away. Padding is the space inside the border, between the edge and the content. Both use exactly the same shorthand pattern, so once you learn one you know the other.

One to four values

The margin and padding shorthands accept one, two, three or four values. The number of values decides which sides each value applies to.

ShorthandWhat it means
margin: 10px;All four sides are 10px
margin: 10px 15px;Top and bottom 10px, left and right 15px
margin: 10px 5px 20px;Top 10px, left and right 5px, bottom 20px
margin: 20px 15px 25px 5px;Top 20px, right 15px, bottom 25px, left 5px

Tip: with four values, the order runs clockwise from the top, like the hands of a clock: top, right, bottom, left. Some people remember it as "TRouBLe".

Spotting the shortcut

Whenever opposite sides match, you can drop values. Look at this longhand:

padding-top: 10px;
padding-right: 30px;
padding-bottom: 10px;
padding-left: 30px;

Top matches bottom, and left matches right, so two values are enough:

padding: 10px 30px;
Results
padding: 10px 30px

Centring with auto

A very common use of the two-value form is centring a block horizontally. Setting the left and right margins to auto tells the browser to share the leftover space equally.

.container {
  max-width: 960px;
  margin: 0 auto; /* no top/bottom margin, centred left and right */
}

Logical shorthands

Modern CSS also has logical properties, which follow the direction text flows instead of physical sides. In English, the inline direction runs left to right and the block direction runs top to bottom. Their shorthands take one or two values: start, then end.

.card {
  margin-block: 24px;        /* top and bottom in English */
  padding-inline: 16px 32px; /* left 16px, right 32px in English */
}

The benefit is that your layout automatically flips for right-to-left languages such as Arabic or Hebrew, with no extra rules.

Borders and rounded corners

A border needs three things: a width (how thick), a style (solid, dashed, dotted and so on) and a colour. In longhand that's three lines:

border-width: 2px;
border-style: solid;
border-color: #1d4ed8;

The border shorthand combines them into one, in any order:

border: 2px solid #1d4ed8;

Key rule: the style is the one part you can't skip. Without it, the style defaults to none and no border appears at all. If you leave out the width it becomes medium (usually 3px), and if you leave out the colour it uses the text colour.

Different widths on each side

The border shorthand applies the same values to all four sides. If you want different widths, set the border first, then follow it with border-width. That property uses the same one-to-four-value pattern as margins.

.note {
  border: solid #1d4ed8;
  border-width: 4px 1px; /* 4px top and bottom, 1px left and right */
}

You can also target a single side with border-top, border-left and so on, or use the logical versions border-block and border-inline.

.quote {
  border-inline-start: 5px solid #1d4ed8; /* a left bar in English */
}

Rounded corners with border-radius

border-radius is a shorthand too, setting the curve of all four corners. It follows a similar pattern, but starts at the top-left corner and moves clockwise.

border-radius: 12px;           /* all corners */
border-radius: 12px 0;         /* top-left and bottom-right 12px, the other two square */
border-radius: 4px 12px 20px 28px; /* top-left, top-right, bottom-right, bottom-left */
border-radius: 50%;            /* turns a square into a circle */
Results

Backgrounds

Backgrounds have more longhand properties than almost anything else in CSS. Here's a background with a colour and an image, written the long way:

background-color: #ffffff;
background-image: url("hero.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-attachment: fixed;

The background shorthand squeezes all of that into a single line:

background: #ffffff url("hero.jpg") no-repeat center / cover fixed;

Most parts can go in any order, but there's one rule to remember. background-size must come straight after the position, separated by a forward slash (/). The slash tells the browser where the position ends and the size begins.

Layering backgrounds

The shorthand can also stack several backgrounds on one element, separated by commas. The first layer sits on top. A popular trick is laying a semi-transparent gradient over a photo so that white text stays readable.

.hero {
  background:
    linear-gradient(rgb(0 0 0 / 0.5), rgb(0 0 0 / 0.5)),
    url("hero.jpg") center / cover no-repeat;
}

Tip: when layering, a background colour can only go in the last (bottom) layer. Put it anywhere else and the whole declaration is ignored.

Fonts

The font shorthand sets the style, variant, weight, size, line height and family of your text in one go. Here's the longhand:

font-style: italic;
font-variant: small-caps;
font-weight: 700;
font-size: 1.5rem;
line-height: 1.4;
font-family: Georgia, "Times New Roman", serif;

And here's the shorthand:

font: italic small-caps 700 1.5rem/1.4 Georgia, "Times New Roman", serif;
Results

The quick brown fox

The rules for font

The font shorthand is stricter than most, so it trips people up. Keep these rules in mind:

  • Size and family are required. Without both, the browser ignores the whole line.
  • Order matters at the end. The size comes second to last and the family comes last.
  • Line height rides along with size. Put it straight after the size with a slash, as in 1.5rem/1.4.
  • Style, variant and weight are optional. They go before the size in any order, and anything you leave out resets to normal.

So the shortest valid version looks like this:

font: 1rem Georgia, serif;

Tip: use a unitless line height such as 1.4 rather than a fixed value like 30px. A unitless value scales with the font size, so headings and body text both get comfortable spacing.

Lists

The list-style shorthand controls the markers (bullets or numbers) in front of list items. It combines three longhands:

list-style-type: square;
list-style-position: inside;
list-style-image: none;

In shorthand, in any order:

list-style: square inside;
Results
  • Square bullets
  • Sitting inside the text area

The type is the marker shape, such as disc, square or decimal. The position decides whether the marker sits outside the text block (the default) or inside it.

The version you'll probably use most is the one that removes markers completely, which is handy for navigation menus:

.menu {
  list-style: none;
  padding: 0;
  margin: 0;
}

Tip: for custom bullets, the ::marker selector is now usually a better choice than an image. It lets you change the colour and size of bullets with normal CSS, as in li::marker { color: #1d4ed8; }.

Modern shorthands worth knowing

CSS has gained plenty of new shorthands since the early days, mostly for layout and animation. These are the ones you'll meet most often.

ShorthandCombinesExample
gaprow-gap, column-gapgap: 24px 16px;
insettop, right, bottom, leftinset: 0;
flexflex-grow, flex-shrink, flex-basisflex: 1;
place-itemsalign-items, justify-itemsplace-items: center;
grid-areaGrid row and column start/end linesgrid-area: 1 / 1 / 3 / 2;
transitionProperty, duration, timing, delaytransition: color 0.3s ease;
overflowoverflow-x, overflow-yoverflow: hidden auto;

gap

gap sets the space between items in a flexbox or grid layout (two CSS layout systems for arranging items in rows and columns). With one value both gaps match; with two, the first is the row gap and the second the column gap.

.gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}
Results

inset

When you position an element with position: absolute or fixed, you set where it sits using top, right, bottom and left. The inset shorthand takes them in the same clockwise order as margins. inset: 0 is a quick way to make an overlay fill its parent.

.overlay {
  position: absolute;
  inset: 0; /* same as top: 0; right: 0; bottom: 0; left: 0; */
}

flex

The flex shorthand controls how a flex item grows and shrinks to fill space. It combines three values, but there are a few keyword-style versions worth memorising:

  • flex: 1 means "grow to share the free space equally". It's the most common one.
  • flex: auto means grow and shrink, but start from the item's natural size.
  • flex: none means keep its natural size and never grow or shrink.

place-items

place-items sets vertical and horizontal alignment together. Paired with grid, it gives you the famous one-line centring trick:

.centre-me {
  display: grid;
  place-items: center;
}

transition

A transition animates a property smoothly when it changes, for example when you hover over a button. The shorthand lists the property, the duration, the timing curve and an optional delay. If there are two time values, the first is always the duration.

.button {
  background: #1d4ed8;
  transition: background-color 0.3s ease, transform 0.2s ease;
}
.button:hover {
  background: #1e3a8a;
  transform: translateY(-2px);
}

Common traps and when to use longhand

Shorthands are brilliant, but they have one behaviour that catches nearly everyone at some point.

Shorthands reset what you leave out

When you use a shorthand, every part you don't mention is reset to its default value. That can wipe out something set in an earlier rule. Here's a classic example:

.card {
  background-image: url("pattern.png");
}
.card.highlight {
  background: #fef3c7; /* oops: the pattern disappears */
}

The second rule only meant to change the colour. However, because background is a shorthand, it also reset background-image to none. The fix is to use the longhand for the one thing you're changing:

.card.highlight {
  background-color: #fef3c7; /* the pattern stays */
}

The same thing happens with font, which resets line-height, and with border, which resets every side at once.

When to choose shorthand or longhand

SituationBest choice
Setting up an element's styles for the first timeShorthand
Changing one detail in a hover state or variationLonghand
Only one side or part needs a valueLonghand (e.g. margin-top)
Values that change with a custom propertyLonghand is often clearer
Overriding styles from a framework or themeLonghand, to avoid surprise resets

Tip: if a style mysteriously vanishes, open your browser's developer tools (right-click the element and choose Inspect). Look for a crossed-out longhand: a shorthand in a later rule has probably overridden it.

Wrapping up

Shorthand properties make your CSS shorter, tidier and easier to scan. Learn the clockwise pattern for margins, padding and inset, remember the slash rules for background and font, and keep the newer layout shorthands like gap and flex close to hand.

Above all, remember that a shorthand sets everything in its family. Use it when you're defining styles, switch to longhand when you're tweaking them, and you'll get the best of both.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee