Menu

CSS Padding: Shorthand, Sides, Percentages and Examples

Padding is the space inside an element, between its content and its border. Learn the padding shorthand with 1 to 4 values, the single-side properties, logical padding, percentages, and how padding changes an element's size.

This page includes runnable editors - edit, run, and see output instantly.

What padding is

Padding is the space inside an element, between its content and its border. It takes the element's background, so it is the property that gives a button, a card or a badge room to breathe.

The blue area is the content. The green band around it is the 24px of padding on every side, and the border sits outside that.

The padding shorthand: 1, 2, 3 or 4 values

padding sets all four sides in one declaration. The number of values decides which side gets which, going clockwise from the top:

DeclarationTopRightBottomLeft
padding: 10px;10101010
padding: 10px 20px;10201020
padding: 10px 20px 30px;10203020
padding: 10px 20px 30px 40px;10203040

Two values mean "vertical, horizontal". Three values mean "top, horizontal, bottom". Four go around the clock: top, right, bottom, left (a common memory aid is TRouBLe).

Type any shorthand below. The box redraws and the browser reports what each side got:

Try 8px, 8px 24px, 0 0 40px, and 1em 5%. An invalid value, such as -10px, is ignored and the box keeps the last valid padding.

One side at a time

Each side has its own property, which is clearer than a four-value shorthand when you only want to change one:

.card {
  padding-top: 16px;
  padding-right: 24px;
  padding-bottom: 16px;
  padding-left: 24px;
}

A shorthand followed by one side is a common pattern: set everything, then adjust one side.

.card {
  padding: 16px;
  padding-bottom: 32px; /* only the bottom changes */
}

The order matters here: a padding shorthand written after padding-bottom would reset the bottom back to 16px.

Logical padding: padding-inline and padding-block

padding-left and padding-right are physical: they stay on the same side in every language. The logical properties follow the writing direction instead, which is what you want on a site that is also shown right to left:

Logical propertyIn English (left to right)In Arabic (right to left)
padding-inline-startleftright
padding-inline-endrightleft
padding-inlineleft and rightright and left
padding-blocktop and bottomtop and bottom

Padding makes the box bigger (unless border-box)

By default, width is the width of the content only, and padding is added on top of it. A width: 200px box with padding: 20px is 240px wide:

box-sizing: border-box keeps the box at 200px and takes the padding from inside. The box model page covers the full math.

Percentage padding

A percentage padding is measured against the width of the parent, on all four sides, including top and bottom:

That is where the old "padding-top: 56.25%" trick for 16:9 video boxes came from. Today aspect-ratio: 16 / 9 does the same job directly.

Padding is the right way to make a link or button bigger, because padding is part of the element and therefore part of what you can click or tap. Margin would add space but not a bigger target.

.btn {
  display: inline-block; /* lets vertical padding take space in the line */
  padding: 10px 18px;    /* a comfortable touch target */
}

Values padding accepts

ValueExampleNotes
Length12px, 1rem, 0.5emem scales with the element's font size
Percentage5%Of the containing block's width
0padding: 0No unit needed
Negative-10pxInvalid, ignored
autoautoInvalid for padding (it works for margin)

Common mistakes

  • Wrong order in the four-value shorthand. It is top, right, bottom, left, clockwise.
  • A box that grows when you add padding. Set box-sizing: border-box.
  • Using padding to separate two elements. Space between elements is margin; padding is space inside one. See margin vs padding.
  • Vertical padding on an inline link that overlaps other lines. Make it inline-block.
  • Negative padding or padding: auto. Both are invalid and silently ignored.

Frequently Asked Questions

What is padding in CSS?

Padding is the space between an element's content and its border. It is part of the element, so it shows the element's background color and makes the clickable area of a link or button bigger.

What order are the values in the padding shorthand?

Clockwise from the top: padding: top right bottom left;. With three values it is top, left and right, bottom; with two values, top and bottom then left and right; with one value, all four sides.

Does padding increase the size of an element?

With the default box-sizing: content-box, yes: padding is added to width and height. With box-sizing: border-box, padding is taken from inside the width, so the element keeps the size you set.

What is a percentage padding relative to?

The width of the containing block, for all four sides. padding-top: 50% is half of the parent's width, not its height, which is why old aspect-ratio tricks used vertical padding.

Can padding be negative?

No. A negative padding value is invalid and the declaration is ignored. To pull content outward, use a negative margin instead.

How do I add padding in HTML?

With CSS, in a stylesheet or a style attribute: <div style="padding: 16px;">. HTML has no padding attribute for general elements; the old cellpadding attribute on tables is obsolete and replaced by padding on td and th.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED