Answer 34 of 50
Grid Systems in Web Design: Creating Consistency and Professional Appearance
A grid gives every element a reason to sit where it does: shared column edges, consistent gutters, and a rhythm that makes a page look organized rather than assembled. On the web, a grid is a set of relationships that adapt to the screen, not fixed positions. Building one means choosing a column count, defining gutters and margins, implementing it with CSS Grid, and deciding how it responds to both the viewport and the space a component sits in.
Why twelve columns, and when to use another number
Twelve divides evenly by 2, 3, 4, and 6, so halves, thirds, quarters, and sixths all fit without remainders: a sidebar can span 3 columns and the main content 9, or three equal cards can span 4 each. The number became familiar through frameworks. The 960 Grid System offered “two variants: 12 and 16 columns”. The Bootstrap 1.4.0 documentation describes its default as “a 940px wide 16-column grid” based on the 960 grid system, and the Bootstrap 2 documentation describes a grid that “utilizes 12 columns”.
Twelve is a convention, not a rule. A portfolio built around large images may need only 6 columns; a dense dashboard may want 16 for finer control. Pick the number that divides your common layouts cleanly, then use it consistently.
Gutters, margins, and a container
A grid needs three decisions besides the column count:
| Decision | What it controls | Starting point to test |
|---|---|---|
| Container maximum width | How wide content can get on large screens | A width that keeps body text at a readable line length |
| Gutter | Space between columns | A value from your spacing scale, such as 16 or 24 pixels |
| Side margins | Space between content and the screen edge | Smaller on phones, larger on desktop |
Tie gutters and margins to the same spacing scale as the rest of the design, so the grid and the components inside it share one rhythm. How spacing scales work is covered in how insufficient whitespace harms user experience.
CSS Grid for the page, Flexbox inside components
MDN’s guide to how grid layout relates to other layout methods puts the difference simply: “flexbox was designed for layout in one dimension”, a row or a column, while “Grid was designed for two-dimensional layout”, rows and columns at the same time. Layouts can use both: Grid for the page structure, Flexbox for a row of buttons or the contents of a card.
A twelve-column grid with 20-pixel gutters takes three lines:
.container {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 20px;
}
Items then follow the grid automatically or span a set number of columns.
Responsive behavior: two strategies
There are two ways to adapt a twelve-column grid to smaller screens, and it helps to pick one deliberately:
- Keep 12 columns and change spans. An item spans 4 columns on desktop, 6 on a tablet, and 12 on a phone, so a row of three becomes two, then one.
- Change the column count. Redefine the grid at each breakpoint, for example 12 columns on desktop, 8 on a tablet, and 4 on a phone, and keep spans proportional.
The first strategy, written desktop-first with max-width media queries:
.item {
grid-column: span 4;
}
@media (max-width: 768px) {
.item {
grid-column: span 6;
}
}
@media (max-width: 480px) {
.item {
grid-column: span 12;
}
}
Content may also reflow: an image beside text on desktop can move above it on a phone.
Respond to the container, not only the viewport
A card used in both a narrow sidebar and a wide main column needs different layouts in each place, which a viewport media query cannot tell apart. Container queries solve this. MDN’s guide to container queries explains that they “enable you to apply styles to an element based on certain attributes of its container”, and the @container rule has “been available across browsers since February 2023.”
<ul class="card-list">
<li class="item">...</li>
<li class="item">...</li>
<li class="item">...</li>
</ul>
.card-list {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 20px;
container-type: inline-size;
}
.card-list .item {
grid-column: span 4;
}
@container (width < 500px) {
.card-list .item {
grid-column: span 12;
}
}
The list is both the twelve-column grid and the container, so its items switch from three per row to one per row whenever the list itself is narrower than 500 pixels, whether it sits in a sidebar or in the main column.
Subgrid: what it inherits and what it aligns
Subgrid lets a nested element use its parent’s tracks instead of defining its own, so inner content lines up with the outer grid. Two details decide whether it does what you expect. First, MDN’s subgrid guide explains that a subgrid adopts only the tracks it spans: “if you use grid-template-columns: subgrid and the nested grid spans three column tracks of the parent, the nested grid will have three column tracks of the same size as the parent grid.” Second, lining up headers, bodies, and footers across side-by-side cards is a row problem, so it needs row subgrid:
.cards {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}
Each card spans three rows of the parent, so the tallest header in a row of cards sets the header height for all of them. The web.dev article on CSS subgrid shows the same effect: “After subgrid, aligning the variably sized content is possible.” MDN reports subgrid “available across browsers since September 2023.”
Breaking the grid on purpose
A grid that is followed everywhere can look predictable. Deliberate breaks, such as a hero image that bleeds to the edge while text stays in the grid, or a pull quote that spans an unexpected width, stand out precisely because everything else aligns. Test every break at several widths: an overlap that works on desktop can collide on a tablet.
Vertical rhythm
Columns get most of the attention, but vertical spacing matters too. A strict print-style baseline grid is hard on the web, where text reflows, images vary in height, and components carry their own padding. A practical alternative is approximate rhythm: base vertical spacing on multiples of the body line height, for example 24 pixels for 24-pixel lines, and accept that not every element locks to it.
Document the grid
A grid that lives only in one designer’s head drifts. Document the column count and behavior at each breakpoint, gutter and margin values, the container maximum width, how common layouts span columns, and when breaks are allowed. Show the grid overlaid on real pages, with correct and incorrect examples, and keep overlays available in design files and in development so misalignment is caught before release. How a grid fits into a wider design system is covered in the long-term benefits of design systems.
Where a grid matters less
Single-page microsites and experimental art portfolios may not need grid infrastructure, and some brand sites reject grids as part of their message. Those are choices to make knowing what the grid would have provided. For a commercial site with many page types, a grid saves decisions on every new page.
From print to the web
Josef Müller-Brockmann’s book Grid Systems in Graphic Design is a reference for proportion and order in print. Print grids fix positions on a known page; web grids define relationships on a canvas whose size, fonts, and content length you do not control. Nathan Ford’s 2014 A List Apart article Content-out Layout argues for building proportions from the content outward, using ratios rather than a predefined canvas. For a hands-on introduction to defining a grid container, Rachel Andrew’s Smashing Magazine article Understanding CSS Grid: Creating A Grid Container walks through the basics.
Questions teams ask
Should we use a framework like Bootstrap or build our own grid?
A framework gives you a grid plus components and conventions, which suits fast projects that accept its defaults. A custom grid with CSS Grid gives more control and less code. For a distinctive design, a custom grid can save you from overriding framework defaults later.
How do we choose between CSS Grid and Flexbox for a layout?
If you care about rows and columns at the same time, use Grid. If items flow along one axis, use Flexbox. A page with distinct regions is Grid territory; a row of buttons is Flexbox territory.
Our CMS content varies in length. How do we keep the grid intact?
Design for a range, not for one length. Test with very short and very long content, and use row subgrid or flexible heights so cards in a row stay aligned when their content differs.
Pick a column count that divides your common layouts, tie gutters to your spacing scale, choose one responsive strategy, use container queries for components that move between contexts, give subgrid the rows it needs to align cards, and document the grid so the whole team uses it.