CSS propertyBox model

padding

Sets the inner space between the content and the border.

Initial value
0
Inherited
No
Animatable
yes, as a length
Applies to
all elements except internal table elements (other than cells)
JavaScript
element.style.padding = "12px 20px"
On this page

Example

#
CSS
.alert {
  padding: 12px 16px;
  border-radius: 10px;
  background: #1e1533;
}
Preview

Definition and usage

#

The padding property is shorthand for four inner spaces: top, right, bottom, and left. Padding sits inside the border and takes on the element’s background color, which is why it makes a button or card "breathe".

The order of values is the same as in margin: one for all sides, two for vertical and horizontal, four clockwise starting from the top. Unlike margin, padding can’t be negative.

A percentage padding is always calculated against the parent’s width, even vertically. That used to be exploited to keep an element’s proportions fixed; today aspect-ratio does the job more conveniently.

Syntax

#
Syntax
padding: <top> <right>? <bottom>? <left>?;

Values

#
  • 0

    The text touches the edge of the background.
  • 12px

    <length>

    An equal inner space on every side.
  • 8px 24px

    vertical horizontal

    A typical button: less space vertically, more horizontally.
  • 4px 8px 20px 32px

    four values

    Top, right, bottom, left, clockwise.

Values in practice

#

0

The text touches the edge of the background.
.demo { padding: 0; }

12px

An equal inner space on every side.
.demo { padding: 12px; }

8px 24px

A typical button: less space vertically, more horizontally.
.demo { padding: 8px 24px; }

4px 8px 20px 32px

Top, right, bottom, left, clockwise.
.demo { padding: 4px 8px 20px 32px; }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • With box-sizing: content-box, padding makes the element bigger than width. With border-box it fits inside the given width.
  • Clickable elements need enough padding. A touch target on a phone should be at least about 44px.

Related entries

#

Spotted a mistake or missing an example? Write to us.