← Back to CSS Course | Chapter 3: Box Model | Lesson 3 of 7

CSS Padding

Padding is the space inside an element, between its content and its border. If margin is personal space between elements, padding is more like the cushioning inside a single element, keeping its own content from feeling cramped against its edges. Just like margin, padding can be set on all sides at once, on individual sides, or with a shorthand property that accepts up to four values. Padding is part of what makes a button feel clickable or a card feel comfortable to read. Nearly every card, button, and input on a well-designed site like cookiescursor.com relies on padding to keep its content from touching its own edges.
Syntax
css
selector {
  padding: top right bottom left;
  padding-top: length;
}

What Is Padding?

Padding is the space between an element's content and its border, sitting inside the element rather than outside it like margin does. Padding also increases an element's total rendered size unless you're using box-sizing: border-box.

Note: If an element has a background color, padding is the area where that background shows around the content, right up to the border.
Warning: Removing all padding can make text or icons feel cramped right up against an element's edge or border.

Example: What Is Padding?

css
<style>
.box {
  background: lightblue;
  padding: 20px;
}
</style>
<div class="box">Padding sits inside the element, around the content</div>

Setting Padding on All Sides

Writing padding with a single value applies that exact amount of inner space to all four sides of an element at once. Just like margin's single-value shorthand, this applies identically to top, right, bottom, and left in one line.

Note:
  • A single-value padding declaration is the quickest way to give a card or button consistent breathing room on every side.
  • Accepted values:
  • length — e.g. 12px, 1rem
  • percentage — relative to the container's width
  • one to four values — top, right, bottom, left order
  • negative values are not allowed
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Warning: A single padding value affects all sides equally, which may add more horizontal space than you actually wanted on a wide element.

Example: Setting Padding on All Sides

css
<style>
.box {
  padding: 20px;
  background: lightblue;
}
</style>
<div class="box">Uniform padding on all four sides</div>

Individual Padding Sides

padding-top, padding-right, padding-bottom, and padding-left let you control the inner spacing on just one side of an element, independent of the others. Unlike margin, padding never collapses between elements, since it's part of the element's own box rather than space outside it.

Note:
  • Individual padding sides are useful when a component needs asymmetric spacing, like extra left padding to make room for an icon.
  • Accepted values:
  • padding-top — space inside above the content
  • padding-right — space inside on the right
  • padding-bottom — space inside below
  • padding-left — space inside on the left
  • each takes a length or percentage (never negative)
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Warning: It's easy to set one side's padding and forget the others still default to whatever was inherited or previously set.

Example: Individual Padding Sides

css
<style>
.box {
  padding-top: 10px;
  padding-right: 20px;
  padding-bottom: 30px;
  padding-left: 40px;
  background: lightblue;
}
</style>
<div class="box">Different padding per side</div>

Padding Shorthand

The padding shorthand sets all four sides in one declaration, and the number of values decides how they are applied. One value sets all four sides. Two values set top and bottom first, then left and right. Three values set top, then left and right together, then bottom. Four values go clockwise, starting from the top: top, right, bottom, left.

Padding also accepts percentages, such as padding: 5%. A percentage is calculated from the width of the parent (containing) block, even for top and bottom padding. Use padding: 0 to remove all padding from an element.

Note:
  • The same TRouBLe memory trick from margin (top, right, bottom, left) applies here too.
  • Accepted values:
  • length | percentage — each value (never negative)
  • 1 value — all four sides
  • 2 values — top/bottom, then left/right
  • 3 values — top, left/right, bottom
  • 4 values — top, right, bottom, left (clockwise)
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Warning: As with margin, the number of values you pass changes their meaning, double check whether you meant two values or four.

Example: Padding Shorthand

css
<style>
.one { padding: 20px; background: #def; }
.two { padding: 10px 30px; background: #fde; }
.three { padding: 5px 20px 15px; background: #dfd; }
.four { padding: 5px 10px 15px 20px; background: #ffd; }
</style>
<div class="one">1 value — all sides 20px</div>
<div class="two">2 values — top/bottom 10px, left/right 30px</div>
<div class="three">3 values — top 5px, sides 20px, bottom 15px</div>
<div class="four">4 values — top 5px, right 10px, bottom 15px, left 20px</div>

Padding and the Box Model

By default, padding adds to an element's total rendered width and height, on top of whatever width or height you set. box-sizing: border-box changes this, so padding is included inside the width you specify instead.

Note: Many developers set box-sizing: border-box globally at the start of a project, it makes width and padding much more predictable together.
Warning: Without border-box sizing, adding padding to an element with a fixed width can make it visually larger than you expected.

Example: Padding and the Box Model

css
<style>
.box {
  width: 200px;
  padding: 20px;
  box-sizing: border-box;
  background: lightblue;
}
</style>
<div class="box">Padding included inside the 200px width</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Confusing padding (space inside the border) with margin (space outside the border).
  2. Forgetting that padding adds to an element's total rendered size unless box-sizing: border-box is used.
  3. Using inconsistent padding values across similar components, which makes a design feel unpolished.
Chapter Summary
  • Padding is the space between an element's content and its own border.
  • Padding can be set on all sides at once, on individual sides, or with a shorthand property.
  • By default, padding adds to an element's total size; box-sizing: border-box changes that behavior.
Browser Support

All standard CSS padding properties covered here are supported by every modern browser.

🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.