Content, padding, border, and margin
Every element is laid out as a box. Knowing which part a CSS property changes helps explain unexpected gaps and sizing.
✓ What you will learn in this topic
- Explain the key idea: The box model consists of content, padding, border, and outer margin.
- Apply the concept: A blue border encloses the demo content and padding; the margin creates space outside that border.
- Avoid this common mistake:
Concept explanation
The content box holds text or other children. Padding adds room between content and border. The border surrounds both. Margin creates separation outside the border. Backgrounds generally paint through the padding area, but not the margin.
In the default content-box model, width describes only the content. Padding and border are added outside it. This is why a card declared as 300 pixels wide can occupy more than 300 pixels on screen.
Syntax
A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.
.demo {
width: 18rem;
padding: 1rem;
border: 4px solid #2563eb;
margin: 1rem;
}Example 1: a small page element
Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.
<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>.demo {
width: 18rem;
padding: 1rem;
border: 4px solid #2563eb;
margin: 1rem;
}Expected result: A blue border encloses the demo content and padding; the margin creates space outside that border.
Example 2: a practical interface card
The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.
<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>.card {
padding: 1.25rem;
border: 1px solid #cbd5e1;
margin-block: 1rem;
}Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.
Common mistake
With the default content-box model, declared width excludes padding and borders, so the rendered box can become wider than expected.
Best practice
Use border-box sizing consistently and remember that margin is outside the border while padding is inside it.
Practice exercise
Build two cards with equal outer widths, different padding, and visible borders. Explain why border-box keeps their measurements predictable. Apply it to the example above and inspect the result at a narrow and a wide viewport.
Section summary
- The box model consists of content, padding, border, and outer margin.
- Use border-box sizing consistently and remember that margin is outside the border while padding is inside it.
Knowledge check
Where is padding located in the box model?
Reveal the explanation
Padding is internal space; margin is outside the border.
Finish the module lessons, then take the server-scored module quiz to check your understanding.
Practical example
css.demo {
width: 18rem;
padding: 1rem;
border: 4px solid #2563eb;
margin: 1rem;
}
Expected result:
A blue border encloses the demo content and padding; the margin creates space outside that border.
Hands-on practice task
Build two cards with equal outer widths, different padding, and visible borders. Explain why border-box keeps their measurements predictable.
In this section
Other lessons in The CSS Box Model
Broaden your knowledge
Related learning topics
HTML
Learn to structure accessible, meaningful web pages with a complete HTML course from first document to professional application patterns.
JavaScript
Build strong JavaScript foundations with browser examples, focused practice, beginner projects, and module quizzes.