CSS Basics

CSS Padding

CSS Padding is the transparent area between an element's content and its border, creating internal spacing that expands the element's clickable area and affects background visibility.

What is padding in CSS?

Padding is the space between an element's content and its border, and it takes the element's background colour. It uses the same shorthand as margin: padding: 12px 24px sets 12px top and bottom and 24px left and right. With box-sizing: border-box, padding fits inside the element's width instead of adding to it.

.button { padding: 12px 24px; }
.card   { padding: 1.5rem; }
.input  { padding-inline: 1rem; padding-block: .5rem; }

Padding vs Margin

Padding (Internal)

Content
  • • Space between content and border
  • • Increases element's size
  • • Inherits background color
  • • Cannot be negative

Margin (External)

Content
  • • Space outside the element
  • • Pushes other elements away
  • • Transparent background
  • • Can be negative

Padding Properties Demo

Explore different padding values and their effects on layout. Hover over elements to see padding visualization, click buttons and cards to see interactive effects!

Code Editor

<div class="box all">padding: 20px</div>
<div class="box two">padding: 8px 32px</div>
<div class="box logical">padding-inline: 40px</div>
<p id="sizes"></p>

Preview

Padding Syntax

Padding Shorthand Values

/* One value - all sides */ padding: 20px; /* Two values - vertical | horizontal */ padding: 15px 25px; /* top/bottom: 15px, left/right: 25px */ /* Three values - top | horizontal | bottom */ padding: 10px 20px 30px; /* top: 10px, left/right: 20px, bottom: 30px */ /* Four values - top | right | bottom | left (clockwise) */ padding: 10px 15px 20px 25px;

Remember the clockwise order: Top → Right → Bottom → Left (TRouBLe)

Individual Properties

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

Use individual properties for precise control

Different Units

padding: 20px; /* Pixels */ padding: 1rem; /* Root em */ padding: 1.5em; /* Em units */ padding: 5%; /* Percentage */

Choose units based on responsive needs

Common Padding Patterns

Design system standards

Buttons

Small: 8px 16px
Medium: 12px 24px
Large: 16px 32px

Cards

Compact: 16px
Standard: 24px
Spacious: 32px

Form Elements

Your Learning Path

Master CSS padding and internal spacing

What You've Learned

  • ▶Padding vs margin differences
  • ▶Padding shorthand syntax
  • ▶Individual padding properties
  • ▶Different padding units
  • ▶Common padding patterns

What's Next

  • ▶CSS Height & Width - Element dimensions
  • ▶CSS Box Model - Complete layout system
  • ▶CSS Text - Typography and text styling
  • ▶CSS Fonts - Typography control

Pro Padding Tip

Consistent padding creates visual rhythm in your designs. Use a scale system (8px, 16px, 24px, 32px) to maintain harmony across your interface components.

CSS Padding — step by step

1 / 4
1

Padding Shorthand

padding: 10px 20px sets top/bottom to 10px and left/right to 20px. Four values go clockwise: top right bottom left.

1 of 4
🧠Use box-sizing: border-box Globally
Apply the universal selector with box-sizing: border-box in your reset so all elements include padding and border within their declared width — this makes layout math predictable and eliminates size-overflow surprises.

🎯 Quick Check

With box-sizing: border-box, an element has width: 300px and padding: 20px. What is its content area width?

CSS Padding: summary

Key points

  • Padding can't be negative.
  • Percentage padding is relative to the parent's width, which is the basis of the old aspect-ratio trick.
  • Use rem for padding so spacing scales with the user's font size.
  • padding-inline and padding-block are logical properties that adapt to the writing direction.

Frequently asked questions

›Does padding increase the width of an element?

With the default box-sizing: content-box, yes: padding is added to the width. With box-sizing: border-box, the width stays the same and the content area shrinks.

›When should I use padding instead of margin?

Use padding for space inside an element, where the background should extend. Use margin for space between separate elements.