The Box Model, Flexbox & Grid
Every element is a box — master the model, then lay out with Flexbox (1D) and Grid (2D).
The CSS Box Model
Every element is a box made of four layers, from outside in:
- Margin — space outside the border; pushes other elements away.
- Border — the visible frame around the element.
- Padding — space between the content and the border.
- Content — the actual text, image or other content.
Box Model in CSS — box-sizing Best Practice
- The default
box-sizingiscontent-box— width counts only the content. - Setting
box-sizing: border-boxincludes padding + border in the width. - 2025 best practice: apply
border-boxglobally in every project.
/* 2025 global reset — every project */
*, *::before, *::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.box {
width: 300px;
padding: 20px; /* inner space */
border: 2px solid #00B4D8;
margin: 1rem auto; /* centered */
}
/* With border-box: total width stays 300px
Without: 300px + 40px + 4px = 344px */
/* 2025: logical properties */
.card {
padding-block: 1rem; /* top + bottom */
padding-inline: 1.5rem; /* left + right */
margin-inline: auto; /* center */
}
CSS Flexbox — Modern 1D Layout
| Property | Values / Purpose |
|---|---|
display: flex | Turns a container into a flex container |
flex-direction | row (default) | column | row-reverse | column-reverse |
justify-content | Aligns items along the main axis: flex-start | center | flex-end | space-between | space-evenly |
align-items | Aligns items along the cross axis: stretch | center | flex-start | flex-end | baseline |
gap | Space between flex items (no margins needed!) |
flex: 1 | Item grows to fill available space |
flex-wrap: wrap | Items flow to the next row when needed |
/* Flex container */
.nav {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}
/* Flex item sizing */
.sidebar { flex: 0 0 260px; } /* fixed */
.main { flex: 1; } /* fills the rest */
/* Centering — the easiest way in 2025 */
.hero {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* Wrapping card grid */
.card-row { display: flex; flex-wrap: wrap; gap: 1.5rem; }
.card { flex: 1 1 250px; } /* grow, shrink, min 250px */
CSS Grid — Modern 2D Layout
| Feature | Purpose |
|---|---|
display: grid | Creates a 2D grid container |
grid-template-columns / -rows | Define tracks |
fr unit | Fractional share of available space — 1fr 2fr is 33% / 67% |
gap | Row and column gaps |
grid-area | Name areas for semantic placement |
repeat(auto-fill, minmax(250px, 1fr)) | Responsive grid with NO media queries |
subgrid | 2025: nested items align to parent grid tracks |
/* Basic grid */
.layout {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-rows: auto 1fr auto;
gap: 1rem;
}
/* Named areas — like a blueprint */
.page {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
/* Responsive grid — NO media queries! */
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 1.5rem;
}
Activity — complete the code
Complete the code correctly to mark this lesson as complete automatically.
Complete the global reset and the flex container.
*, *::before, *::after {box-sizing: ;}.nav {display: ;justify-content: ;gap: 1rem;}