Sub topic 7
Fixed and fluid grids compared
This chapter assumes you know what columns, gutters, margins and a container are, and that you can write basic CSS Grid. Here you will compare the two ways a grid can respond to the screen width, and see why most real pages combine them.
What differs
A grid is fixed or fluid depending on how its column widths are set.
- Fixed grid: column widths are absolute units such as
pxorrem. The grid is the same width on every screen. - Fluid grid: column widths are proportions, such as
frunits or percentages. The columns stretch and shrink with their container.
Gutters can be fixed or fluid independently of the columns. Most fluid grids keep a fixed gutter (for example 1.5rem) and let only the columns flex.
A fixed grid
.grid-fixed {
display: grid;
grid-template-columns: repeat(12, 4rem);
column-gap: 1.5rem;
width: 64.5rem; /* 12 × 4rem + 11 × 1.5rem */
margin-inline: auto;
}The width is arithmetic: twelve columns plus eleven gutters. The layout is predictable, and you can design against exact measurements. The cost is that 64.5rem is about 1032px at the default font size. On a 390px phone, the page scrolls sideways.
A fluid grid
.grid-fluid {
display: grid;
grid-template-columns: repeat(12, minmax(0, 1fr));
column-gap: 1.5rem;
}Every column gets an equal share of whatever space remains after the gutters. The page fits any width, but the columns have no upper limit. On a very wide monitor, a text column can grow until lines become hard to read.
The minmax(0, 1fr) form matters. A plain 1fr means minmax(auto, 1fr), so a column cannot shrink below its content. A long URL or a wide image can then push the grid past its container. Setting the minimum to 0 lets the column shrink.
Comparison
| Property | Fixed | Fluid |
|---|---|---|
| Column width unit | `px`, `rem` | `fr`, `%` |
| Fits small screens | No, it overflows | Yes |
| Fills large screens | No, it leaves empty space | Yes, it can stretch too far |
| Layout predictability | High | Lower |
| Typical failure | Horizontal scrolling | Overly long lines |
The usual answer: fluid columns in a capped container
Most sites combine both. The columns are fluid, and the container supplies a fixed maximum width, as in the earlier container chapter.
.page {
max-width: 72rem;
margin-inline: auto;
padding-inline: 1rem;
}
.grid {
display: grid;
grid-template-columns: repeat(12, minmax(0, 1fr));
column-gap: 1.5rem;
}Below 72rem the grid is fluid and the margins stay at 1rem. Above 72rem the container stops growing, the columns reach their largest size, and the extra space goes into the side margins.
Common errors
- Mixing a fixed
widthon the grid with1frcolumns and expecting it to adapt. The fixed width wins. - Using percentage columns plus a separate
marginfor spacing. Percentages and margins add up to more than 100%, and the last column wraps or overflows. With CSS Grid, usegapinstead. - Forgetting
minmax(0, 1fr)and then wondering why one column refuses to shrink. - Leaving a fluid grid uncapped and getting text lines of 150 characters or more on wide screens.
A fluid grid with a cap still has only one layout shape, so on a narrow screen twelve columns can become too small to use. The next chapter fixes that by changing the grid at different breakpoints.
Exercise
A page uses grid-template-columns: repeat(12, 5rem) with a 1.5rem gap. It looks right on desktop but scrolls sideways on phones. Name the grid type and give one change that fixes it while keeping the layout from stretching on wide monitors.
Show answer
This is a fixed grid, with a total width of 76.5rem. Change the columns to repeat(12, minmax(0, 1fr)) and put the grid inside a container with max-width: 72rem, margin-inline: auto and padding-inline: 1rem. The columns then shrink on phones and stop growing on wide screens.