CSS · Lesson 7 of 12 · 4 min
CSS Grid Fundamentals
Grid is for two-dimensional layout. Know when to use it over Flexbox and how to structure a full page with it.
After this, you will be able to answer the most common system design question for CSS: "How would you build this page layout?" You'll use Grid for the macro layout and explain the trade-offs of different placement strategies. This is a core competency, not an obscure "gotcha."
When to use Grid (and when not to)
The first question you'll get is "Grid vs. Flexbox." Your answer must be immediate and clear.
Flexbox is for one-dimensional layout. It arranges items in a single line, either a row or a column. Use it for component internals: aligning items in a navigation bar, spacing out buttons in a card footer, or vertically centering content.
Grid is for two-dimensional layout. It arranges items in rows and columns simultaneously. Use it for the overall page structure: a header, footer, main content area, and sidebars.
The expert answer: "I use them together. Grid for the page's macro-layout, and Flexbox for the micro-layouts inside individual grid cells. For example, the page might be a grid with a
mainandasidearea. Inside theaside, I'd use Flexbox to align a list of links."
This shows you see them as complementary tools, not competitors.
Defining the grid structure
To create a grid, you need a container and some items. The container gets display: grid, and its direct children become the items.
The structure is defined on the container with grid-template-columns and grid-template-rows. The values you provide are the most important part.
The fr unit is your most-used tool. It stands for "fraction" and represents a fraction of the available space in the grid. If one column is 100px and two others are 1fr, the fixed column takes its 100px first, and the remaining space is divided equally between the two fractional columns.
The trap: Candidates often mistakenly think 1fr is just like a percentage. It's not. It's smarter because it works with the space left over after fixed-size and content-sized tracks are accounted for.
For responsive grids, the repeat() and minmax() functions are essential. This one line is a pattern you should memorize:
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
This creates as many columns as will fit. Each column will be at least 250px wide, and they will grow equally to fill any remaining space. auto-fit will collapse empty tracks, allowing the filled ones to grow, which is usually what you want for a responsive gallery.
Finally, use gap to create space between grid items. gap: 16px; is cleaner and more reliable than using margins on the items themselves.
Placing items: Lines vs. Areas
Once you have a grid, you have two ways to place items: by line number or by named area.
Line-based placement is explicit. Grid lines are numbered starting from 1. You can place an item from column line 1 to column line 3 with grid-column: 1 / 3;. You can also make it span a certain number of tracks with grid-column: span 2;. This is fine for simple components, but it quickly becomes unmanageable for a full page layout.
Area-based placement is more declarative and robust. You name your grid cells on the container, then assign items to those names. This is the preferred method for page layouts.
- On the container:
grid-template-areaslets you draw your layout with strings. - On the item:
grid-areaassigns that item to a name you defined.
The main benefit is for responsive design. In a media query, you just redefine the grid-template-areas string to completely change the layout. You don't have to touch any of the individual items. This is a massive win for maintainability.
The go-to example: A responsive page layout
An interviewer might show you a wireframe and ask you to code it. The "holy grail" layout is a perfect use case for grid-template-areas.
Here's the HTML structure. No extra wrappers needed.
<div class="page-layout">
<header>Header</header>
<nav>Navigation</nav>
<main>Main Content</main>
<aside>Sidebar</aside>
<footer>Footer</footer>
</div>
And here's the CSS that makes it work. Notice how the media query just re-shuffles the named areas.
.page-layout {
display: grid;
gap: 1.5rem;
/* Define named areas for a desktop layout */
grid-template-areas:
"header header header"
"nav main aside"
"footer footer footer";
grid-template-columns: 1fr 3fr 1fr;
grid-template-rows: auto 1fr auto;
}
/* Assign each element to its named area */
header { grid-area: header; }
nav { grid-area: nav; }
main { grid-area: main; }
aside { grid-area: aside; }
footer { grid-area: footer; }
/* On smaller screens, stack everything in a single column */
@media (max-width: 768px) {
.page-layout {
grid-template-areas:
"header"
"nav"
"main"
"aside"
"footer";
/* A single column that takes up all the space */
grid-template-columns: 1fr;
grid-template-rows: auto; /* Let rows size by content */
}
}
This example demonstrates a deep understanding of Grid's strengths: semantic structure, separation of concerns, and powerful responsive capabilities with minimal code. If you can write this from memory and explain it, you're in a great position.
Try it
Take the page layout example above. Modify it to handle a third state for very wide screens (e.g., min-width: 1200px). In this state, the navigation should move from the side to sit horizontally inside the header. You will need to adjust grid-template-areas on both the .page-layout and potentially nest a grid or flexbox inside the header.
Next: Learn how to handle implicit grids and fine-tune alignment with justify-items and align-content in "Advanced CSS Grid & Area Layouts".
Lock it in — apply this lesson under interview conditions.