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)
- • Space between content and border
- • Increases element's size
- • Inherits background color
- • Cannot be negative
Margin (External)
- • 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
Remember the clockwise order: Top → Right → Bottom → Left (TRouBLe)
Individual Properties
Use individual properties for precise control
Different Units
Choose units based on responsive needs
Common Padding Patterns
Design system standards
Buttons
Cards
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
Padding Shorthand
padding: 10px 20px sets top/bottom to 10px and left/right to 20px. Four values go clockwise: top right bottom left.
🎯 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
remfor padding so spacing scales with the user's font size. padding-inlineandpadding-blockare 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.