Chapter 8 of 17 · CSS for Beginners — Complete Free Tutorial
The Box Model
Every element on a web page is a rectangle made of nested layers. Once the box model clicks, "why is my element 40 px too wide?" stops being a mystery.
The four layers
| Layer | Property | Question it answers |
|---|---|---|
| Content | width / height | How big is the text area? |
| Padding | padding | How much breathing room inside the border? |
| Border | border | What line surrounds the padding? |
| Margin | margin | How far from neighbouring elements? |
Visualise: a box of content, wrapped in foam (padding), wrapped in a shell (border), with a gap around it (margin).
content-box vs border-box
By default (content-box), width describes content only — padding and border are added on top:
.a {
box-sizing: content-box; /* default */
width: 300px;
padding: 20px;
border: 5px solid;
}
/* rendered width = 300 + 20·2 + 5·2 = 350px */
With border-box, width includes padding + border — what designers mean when they say "300px wide":
.b {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 5px solid;
}
/* rendered width = exactly 300px; content shrinks to fit */
Expected output: two boxes that look identical inside, but .b is 50 px narrower overall.
The universal reset every project needs
*,
*::before,
*::after {
box-sizing: border-box;
}
Put this at the top of your stylesheet, always. It removes 90% of layout surprises.
Margin vs padding — when to use which
| Use padding when… | Use margin when… |
|---|---|
| Space belongs inside the element (button air, card content) | Space separates the element from neighbours |
| The background should extend into the space | The gap should show the parent's background |
| Clicking that area should hit the element | The area should click through to what is behind |
A card: padding for the content inside, margin-bottom (or grid gap) between cards. A button: generous padding, no margin (the flex gap handles spacing).
Shorthand orders
margin: 10px 20px; /* top/bottom 10, left/right 20 */
margin: 10px 20px 30px; /* top 10, left/right 20, bottom 30 */
margin: 10px 20px 30px 40px; /* clockwise: top right bottom left */
padding: 1rem; /* all sides */
border: 1px solid #e2e8f0; /* width style color */
border-radius: 8px 8px 0 0; /* per-corner, clockwise */
Margin collapsing (preview)
Adjacent vertical margins in normal flow collapse into one — the larger wins (margin-top: 30px next to margin-bottom: 10px leaves 30 px, not 40). The full rules live in Display & Margin Collapsing; modern layouts avoid the issue entirely with flexbox/grid gap.
Outline: the border that never shifts layout
button:focus-visible {
outline: 3px solid #4f46e5;
outline-offset: 2px; /* draws outside the border box */
}
Unlike border, an outline does not take space — focus rings never move your layout. That is why accessibility outlines are outlines.
Inspecting boxes in dev tools
- Right-click an element → Inspect.
- Open the Styles/Computed panel — the famous nested-box diagram shows every layer's size with colour coding (content blue, padding green, border orange, margin red).
- Hover the diagram to highlight the area on the page.
Make this a reflex whenever spacing looks wrong.
Practical example: a predictable card
.card {
box-sizing: border-box;
width: 100%;
max-width: 340px;
padding: 1.5rem;
border: 1px solid #e2e8f0;
border-radius: 1rem;
background: white;
}
.card + .card { margin-top: 1.25rem; }
Expected output: cards exactly 340 px wide (border included), 24 px of inner air, stacked 20 px apart — regardless of content.
Common mistakes
- Forgetting the reset — mixing models across components causes cumulative width bugs.
- Using margin for inner spacing — clicking the "gap" hits nothing, and backgrounds stop extending.
- Setting both width and max-width with padding — fine with
border-box, a nightmare without. - Every element gets borders — whitespace and background contrast usually communicate structure better.
Practice checklist
- Rebuild the two-box experiment and verify rendered widths in dev tools.
- Add the universal
border-boxreset to your stylesheet. - Create a card (width, padding, border, radius) and confirm the total width equals your
widthvalue.
Boxes need placement rules — next: Display & Margin Collapsing.
Frequently Asked Questions
CSS (Cascading Style Sheets) is the language that controls how HTML content looks — colours, fonts, spacing, layout, responsiveness and animation. HTML provides structure and meaning; CSS provides presentation. One stylesheet can restyle an entire website at once.
The fundamentals (selectors, box model, Flexbox, media queries) take about 2–3 weeks of daily practice. Grid, animations and architecture come with a little more practice. This 17-chapter series is designed for 3–4 weeks at a relaxed pace — faster if you type out every example.
Yes. HTML first, then CSS, then JavaScript. CSS and HTML together let you build complete, good-looking static pages, and almost every JavaScript framework assumes you already know CSS layout and selectors.
No — CSS is a styling language with selectors and property-value declarations, not variables, loops or conditionals. You can get far without any programming logic, and JavaScript adds the behaviour later.
Flexbox distributes space along one axis (a row or a column) and is ideal for components — toolbars, navbars, card rows. Grid controls rows and columns together and is ideal for page structure and card grids. They are complementary: many layouts use a grid page shell with flex components inside.
Usually one of three reasons: the selector does not match (check class names), a more specific rule wins (use the browser inspector to see computed styles and specificity), or the file is not linked/loads in the wrong order. The specificity chapter explains how the cascade decides.
Every element is a box of four layers: content, padding (inner space), border, and margin (outer space). With box-sizing: border-box, the width you set includes padding and border — the universal reset every project uses.
Use relative units, flexible grids like repeat(auto-fit, minmax(260px, 1fr)), images with max-width: 100%, and mobile-first media queries. The goal is zero horizontal scrolling from 320px up — the responsive chapters show the full workflow.
Not to learn. Frameworks are productivity tools built on the same CSS you are learning here; understanding the fundamentals makes any framework easier to use (and to debug when it fights you). Learn vanilla CSS first, then evaluate frameworks with an informed eye.
JavaScript for interactivity, then a build tool and version control (Git). With HTML, CSS and JavaScript you can build complete websites; after that, frameworks like React or server-side tools like Laravel Blade build on these fundamentals.