Many people fear Grids. This especially applies to young developers who are used to laying out with Flex, or conversely, older developers who only know Flex and are afraid of Grids.
In fact, there’s no need to be afraid of Grids. You don’t go to Frontend without going, as the famous saying goes. Use Grids — they’re not that scary. It’s convenient to lay out with them, and convenient to edit everything with them.
Many are used to Flexbox and think it solves all tasks. But in fact, Grid offers much more possibilities.
/* Flexbox - one dimension */
.container {
display: flex;
flex-direction: row;
}
/* Grid - two dimensions */
.container {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: auto;
}There’s a belief that Grid is something complex and only needed for large projects. In fact, Grid simplifies even simple layouts.
It’s always scary to get used to something new. But those who don’t take risks don’t drink champagne!
Flex works in one direction (row or column), while Grid works simultaneously along two axes.
/* Hard to do this with Flex */
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 20px;
}
/* Elements automatically align in a grid */
.item {
/* No additional styles needed */
}With Grid, you can easily control element positioning:
.item:nth-child(2) {
grid-column: 1 / 3; /* Takes up the first two columns */
grid-row: 2; /* In the second row */
}.layout {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
grid-gap: 20px;
}One line of code replaces several media queries!
✅ You need two-dimensional layout (rows and columns)
✅ Creating galleries, tables, forms
✅ You need precise element positioning
✅ Laying out designs with fixed areas
✅ You need one-dimensional layout (only rows OR columns)
✅ Creating navigation menus
✅ Aligning elements along one axis
✅ You need flexibility in element sizes
.products {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
grid-gap: 20px;
}.form {
display: grid;
grid-template-columns: 1fr 2fr;
grid-gap: 10px;
}
.form label {
grid-column: 1;
}
.form input,
.form textarea {
grid-column: 2;
}.layout {
display: grid;
grid-template-areas:
"header header header"
"sidebar main aside"
"footer footer footer";
grid-template-columns: 200px 1fr 150px;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }I used to fear Grid too. I laid out everything with Flex and thought that was the pinnacle of mastery. But once I needed to lay out a complex gallery with different sized cards.
With Flex it took 3 days and a bunch of workarounds. I decided to try Grid — did it in 30 minutes, and the code was 10 times cleaner.
Since then I:
Result: faster layout, fewer bugs, easier code maintenance.
Not flex alone! Grid is power that can significantly simplify your work. Don’t be afraid to experiment with new technologies.
Grid is like a Swiss Army knife for layout. Universal and very convenient.
Next time you’re laying out a complex design, remember: you can save hours of work just by using Grid. Your code and your colleagues will thank you.