Padding — CSS

Padding is the space inside an element, between the content and the border. The element's background colour fills the padding too. The padding property takes…

The text in your button touches the button's edge. The button looks cramped, and it is hard to tap on a phone. A margin won't help here, because a margin only moves the button away from the elements next to it.

What is missing is padding: the space inside an element, between its content and its edge line (border). The more padding, the more room the content gets, and the box grows with it. The element's background colour fills the padding too. A margin is different: it sits outside the box and is always transparent.

Room inside a button

This example gives a button a background colour and padding:

<button class="btn">Buy now</button>

<style>
.btn {
  background-color: lightblue;
  padding: 12px 24px;
}
</style>

The result: a bigger blue button, with the text in the middle and room all around it. The blue fills the padding, and the padding area can be clicked too. That is why buttons need padding: the bigger the area you can click, the easier the button is to tap on a phone.

One to four values

Like margin, the padding property takes one to four values:

padding: 10px;                /* every side 10px */
padding: 10px 20px;           /* top-bottom 10px, right-left 20px */
padding: 10px 20px 30px;      /* top 10px, right-left 20px, bottom 30px */
padding: 10px 20px 30px 40px; /* top, right, bottom, left */

Four values go clockwise, starting at the top. Each side also has a property of its own: padding-top, padding-right, padding-bottom and padding-left. So padding: 10px 20px is exactly the same as these four declarations:

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

The numbers need a unit, like px. Padding can't be negative.

Padding makes the box wider

A box with width: 100px and padding: 20px is 140px wide on screen: 100 + 20 on the left + 20 on the right. The cause is the browser's default, box-sizing: content-box: width only sets the width of the content, and padding is added outside that width. If the box must stay 100px wide, add box-sizing: border-box, so the padding is counted inside the width.

Padding or margin?

A button whose text touches its edge needs padding, because the missing room is inside the button. If the button is too close to another element, what is missing is margin.

⚠️ Padding in the wrong order, or in the wrong place

TL;DR: Padding is the space inside an element, between the content and the border. The element's background colour fills the padding too. The padding property takes one to four values: one value for every side, two values for top-bottom then right-left, four values for top, right, bottom, left (clockwise). By default (content-box), padding is added outside the width, so the box is wider than width. For room inside the box, use padding. For a gap to other elements, use margin.

⚡ Room inside a button

Write:

.btn {
  background-color: lightblue;
  padding: 12px 24px;
}

Meaning: Padding is the space inside an element, between the content and the border. In this rule, the .btn button gets 12px of padding on the top and bottom, and 24px on the right and left. The background colour fills the padding too, and the padding area can be clicked, so the button is easy to tap on a phone.

⚡ Two values, two directions

Guess first: in padding: 10px 20px, which sides get the 20px?

Right and left. The first value is always for top and bottom, the second for right and left.

Meaning: padding takes one to four values. Four values go clockwise from the top: top, right, bottom, left. Each side also has a property of its own, like padding-top for the top side.

⚡ Inside or outside

Meaning: Need room inside the box, between the content and its edge? Use padding. Need a gap outside the box, to the elements next to it? Use margin.

What you will learn