TExcelTAD
Home › CSS › CSS Padding

CSS Padding

Padding is the space inside an element, between its content and its border. Margin pushes other elements away; padding gives the content itself room to breathe. Because padding sits inside the border, the element's background colour fills it too.

Padding on each side

There is one property per side:

Property Side
padding-top above the content
padding-right right of the content
padding-bottom below the content
padding-left left of the content

Values can be lengths (px, em, rem…) or percentages. Percentages are always calculated from the width of the containing block — even for top and bottom padding. Negative values are not allowed.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .box {
    background: #ffe08a;
    border: 3px solid #d49b00;
    margin-bottom: 12px;
  }
  .none { padding: 0; }
  .roomy {
    padding-top: 20px;
    padding-right: 40px;
    padding-bottom: 20px;
    padding-left: 80px;
  }
</style>
</head>
<body>
  <div class="box none">No padding: the text touches the border.</div>
  <div class="box roomy">Top 20px, right 40px, bottom 20px, left 80px.</div>
</body>
</html>

Notice how the yellow background fills the padding area — that is the easiest way to see padding.

The padding shorthand

Writing four properties is tedious, so the padding shorthand accepts one to four values. The order is always clockwise starting at the top: top, right, bottom, left.

Shorthand Meaning
padding: 10px; all four sides 10px
padding: 10px 30px; top & bottom 10px, left & right 30px
padding: 10px 30px 5px; top 10px, left & right 30px, bottom 5px
padding: 10px 20px 30px 40px; top, right, bottom, left
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; flex-wrap: wrap; gap: 12px; }
  div { background: #c7e7ff; border: 2px solid #2a7ab8; }
  span { background: white; display: block; }
  .a { padding: 10px; }
  .b { padding: 10px 30px; }
  .c { padding: 10px 30px 40px; }
  .d { padding: 5px 15px 30px 50px; }
</style>
</head>
<body>
  <div class="a"><span>10px</span></div>
  <div class="b"><span>10px 30px</span></div>
  <div class="c"><span>10px 30px 40px</span></div>
  <div class="d"><span>5px 15px 30px 50px</span></div>
</body>
</html>

The white strip is the content box; the blue around it is the padding.

Tip: Remember the order with "TRouBLe": Top, Right, Bottom, Left.

Padding and element width

By default, width sets the width of the content only. Padding and border are added on top. A box with width: 200px and padding: 25px therefore takes up 250px on screen. This surprises almost everybody the first time.

The fix is box-sizing: border-box, which makes width include the padding and border:

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .ruler { width: 200px; height: 8px; background: #333; margin: 8px 0; }
  .box { width: 200px; padding: 25px; background: #ffc4d6; border: 2px solid #c2185b; }
  .fixed { box-sizing: border-box; background: #c8f7c5; border-color: #2e7d32; }
</style>
</head>
<body>
  <div class="ruler"></div>
  <div class="box">content-box: wider than 200px</div>
  <div class="ruler"></div>
  <div class="box fixed">border-box: exactly 200px</div>
</body>
</html>

You will learn more in the Box Sizing lesson. Many developers put *, *::before, *::after { box-sizing: border-box; } at the top of every stylesheet.

Padding on inline elements

Inline elements such as <a> and <span> accept padding, but vertical padding does not push surrounding lines apart — it simply overlaps them. Horizontal padding does take space. That is why buttons made from links often use display: inline-block.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; line-height: 1.6; max-width: 380px; }
  .tag { background: #6a5acd; color: white; padding: 8px 14px; border-radius: 6px; }
  .tag.block { display: inline-block; }
</style>
</head>
<body>
  <p>This inline <span class="tag">tag</span> has vertical padding that spills over the lines above and below it.</p>
  <p>This <span class="tag block">inline-block</span> tag reserves space for its padding, so the line grows.</p>
</body>
</html>

Summary

  • Padding is the space between content and border, painted with the element's background.
  • Use padding with 1–4 values (clockwise from the top) or the individual side properties.
  • Percentages are relative to the containing block's width.
  • Padding adds to the size unless you use box-sizing: border-box.

Test yourself

What does padding: 5px 20px; do?

  1. 5px on all sides
  2. Top and bottom 5px, left and right 20px
  3. Top 5px, bottom 20px
  4. Left 5px, right 20px

Which statement about padding is true?

  1. Padding can be negative
  2. Padding is transparent and never shows the background
  3. The element's background fills the padding area
  4. Padding is outside the border

A div has width: 300px; padding: 20px; and the default box-sizing. How wide is its content plus padding?

  1. 300px
  2. 320px
  3. 340px
  4. 260px