← Back to HTML & CSS, page 4

html-css-0064 · Easy fresher

How do CSS shorthand properties such as margin, padding and background work, and what can go wrong with them?

Shorthands set several properties together: margin and padding take one to four values in the order top, right, bottom, left. Any longhand not mentioned in a shorthand is reset to its initial value, so later shorthands can silently undo earlier rules.

Full answer

A shorthand sets several related properties in one line. The most used are margin, padding, border, background, font, flex and inset.

For margin and padding, the number of values decides the sides.

  • One value: all four sides.
  • Two values: top and bottom, then left and right.
  • Three values: top, then left and right, then bottom.
  • Four values: top, right, bottom, left, clockwise from the top. A memory aid is "TRouBLe".

Related shorthands such as margin-inline: 1rem set the left and right sides (in left-to-right text), and margin-block sets the top and bottom.

A shorthand always sets every longer property it covers. Any value you leave out is reset to its initial value. This is the usual cause of surprises. If you write background-size: 50% and later background: red, the shorthand resets background-size to auto. The font shorthand also resets line-height, so font: 16px Georgia after a custom line-height removes it.

Box D has 10px at the top, 20px on the right, 30px at the bottom and 40px on the left. Box E has rounded corners on the top-left and bottom-right only. In the reset paragraph the font shorthand wipes out the earlier line-height, so the line height returns to normal, while in the keep paragraph the longhand comes after and wins. The background shorthand resets the earlier background-size.

When you want to change one part only, use the longhand, such as background-color, so you do not reset the others.

html
<style>
  .a { padding: 10px; }
  .b { padding: 10px 20px; }
  .c { padding: 10px 20px 30px; }
  .d { padding: 10px 20px 30px 40px; }
  .e { border: 2px solid teal; border-radius: 8px 0 8px 0; }
  .reset { line-height: 40px; font: 16px Georgia; }
  .keep  { font: 16px Georgia; line-height: 40px; }
  .bg    { background-size: 50%; background: red; }
</style>
<div class="a" id="a">A</div>
<div class="b" id="b">B</div>
<div class="c" id="c">C</div>
<div class="d" id="d">D</div>
<div class="e" id="e">E</div>
<p class="reset" id="reset">Font after line-height</p>
<p class="keep" id="keep">Line-height after font</p>
<div class="bg" id="bg">Background</div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • margin and padding take 1 to 4 values: top, right, bottom, left.
  • Two values mean vertical then horizontal.
  • A shorthand resets every longhand it covers that you leave out.
  • font resets line-height; background resets size and position.
  • Use a longhand to change one part only.

Common mistakes

  • Writing margin: 10px 20px and thinking the values are left and right then top and bottom. The first is vertical, the second is horizontal.
  • Using the background shorthand after background-size and losing the size.
  • Putting the font shorthand after line-height and losing the line height.

Follow-ups

  • What does margin: 0 auto do?
  • How would you change only the background color without resetting the other background properties?

Tips

  • Say the clockwise order out loud when you answer: top, right, bottom, left.
  • Mention the reset behaviour. It is the detail that shows you have debugged shorthand problems.