Lesson 4
25 min
margin, border, padding, width/height : understand CSS boxes.
Definition
The box model is the principle by which the browser treats every HTML element as a rectangular box. This box is made of four nested areas — content, padding, border and margin — which determine both its actual size and the space it takes up relative to neighboring elements.
In CSS, every element is represented as a rectangular box, made of four concentric layers: the content, the padding, the border, and the margin.
margin
border
padding
.card {
width: 300px;
height: 150px;
padding: 20px; /* inner space, between the content and the border */
border: 2px solid #6d28d9; /* border */
margin: 16px; /* outer space, between the box and its neighbors */
}With the default behavior, the space a box actually takes up is the sum of all its layers. For the width, you add the content, the padding and the border on both sides, then the margins on both sides. Knowing how to do this calculation helps you understand why two 50% columns no longer fit side by side as soon as you add padding to them.
.box {
width: 300px;
padding: 20px;
border: 2px solid black;
margin: 10px;
}
/* Width of the visible box: 300 + (20 x 2) + (2 x 2) = 344px */
/* Space taken up on the page: 344 + (10 x 2) = 364px */See it in the browser
Right-click an element, then choose “Inspect”: the styles tab shows a colored diagram of the box, with the exact value of each layer. It’s the fastest way to understand where an unexpected offset comes from.
By default, widthand height only apply to the content: the padding and the border are added on top, which can make your layout overflow in surprising ways.
.box {
width: 300px;
padding: 20px;
border: 2px solid black;
/* Actual displayed width: 300 + 20*2 + 2*2 = 344px, not 300px! */
}* {
box-sizing: border-box;
/* From now on, padding and border are INCLUDED in the 300px */
}
.box {
width: 300px;
padding: 20px;
border: 2px solid black;
/* Actual displayed width: 300px, as expected */
}Practically an industry standard
Almost all developers add * { box-sizing: border-box; }at the very top of their CSS, once and for all. It avoids countless layout surprises.
margin andpadding accept from 1 to 4 values, going clockwise starting from the top.
.a { padding: 20px; } /* all 4 sides at 20px */
.b { padding: 10px 20px; } /* top/bottom: 10px, left/right: 20px */
.c { padding: 10px 20px 15px; } /* top: 10px, left/right: 20px, bottom: 15px */
.d { padding: 10px 20px 15px 5px; } /* top, right, bottom, left */
/* You can also target a single side */
.e {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;
}Not all boxes behave the same way. A block element (likediv or p) takes up the full available width and starts on a new line: it fully respects width, height and vertical margins. Aninline element (like span or a) stays within the flow of the text and ignores width and height. The inline-block mode combines both: inline, but with adjustable dimensions.
.block {
display: block; /* full width, new line */
}
.inline {
display: inline; /* width and height are ignored */
}
.button {
display: inline-block; /* stays inline, but accepts width, height and vertical padding */
padding: 8px 16px;
}A surprising behavior: when two vertical margins of block elements touch in normal flow, they don’t add up — they collapse, and only the larger one is kept. Horizontal margins never collapse. This explains why two paragraphs that each have 20px of top and bottom margin are separated by 20px, not 40px.
.first {
margin-bottom: 30px;
}
.second {
margin-top: 20px;
}
/* Actual space between the two boxes: 30px (the larger one), not 50px */A classic: centering a fixed-width block in the middle of its parent, using automatic margins.
.container {
width: 800px;
margin: 0 auto; /* top/bottom: 0, left/right: automatic and equal */
}.card {
border: 2px solid #6d28d9; /* width, style, color */
border-radius: 12px; /* rounded corners */
border-top: 4px solid red; /* a single side */
}
.round {
width: 60px;
height: 60px;
border-radius: 50%; /* turns a square into a circle */
}Let’s bring together the concepts from this lesson in a concrete example. This card combines a maximum width, comfortable padding, a rounded border, a light shadow and automatic centering: a reusable pattern for many components.
<div class="card">
<h3>Card title</h3>
<p>A short text explaining the content of this card.</p>
</div>* {
box-sizing: border-box;
}
.card {
max-width: 400px;
margin: 24px auto;
padding: 20px;
border: 1px solid #ddd6fe;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
background-color: white;
}Padding or margin?
A simple rule: if the space should stay inside the box (and benefit from its background color), use padding. If it should separate the box from its neighbors, use margin.
The box-shadow property gives a box depth without changing its size. It is set with the horizontal offset, the vertical offset, the blur, then the color. Similarly,outline draws a line around the box, but unlike a border it takesno space in the layout: that’s why browsers use it to indicate keyboard focus.
.card {
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); /* x, y, blur, color */
}
.field:focus {
outline: 3px solid #6d28d9; /* adds no size to the box */
outline-offset: 2px; /* small gap between the box and the outline */
}Padding, margins and borders accept all length units. Pixels give a fixed result;rem units follow the page’s font size and adapt if the user enlarges their text, which makes them an excellent choice for consistent spacing. Percentages, on the other hand, are calculated relative to the parent’s width, even for vertical spacing.
.card {
padding: 1.5rem; /* adapts to the page's font size */
margin-bottom: 2rem;
width: 90%; /* 90% of the parent's width */
border: 1px solid #cbd5e1;
}A fixed width is a problem on a small screen: the box overflows. It’s better to set a limit with max-width: the box adapts to the available space, but never exceeds the chosen value. And if the content is larger than the box, theoverflow property decides what happens to it: visible, clipped or scrollable.
.container {
width: 100%;
max-width: 800px; /* never exceeds 800px, but shrinks on mobile */
margin: 0 auto;
}
.area {
height: 200px;
overflow: auto; /* scrollbar if the content overflows */
}Exercise
<div>elements) with padding, a rounded border and a margin between them. Addbox-sizing: border-box;globally.Exercise: calculate and verify
box-sizing: border-box;, then check your results in the browser inspector.Exercise: margin collapsing
margin-bottom: 40px;on the first and margin-top: 25px;on the second. Measure the actual space between them in the inspector and explain the result.Going further
border-radius: 50%;, then center a fixed-width block (400px) horizontally in the middle of the page.