DEV Community

Narasimma Radhakrishnan
Narasimma Radhakrishnan

Posted on

Grid vs Flex: Which One Should You Use? 🤔

As a beginner in CSS, I was confused between Flexbox and Grid. Both are powerful layout tools, but they solve different problems.

Flexbox = One Direction

Use Flexbox when you want to arrange items in a row or a column.

Example: Navigation bar, buttons, or a row of cards.
.container {
display: flex;
justify-content: center;
align-items: center;
}

Grid = Two Dimensions

Use Grid when you need both rows and columns.

Example: Image gallery, dashboard, or product layout.

.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}

css #webdev #beginners #frontend

Top comments (1)

Collapse
 
markmuslimov profile image
Mark •

Let's goooo. I just learned it the other week aswell.
Tbh I kind of used Flex to make a

  • point - point )Flex here
  • point - point )flex here

But after learning GRID, I could finally use it to not do 2x whole flex and shortify my code.

  • point - point )
  • point - point ) GRID

Knowing both and using them depending on what you need is the trully satisfying.