Here is what you'll build in this lesson: the three Mist Homestay room cards, neatly side by side on a laptop, no wider on a big monitor, and one under the other on a phone. Two tools make that happen: a container and a grid.
A container is a box that limits how wide a page's content gets. A grid is the set of invisible columns inside the container that decide where each element sits. Picture a display cabinet in a corner shop. The cabinet has a fixed width, like the container. Inside it are shelves divided into equal compartments, like the columns. Between the compartments are dividing boards, which design calls gutters. The items in the cabinet are the elements on the page: a big item takes two or three compartments, a small one takes one, but everything stays straight because the compartments are the same.
Three room cards
Mrs. Sri now has three rooms, and all three sit side by side on the booking page:
| Room | Price per night | Guests |
|---|---|---|
| Terrace Room | $35 | 2 |
| Attic Room | $27.50 | 2 |
| Garden Room | $42.50 | 4 |
With no width limit, these three cards stretch across the whole screen. On a 1920px monitor one card can be more than 600px wide, and the eye has to travel a long way to compare prices.
Container: the page's width limit
The container is written as one class, .container:
.container {
max-width: 1200px; /* content at most 1200px wide */
margin: 0 auto; /* leftover space split evenly left and right */
padding: 0 1.5rem; /* 24px, so content doesn't touch a phone's edge */
}
max-width: 1200pxmeans "narrower is fine, wider is not". On a phone the container gets narrow with the screen. On a big monitor it stops at 1200px.margin: 0 auto: 0 top and bottom,autoleft and right. The browser splits the leftover space equally between left and right, so the container sits in the middle.padding: 0 1.5remputs 24px between the content and the left and right edges. With the defaultbox-sizing(content-box), 1200px is the content width, and the padding sits outside it. So the box is 1200 + 48 = 1248px in total.
The result at four screen widths (this example uses body { margin: 0 }, so the page has no default browser margin):
| Screen | Container content width | Empty space on each side |
|---|---|---|
| 375px (phone) | 375 - 48 = 327px | 24px padding |
| 768px (tablet) | 768 - 48 = 720px | 24px padding |
| 1280px (laptop) | 1200px | (1280 - 1248) / 2 = 16px, plus 24px padding = 40px |
| 1920px (monitor) | 1200px | (1920 - 1248) / 2 = 336px, plus 24px padding = 360px |
Ordinary websites usually use a container of around 1024 to 1280px, and the exercise in this lesson uses 1200px. Bootstrap 5 (a popular ready-made set of CSS), for example, uses 1140px for screens of 1200px and up, and 1320px for screens of 1400px and up. For an article page full of long text, limit the text width to about 65ch, as in the font size lesson.
The 12-column grid
Why 12? Because 12 divides by 1, 2, 3, 4, 6 and 12. From one grid you can make 2 equal columns (6 + 6), 3 columns (4 + 4 + 4), 4 columns (3 + 3 + 3 + 3), or main content plus a sidebar, a side column (8 + 4). A 10-column grid can't be split into even thirds. That's why the 12-column grid is used by Bootstrap and many design systems (a product's set of design rules and components).
Here is the code: three cards inside .grid, and .grid inside .container:
<div class="container">
<div class="grid">
<div class="card"><h2>Terrace Room</h2><p>$35 / night</p></div>
<div class="card"><h2>Attic Room</h2><p>$27.50 / night</p></div>
<div class="card"><h2>Garden Room</h2><p>$42.50 / night</p></div>
</div>
</div>
<style>
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr); /* 12 equal columns */
gap: 1.5rem; /* 24px gutter */
}
.card { grid-column: span 4; } /* each card takes 4 of the 12 columns */
</style>
display: gridturns.gridinto a grid, and its children (the elements directly inside it, here the cards) become its items.repeat(12, 1fr)makes 12 columns.frmeans one share of the space that's left, so these 12 columns are equally wide.gapis the space between columns, which design calls the gutter.grid-column: span 4makes one card use 4 columns in a row. Three cards × 4 columns = 12, exactly one row.
Calculating a card's width
Guess first: on a 1280px laptop, how many px wide is one card that takes 4 columns? Now calculate it. A 12-column grid has 11 gutters between its columns. So:
- column width = (content width - 11 × gap) / 12
- width of a card spanning n (a card that uses n columns) = n × column width + (n - 1) × gap
On a 1280px laptop the container content is 1200px and the gap is 24px:
- column = (1200 - 11 × 24) / 12 = 936 / 12 = 78px
- card spanning 4 = 4 × 78 + 3 × 24 = 312 + 72 = 384px
One card is 384px wide. Was your guess close?
All the numbers above use content-box. If your project uses the box-sizing: border-box rule from the Box Model lesson, the numbers change. max-width: 1200px now includes the padding, so the container's content is 1200 - 48 = 1152px:
- column = (1152 - 11 × 24) / 12 = 888 / 12 = 74px
- card spanning 4 = 4 × 74 + 3 × 24 = 296 + 72 = 368px
We measured it in Chromium on a 1280px screen. Without that rule, the container is 1248px and a card 384px. With that rule, the container is 1200px and a card 368px. On a phone and a tablet the numbers stay the same, because the container there is narrower than 1200px.
Compare that with repeat(3, 1fr), which makes just three columns: (1200 - 2 × 24) / 3 = 1152 / 3 = 384px. Exactly the same. So when a grid holds only equally wide cards, repeat(3, 1fr) is a short way of writing the same thing. The exercise uses that short way. A 12-column grid is useful once your page mixes widths, for example a room description spanning 8 next to a booking box spanning 4.
Gutters and small screens
Common gutters come from the spacing scale, the spacing system from the Box Model lesson (gaps in multiples of 4 and 8): 16px for dense pages, 24px for most pages, 32px for roomy ones. What matters is one value for the whole page.
The trouble starts on phones. On a 375px screen the container content is 327px. So a column = (327 - 264) / 12 = 5.25px, where 264 = 11 × 24. A card spanning 4 = 4 × 5.25 + 72 = 93px. The room name "Terrace Room" doesn't fit on one line in that. On phones, let each card use all 12 columns:
.card { grid-column: span 12; } /* phone: 327px, one card per row */
@media (min-width: 768px) {
.card { grid-column: span 4; } /* 768px and up: three cards side by side */
}
- The first rule applies on every screen: one card as wide as the container.
@media (min-width: 768px)only applies on screens that are 768px wide or wider. On a 768px tablet a column = (720 - 264) / 12 = 38px, so a card spanning 4 = 4 × 38 + 72 = 224px.
⚠️ Containers that stick to the edge, cards that overflow
- Forgetting
margin: 0 auto. The container stops at 1200px, but it stays on the left. On a 1920px monitor, all 672px of empty space collects on the right. - Forgetting the container's padding. On phones, the card text touches the screen edge.
- Spans adding up to more than 12 in a row. Span 4 + 4 + 6 = 14: only 4 columns are left in the row, so the third card drops to a new row.
- Different gaps everywhere. 20px in one section, 30px in another. Pick one gutter from your spacing scale and use it in every grid.
- Columns narrower than their content. The grid doesn't prevent this. A card spanning 3 on a 375px phone really is only 3 × 5.25 + 2 × 24 = 63.75px. Any word longer than that goes past the edge of its card. Calculate first, then pick the span.
TL;DR: A container is a box that limits how wide the page's content gets: max-width: 1200px (ordinary websites use 1024 to 1280px), margin: 0 auto to centre it, and padding: 0 1.5rem so content doesn't touch the edges. A grid is the set of invisible columns that decide where elements sit. A 12-column grid (display: grid; grid-template-columns: repeat(12, 1fr)) divides into 2, 3, 4 and 6. The space between columns is called the gutter (gap), for example 24px. Column width = (content width - 11 × gap) / 12, and a card spanning n (using n columns) = n × column + (n - 1) × gap. At 1280px a column is 78px and a card spanning 4 is 384px, the same as a card in a three-column grid, repeat(3, 1fr). On a 375px phone span 4 is only 93px, so use span 12 and move up to span 4 with @media (min-width: 768px), a rule that only applies on screens 768px and wider.
⚡ A container: the content's width limit
Write:
.container {
max-width: 1200px; /* content at most 1200px wide */
margin: 0 auto; /* leftover space split evenly left and right */
padding: 0 1.5rem; /* 24px, so content doesn't touch a phone's edge */
}
Meaning: A container is a box that limits how wide the page's content gets. Ordinary websites limit the container to around 1024 to 1280px, here 1200px. max-width means narrower is fine, wider is not. margin: 0 auto puts it in the middle.
⚡ 12 columns because 12 divides easily
Write:
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr); /* 12 equal columns */
gap: 1.5rem; /* 24px gutter */
}
.card { grid-column: span 4; } /* each card takes 4 of the 12 columns */
Meaning: A grid is the set of invisible columns that decide where elements sit. repeat(12, 1fr) makes 12 equal columns, and 12 divides by 2, 3, 4 and 6. grid-column: span 4 makes one card use 4 columns. The space between columns (gap) is called the gutter in design. Use one gutter value for the whole page.
⚡ Calculate first, then pick the span
Guess first: on a 1280px laptop the container content is 1200px and the gap is 24px. How wide is a card spanning 4 (using 4 of the 12 columns)?
384px. There are 11 gaps between 12 columns, so one column is (1200 - 11 × 24) / 12 = 78px. The card is 4 columns plus the 3 gaps between them: 4 × 78 + 3 × 24.
Meaning: On a 375px phone, the container content is 327px (375 minus 2 × 24 of padding), and span 4 is only 93px, so a room name like "Terrace Room" doesn't fit on one line. Use span 12 on phones, then move up to span 4 with @media (min-width: 768px), a rule that only applies on screens 768px and wider.