Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 139 Β· The Box Model

Padding

Learn how CSS padding adds breathing room inside an element, how the shorthand and percentages work, and how to choose between padding and margin every time.

Padding is the space inside an element, between its content and its border. It is what stops text from touching the edge of a button, a card or a highlighted note. Without padding, even a beautifully colored box looks cramped.

Padding works a lot like margin: same shorthand, same per-side properties. But it lives inside the border, so it shows the element's background, it makes clickable areas bigger, and it never collapses. This lesson covers the syntax, the slightly odd rule for percentages, and a clear way to decide between padding and margin.

Setting padding

There are four per-side properties and a shorthand:

CSS
.button {
  padding-top: 10px;
  padding-right: 20px;
  padding-bottom: 10px;
  padding-left: 20px;
}

/* Same thing, using the shorthand */
.button {
  padding: 10px 20px;
}

The shorthand follows the same clockwise pattern you learned in Margins:

  • 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.

The logical versions are padding-block (top and bottom in horizontal text) and padding-inline (left and right), plus single-side forms like padding-inline-start. padding-inline: 20px reads nicely for buttons and adapts to other writing directions.

Padding cannot be negative, and it cannot be auto.

Padding shorthand playground

The white strip in each box is its content area; the yellow around it is padding. Edit the values and watch each side change.

Padding and the box size

Remember from box-sizing that padding counts toward the size of an element differently under each model:

  • With content-box, padding is added to width.
  • With border-box, padding is taken out of width, and the content area shrinks.

Either way, padding is part of the border box: the area you see, the area that gets the background, and the area that responds to clicks. That last point matters for usability.

Padding makes bigger click targets

A link or button is clickable across its whole border box, padding included. Margins do not count. So when a link is hard to tap, the fix is padding, not margin.

Click targets: padding vs margin

Both rows space the links apart, but only the padded row gives you large targets. Accessibility guidelines recommend touch targets of at least 24 Γ— 24 CSS pixels, and around 44 Γ— 44 is a comfortable size for fingers. Padding is how you get there without making text bigger.

The links use display: inline-block so their vertical padding takes up space in the line. On a plain inline link, vertical padding is painted but does not push lines apart, as you saw in The Box Model.

Percentage padding

A percentage padding is calculated from the width of the containing block, for all four sides. Yes, even padding-top and padding-bottom use the width, not the height.

That sounds odd, but it led to a famous trick. Because padding-top: 56.25% is always 56.25% of the width, an empty box with that padding keeps a 16:9 shape at any size:

CSS
/* The old way to keep a 16:9 box */
.video-frame {
  height: 0;
  padding-top: 56.25%; /* 9 / 16 = 0.5625 */
  position: relative;
}

Today you should use the aspect-ratio property instead, which says what you mean and works with content inside:

CSS
.video-frame {
  aspect-ratio: 16 / 9;
}

You will meet aspect-ratio properly in Sizing Images: object-fit and aspect-ratio. It is still worth knowing the padding trick, because you will find it in older code.

Percentage padding follows the width

Both boxes keep the same 16:9 shape as you resize the frame. The pink one does it with padding; the blue one with aspect-ratio, and it can hold content without any positioning tricks.

Padding never collapses

Margins between blocks can collapse into one. Padding never does: two boxes with 20 pixels of padding each always show 20 pixels inside each box. Padding (or a border) on a parent is also one of the ways to stop a child's margin from leaking out of it, as you saw in the margin collapsing section of Margins.

Padding or margin?

Both create space. Ask yourself one question: should this space belong to the element, or sit between elements?

Choose padding when… Choose margin when…
The space should show the background or border The space should be empty, showing whatever is behind
You want a bigger clickable area You are separating one element from its neighbors
You are spacing content away from the element's own edges You want to center a block with auto
The space must never collapse You need negative space to overlap something

A card shows both at once:

CSS
.card {
  padding: 1.5rem;        /* text stays away from the card's edges */
  margin-bottom: 1rem;    /* cards stay away from each other */
  background: white;
  border: 1px solid #e2e8f0;
}

When a layout lives inside a Flexbox or Grid container, use the container's gap for the space between items, and padding for the space inside each one. That leaves margins for special cases like margin-left: auto.

Check your understanding

Quick check Β· 5 questions

of 5 answered You scored / 5 Β· perfect!

  1. 1. What does padding: 10px 20px 30px set on the left side?

    Three values mean top, left-and-right, bottom, so both left and right get 20 pixels.
  2. 2. A box sits in a 400px-wide container and has padding-top: 10%. How tall is its top padding?

    Percentage padding always uses the containing block's width, even for top and bottom: 10% of 400 is 40 pixels.
  3. 3. A link is hard to tap on phones. What is the best fix?

    The clickable area is the border box. Padding makes it bigger; margin only adds empty space around it.
  4. 4. Which of these is allowed?

    Padding cannot be negative or auto. padding-inline takes one or two values for the start and end sides.
  5. 5. Two stacked boxes each have padding: 20px. What happens to the padding where they meet?

    Only margins collapse. Padding is inside each box, so each one keeps all of its own.

Summary

  • Padding is the space between an element's content and its border, and it shows the background.
  • The padding shorthand takes one to four values, clockwise from the top; padding-block and padding-inline are the logical versions.
  • Padding can't be negative or auto, and it never collapses.
  • Padding is part of the clickable area, so use it to make links and buttons easier to hit.
  • Percentage padding is based on the containing block's width, on every side; prefer aspect-ratio over the old padding trick.
  • Use padding for space inside an element, margin (or gap) for space between elements.

Next: Borders, the line between padding and margin.