The Box Model and Layout Basics
Understand the box model and the block/inline/inline-block distinction, then apply real CSS spacing and borders to the portfolio page.
Every Element Is a Box
Here’s the single fact that unlocks CSS: every HTML element the browser renders is a rectangular box. A paragraph is a box. A heading is a box. An image is a box. CSS’s job is almost entirely about controlling the size, spacing, and position of these boxes.
Your portfolio page from Module 1 is currently a stack of default-styled boxes with no intentional spacing. This lesson gives you the vocabulary and the real CSS to control that — starting with the box model, the single most important concept in all of CSS.
Linking a Stylesheet
CSS lives in its own file. Create styles.css next to index.html, and link it from the <head>:
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Jordan Lee — Portfolio</title>
<link rel="stylesheet" href="styles.css" />
</head>
A CSS rule has a selector (what to style) and a declaration block (how to style it):
h1 {
color: #3b63f5;
font-size: 2.5rem;
}
This says: find every <h1>, make its text blue, make its font size 2.5rem. rem is a relative unit — 1rem equals the browser’s default font size (usually 16px), so 2.5rem is 40px, but it scales if a user changes their browser’s base font size. Prefer rem for font sizes over hardcoded px.
The Box Model, Piece by Piece
Every box is built from four concentric layers, from the inside out:
- Content — the actual text or image
- Padding — transparent space inside the border, cushioning the content
- Border — a visible (or invisible) line around the padding
- Margin — transparent space outside the border, pushing other elements away
Two properties control total box size:
.card {
width: 300px;
padding: 20px;
border: 2px solid #3b63f5;
}
By default, width: 300px sets the width of the content only — padding and border get added on top, so this box actually renders at 300 + 20 + 20 + 2 + 2 = 344px wide. That surprises almost everyone at first, which is exactly why the very next section exists.
Fix the Sizing Surprise: box-sizing
The fix is one property, applied globally, at the very top of every stylesheet you’ll ever write:
* {
box-sizing: border-box;
}
border-box changes width to mean the total rendered width — padding and border are now included inside that number, not added on top. That .card above, with box-sizing: border-box, renders at exactly 300px wide, full stop. Add this rule first, before anything else, in styles.css.
Block, Inline, and Inline-Block
Every box also has a display type that controls how it sits next to other boxes.
| Display type | Behavior | Examples |
|---|---|---|
block | Takes the full available width, starts on a new line | <div>, <h1>–<h6>, <p>, <section> |
inline | Only as wide as its content, sits in the text flow, ignores width/height/vertical margin | <a>, <span>, <strong> |
inline-block | Sits in the text flow like inline, but respects width, height, and margin | set explicitly with display: inline-block |
This trips people up constantly: you set width and margin-top on an <a> tag and nothing happens, because links are inline by default. The fix is either display: inline-block or, more often in modern CSS, display: flex on the parent — which you’ll learn next lesson.
Styling the Portfolio
Step 1 — Start styles.css with the reset and some base typography:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: system-ui, -apple-system, sans-serif;
line-height: 1.6;
color: #1a1a2e;
max-width: 800px;
margin: 0 auto;
padding: 2rem 1.5rem;
}max-width plus margin: 0 auto is the classic “centered column” pattern — the body is capped at 800px wide and centered horizontally on larger screens.
Step 2 — Style the header:
header {
padding-bottom: 1.5rem;
margin-bottom: 2rem;
border-bottom: 2px solid #e5e7eb;
}
header h1 {
color: #3b63f5;
font-size: 2.25rem;
}Step 3 — Give each project <article> a card look, using the box model directly:
article {
padding: 1.25rem;
margin-bottom: 1rem;
border: 1px solid #e5e7eb;
border-radius: 8px;
}
article h3 {
margin-bottom: 0.5rem;
}Step 4 — Reload index.html in the browser. You should see visible card borders around each project, breathing room between sections, and a blue heading — all controlled by padding, margin, and border, exactly as diagrammed above.
/* Two block elements stacked vertically */
h2 { margin-bottom: 20px; }
p { margin-top: 30px; }You might expect 50px of gap between them (20 + 30). In reality, adjacent vertical margins on block elements collapse — the browser uses only the larger of the two, so you get 30px, not 50px. This is a real, specified CSS behavior, not a bug. It only applies to vertical margins on normal block flow (not padding, and not once you switch a parent to display: flex, which you’ll do next lesson and which sidesteps this entirely). Until then, don’t assume margins simply add — test in the browser.
Up Next
You can now size and space individual boxes correctly. But arranging multiple boxes into a real layout — a row of project cards, a header with nav links pushed to one side — needs a layout system. Next lesson: Flexbox, plus your first responsive breakpoint.