The CSS Box Model: Padding, Border and Margin
In CSS, every element on a page is a box. When you can see the boxes, layout gets much easier.
Four layers of every box
- Content: the text or image.
- Padding: space inside the box, around the content.
- Border: a line around the padding.
- Margin: space outside the box, between this box and others.
.card {
padding: 20px;
border: 3px solid black;
margin: 30px;
}Short ways to write spacing
margin: 10px; /* all four sides */
margin: 10px 20px; /* top+bottom, left+right */
margin: 10px 20px 30px 40px; /* top, right, bottom, left (clockwise) */
padding-top: 5px; /* only one side */Width and height
.box {
width: 100%;
max-width: 600px; /* never wider than 600px */
min-height: 200px; /* at least 200px tall, can grow */
}Avoid fixed heights like height: 200px on boxes with text. On a small phone, the text needs more lines, and it spills out of the box. min-height gives the same look but lets the box grow when needed.
When content is too big: overflow
.box { height: 100px; overflow: auto; } /* add a scrollbar if needed */| Value | What happens to extra content |
|---|---|
visible | Spills outside (default) |
hidden | Is cut off |
auto | Gets a scrollbar only when needed |
Block and inline boxes
Boxes come in two main types:
| Block | Inline | |
|---|---|---|
| Examples | div, p, h1, ul | a, strong, em, span |
| Starts on a new line? | Yes | No, stays inside the text |
| Width | Full width by default | Only as wide as its content |
| Top/bottom margin works? | Yes | No |
You can change the type with display: display: block;, display: inline; or display: inline-block; (inline, but margins work).
<div> and <span> have no meaning. They are empty boxes for styling: div is block, span is inline.
Buttons with inline-block
Links are inline, so top and bottom padding does not push other things away. For a button-style link, use inline-block:
.btn {
display: inline-block;
padding: 12px 24px;
background: #2563eb;
color: white;
border-radius: 8px;
text-decoration: none;
}Fix widths with box-sizing
By default, padding and border are added to the width. A box with width: 300px and padding: 20px becomes 340px wide. This confuses everyone. Add this at the top of every stylesheet:
box-sizing: border-box, the width you write is the width you get.* {
box-sizing: border-box;
}Now width: 300px means the full box is 300px, padding and border included.
Center a box
.page {
max-width: 900px;
margin: 0 auto; /* auto left and right = centered */
}max-width says "never wider than 900px, but smaller is fine on phones". margin: 0 auto splits the empty space equally on both sides.
margin: 0 auto centers a box that has a width.See the boxes in DevTools
Right-click an element and choose Inspect. Scroll to the bottom of the Styles panel (or open the Computed tab). You will see a coloured picture of the box model with the exact numbers for margin, border, padding and content.
Hover over elements in the HTML panel and the page highlights them: blue for content, green for padding, orange for margin. When spacing looks wrong, this tells you which layer is causing it.
Mini project: a product card
<div class="card">
<h3>Cotton T-shirt</h3>
<p>Soft, light and perfect for summer.</p>
<a class="btn" href="#">Buy now</a>
</div>* { box-sizing: border-box; }
.card {
max-width: 300px;
margin: 20px auto;
padding: 24px;
border: 1px solid #e5e7eb;
border-radius: 12px;
}
.card h3 { margin: 0 0 8px; }
.card p { margin: 0 0 16px; color: #4b5563; }Soft, light and perfect for summer.
Buy nowEvery space in this card comes from padding or margin. Try changing each number and see which gap moves.
Common mistakes
| Problem | Fix |
|---|---|
| The page scrolls sideways a little | Something is wider than the screen. Add box-sizing: border-box and use max-width instead of width. |
margin: 0 auto does not center | The box needs a width or max-width, and it must be a block box |
| Padding on a link does not push lines apart | Links are inline. Use display: inline-block. |
| A gap appears above the first heading | Headings have a default top margin. Set margin-top: 0. |
Cheat sheet
| Property | What it does |
|---|---|
padding | Space inside the border |
margin | Space outside the border |
border | Line around the box |
box-sizing: border-box | Width includes padding and border |
max-width | Never wider than this |
min-height | At least this tall, can grow |
display | block, inline or inline-block |
overflow | What to do with extra content |
Practice time
- Make a
.cardbox with padding, a border and rounded corners (border-radius: 12px). - Center your main content with
max-widthandmargin: 0 auto. - Use Inspect in your browser: hover an element and look at the coloured box-model picture.
- Build the product card, then make three of them.
- Use DevTools to find out where the space above a heading comes from.
Quick summary
- Every element is a box: content, padding, border, margin.
- Block boxes stack; inline boxes flow inside text.
- Always use
box-sizing: border-box.