DEV Community

Cover image for Back to Basic: Should we use Flexbox or Grid?

Back to Basic: Should we use Flexbox or Grid?

Theodorus Clarence on May 28, 2021

Back to Basic will answer some of my questions when I first started learning CSS. Personally, I think when you start learning CSS, it will be very ...
Collapse
 
arvindpdmn profile image
Arvind Padmanabhan

The one-line summary says it all! Nice examples to highlight the differences. I would like to quote from devopedia.org/css-grid-layout :
"CSS Grid doesn't replace Flexbox. Grid items can be flex parents and vice versa."

Collapse
 
theodorusclarence profile image
Theodorus Clarence

Thanks for your addition!

Collapse
 
ivan_jrmc profile image
Ivan Jeremic

It does

Collapse
 
ihorvorotnov profile image
Ihor Vorotnov • Edited

Your example #2 (divide page into parts) won't work in Safari. There's no need to hack around with 100% width, there ape specific flexbox properties for that - flex-grow and/or flex-basis:

.content {
  width: 50%;
  flex-grow: 1;
}
Enter fullscreen mode Exit fullscreen mode
.content {
  width: 50%;
  flex-basis: 50%;
}
Enter fullscreen mode Exit fullscreen mode

Also, this is actually the case when grid would be better / simpler / easier:

.container {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 50% 50%;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
theodorusclarence profile image
Theodorus Clarence • Edited

Thanks for your addition! I edited the post!
It turns out that using flex-basis won't work with images. But it is fine on other tags

Collapse
 
lyrod profile image
Lyrod

Why not "grid-template-columns: 1fr 1fr"? This will produce the same right?

Collapse
 
theodorusclarence profile image
Theodorus Clarence

Correct, I usually use fraction unit too.

Collapse
 
danielo515 profile image
Daniel Rodríguez Rivero

Will be nice if you explain what:

  grid-column: 1 / 3;
  grid-row: 1 / 3;
Enter fullscreen mode Exit fullscreen mode

means.
One third? Nope. One column and three rows? nope. One ror and three columns? nope.
It is not intuitive to mw

Collapse
 
larsejaas profile image
Lars Ejaas

I think a lot of people struggle with grid untill you realize that you are not positioning items in terms of colum- and row-areas, but instead lines.
Lets say we draw 3 colums. That gives us 4 vertical lines. So if the container needs to fill out the 3 AREAS you need to write:
grid-column: 1/4; This means that you start at line 1 and end at line 4.

Collapse
 
danielo515 profile image
Daniel Rodríguez Rivero

Oh my god! Thank you.
That totally make it click for me.
Thanks

Thread Thread
 
larsejaas profile image
Lars Ejaas

Your' welcome 😊👍

Collapse
 
theodorusclarence profile image
Theodorus Clarence

Check this if you have not understand the grid numbering system.

You can look it up at the link that I referred, after that with the numbering illustration I put up, it should be intuitive enough! Good luck 🚀

Collapse
 
keefdrive profile image
Keerthi

Nice article ...in short Use flex for one dimensionl group of things ie things that need to be placed in a row only, or things that need to be placed column only. ie nav bar = things in a row. With flex you cannot control both row and column.

Use CSS grid for 2 dimensional placement where you want to cotrol the row and column placements. ie the complex layouts of a website.

In flex box you can acheive many things that CSS grid can do, but you have to start nesting containers, which ends up over complicating.

Collapse
 
theodorusclarence profile image
Theodorus Clarence

Great recap!

Collapse
 
kalashin1 profile image
Kinanee Samson

Grid and Flex box actually goes well together, but grid is simpler and removes a lot of unnecessary code

Collapse
 
csorbamatyi profile image
Matyi Csorba

Thank you!
*insert Thank you! meme
;)

Collapse
 
thomasledoux1 profile image
Thomas Ledoux • Edited

Great article!
Note that the gap property can also be used to add spacing between your elements in a flexbox layout: caniuse.com/flexbox-gap.
It is supported by all major browsers by now :-)

Collapse
 
theodorusclarence profile image
Theodorus Clarence

Thanks for your addition. Yep, finally ios & safari is supported, we can start using it now, no more .flex > * + * 😬

Collapse
 
theccode profile image
Eric A. Kumah

With those tiny bits of rows and columns? Wow, Grid says divide and conquer! Adjust in order to fit into any tiny bit of space available; Flexboxes even admit they would employ the services of a grid if necessary! Flexbox has the ability to line things up in one dimension (horizontal) but hey, grids with their rows and columns do the same in 2-dimensions! Rows x Columns.. capable of adjusting based on what's going into the grid.

Collapse
 
ivan_jrmc profile image
Ivan Jeremic

I do everything with grid, it is less code and who cares about IE.

Collapse
 
theodorusclarence profile image
Theodorus Clarence

Nice! I think with grid-auto-flow it can easily replace flexbox now, it is also easier to understand grid than flexbox.

Collapse
 
ivan_jrmc profile image
Ivan Jeremic

Yes grid makes just more sense and you can picture it in your head

Collapse
 
elischei profile image
Eli H. Schei

Great overview!

Collapse
 
natalia_asteria profile image
Natalia Asteria

I love how the text in the "Divide page into parts" codepen is "Hello Bambang".

Collapse
 
kshatriyaprithviraj profile image
Prithvi

But personally, I found mix of Grid + Flexbox more customizable and handy than either of them alone.

Collapse
 
alekseiberezkin profile image
Aleksei Berezkin

Thanks for sharing! What's with grid in older browsers? I'm not about IE, rather about old Androids from like 2016 or 18

Collapse
 
theodorusclarence profile image
Theodorus Clarence

Hey, I don’t really know about old androids, but you can check compatibility on this website. On IE there are some features like the gap property that are not supported yet.

Collapse
 
alexandriastech profile image
alex ➡️ web dev journey 👩🏽‍💻🔍

I love this, especially the case-by-case scenario made it much easier to visualize!

Collapse
 
theodorusclarence profile image
Theodorus Clarence

Thanks! I tried to summarize all the things such as illustrations, code examples that made me understand when I start learning this topic, and write this blog to help others out!

Collapse
 
nirmal5202 profile image
Nirmal Patel

I'm not a css hardcore user, although I would prefer Flexbox, it's really flexible and easy when you get used to it.

Collapse
 
theodorusclarence profile image
Theodorus Clarence

I also love flexbox! Really powerful especially the justify-content property.

Collapse
 
good3n profile image
Tom Gooden✨

Both…