Not Flex Alone! Grids Are Power!

Fri, May 30, 2025 - 3 min read
css grid

🌐 Not Flex Alone! Grids Are Power!

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.


Why People Fear Grids

1. Unfamiliar Syntax

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;
}

2. Stereotypes

There’s a belief that Grid is something complex and only needed for large projects. In fact, Grid simplifies even simple layouts.

3. Fear of Change

It’s always scary to get used to something new. But those who don’t take risks don’t drink champagne!


Grid Advantages Over Flex

1. Two-Dimensional Layout

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 */
}

2. Positioning Control

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 */
}

3. Responsiveness Without Media Queries

.layout {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  grid-gap: 20px;
}

One line of code replaces several media queries!


When to Use Grid vs Flex

Use Grid when:

✅ You need two-dimensional layout (rows and columns)
✅ Creating galleries, tables, forms
✅ You need precise element positioning
✅ Laying out designs with fixed areas

Use Flex when:

✅ You need one-dimensional layout (only rows OR columns)
✅ Creating navigation menus
✅ Aligning elements along one axis
✅ You need flexibility in element sizes


Simple Grid Usage Examples

1. Product Cards

.products {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  grid-gap: 20px;
}

2. Contact Form

.form {
  display: grid;
  grid-template-columns: 1fr 2fr;
  grid-gap: 10px;
}
 
.form label {
  grid-column: 1;
}
 
.form input,
.form textarea {
  grid-column: 2;
}

3. Complex Layout

.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; }

Personal Experience: How Grid Changed My Layout

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:

  1. Use Grid for complex layouts
  2. Keep Flex for simple components
  3. Don’t fear experimenting with new properties

Result: faster layout, fewer bugs, easier code maintenance.


Conclusion: Grid Isn’t Scary

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.