CSS Padding
CSS padding adds space inside an element — between the content and its border. Learn padding-top, right, bottom, left, shorthand values, how padding extends backgrounds, and why box-sizing: border-box matters.
- What is CSS Padding?
- The Box Model
- Basic Syntax
- Individual Sides
- Shorthand (1–4 values)
- Padding & Background
- Padding vs Margin
- box-sizing: border-box
- Common Mistakes
- Try It
- FAQ
What is CSS Padding?
CSS padding creates space inside an element — between the element's content and its border. Think of it as the cushioning inside a box before you reach the content itself.
Padding is part of the CSS box model and makes elements feel less cramped and easier to read. Unlike margin (which pushes things away outside), padding is inside the element — so the background colour fills the padding area too.
Space Inside
Padding lives between the content and the border — it's internal breathing room for your element.
Fills with Background
Because padding is inside, the element's background colour extends through the padding area.
Clickable Area
Padding is part of the element — clicking anywhere inside the padding triggers click events.
Shorthand Ready
Set all four sides at once with the padding shorthand — from 1 to 4 values.
The CSS Box Model
Every HTML element is a rectangular box made of four layers. Padding is the layer between the content and the border:
Basic Padding Syntax
The padding property adds equal space on all four sides of an element. You can use pixels, em, rem, percentages, or any valid CSS length unit:
/* Adds 20px padding on ALL four sides */ div { padding: 20px; } /* Works on any element */ p { padding: 15px; } button { padding: 10px; }
No padding:
padding: 20px:
Individual Side Properties
You can set padding on each side separately using four dedicated properties. Each one controls only that specific side:
div { padding-top: 10px; /* top only */ padding-right: 40px; /* right only */ padding-bottom: 20px; /* bottom only */ padding-left: 30px; /* left only */ }
Padding Shorthand (1–4 Values)
Instead of writing four separate rules, use the padding shorthand. The number of values determines which sides they apply to:
/* 1 value — all sides 25px */ div { padding: 25px; } /* 2 values — top&bottom=20px, left&right=40px */ div { padding: 20px 40px; } /* 3 values — top=10px, left&right=20px, bottom=30px */ div { padding: 10px 20px 30px; } /* 4 values — top=10, right=20, bottom=30, left=40 */ div { padding: 10px 20px 30px 40px; }
Padding and Background Colour
Padding extends the background area
Unlike margin, padding is inside the element — so the element's background colour fills the padding area too. This is why adding padding makes a coloured box appear bigger while keeping the background visible throughout.
div { background-color: lightblue; padding: 30px; border: 2px solid navy; }
No padding:
padding: 30px:
Padding vs Margin
This is one of the most important distinctions in CSS layout. Both add space — but in different places:
| Feature | Padding | Margin |
|---|---|---|
| Space location | Inside the border | Outside the border |
| Affects background | ✅ Yes — background fills padding | ❌ No — background stops at border |
| Purpose | Space between content and border | Space between this element and others |
| Clickable area | ✅ Padding area is clickable | ❌ Margin area is not clickable |
| Can be negative? | ❌ Cannot be negative | ✅ Can be negative |
div { margin: 20px; /* space OUTSIDE the box */ padding: 30px; /* space INSIDE the box */ border: 2px solid black; /* the box wall */ }
box-sizing: border-box
box-sizing: border-box — Stop padding from growing your element
By default, adding padding increases the total size of an element. A width: 300px div with padding: 20px actually renders as 340px wide. Setting box-sizing: border-box keeps the element at exactly its set width — padding is counted inside, not added on top.
/* Apply to ALL elements — recommended for every project */ * { box-sizing: border-box; } /* Without border-box: total width = 300 + 20 + 20 = 340px */ .no-bbox { width: 300px; padding: 20px; /* adds 40px! Total = 340px */ } /* With border-box: total width stays exactly 300px */ .with-bbox { box-sizing: border-box; width: 300px; padding: 20px; /* content shrinks, total stays 300px */ }
Common Mistakes
These are the errors beginners run into most often with CSS padding:
Padding is inside the element; margin is outside. Adding padding to create space between two separate elements won't work — you need margin for that. Always ask: "am I adding space inside this element or between elements?"
A width: 200px element with padding: 20px is actually 240px wide unless you have box-sizing: border-box applied. This breaks layouts because your element is wider than expected. Fix it with * { box-sizing: border-box; }.
Unlike margin, padding cannot be negative. Writing padding: -10px is invalid and browsers will ignore it completely. If you need to pull elements closer together, use negative margin instead.
The clockwise order is Top → Right → Bottom → Left. Beginners often write padding: 10px 20px 30px 40px expecting left-to-right or horizontal-vertical order. Use the "TRouBLe" trick to remember: T, R, B, L.
Questions About CSS Padding
The questions beginners ask most often about CSS padding: