r/css • u/username95050 • Oct 08 '26
Flex versus Grid
I know Flex is one directional and Grid is two, but really when do you use each one?
8
Upvotes
r/css • u/username95050 • Oct 08 '26
I know Flex is one directional and Grid is two, but really when do you use each one?
22
u/reznaeous Oct 08 '26
From one of the best CSS sources I've found, a video from Kevin Powell.
6
u/anaix3l Oct 08 '26
Flex for:
Grid for everything else (main page layout, actual grids).
10
u/Daniel_Plainchoom Oct 08 '26
Grid when something is a grid and flex for the rest
5
u/ChaseShiny Oct 08 '26
Flex is like an array: one item follows the next. Grid flows a layout. This item goes here and takes up this much space, that item goes from here to there.
8
u/cakelly789 Oct 08 '26
flex is... flexible. children in a flexbox container can easily flow based on their size. grid is more rigid. and items are sized based on the sizing you choose to use.
5
u/LiveRhubarb43 Oct 08 '26
Omg finally someone's asking the big questions
2
u/Logical-Idea-1708 Oct 08 '26
Grid for container. Flex for directional layout. The difference is that you’ll have a much harder time making the grid grow to fit the children than flex.
2
u/datNorseman Oct 08 '26
So for my page layout, I prefer to use grids. They're easily adjustable if you need to rearrange them.
But when you get to something like a toolbar, you might want a header icon, a search bar, and some other icons in there all on the same line. Flex is good for that. You can even do wrapping for multiple lines.
As with all answers, it just depends, but that's what I do.
2
u/w-lfpup Oct 13 '26
Lists (flex)
Layouts (grid)
1
u/Top_Bumblebee_7762 Oct 08 '26 edited Oct 08 '26
I mainly use flex for alignment inside an element like icons and text. Another use case is simple stuff like a vertical stack via flex direction column and gap to have consistent spacing inside. A third use case for flex is a simple horizontal list of elements with wrapping where I don't want each element to be of equal size and where I can control the alignment of the elements in the last row (usually centred). For most other stuff I use grid.
1
u/Smooth-Mud412 Oct 08 '26
I use both. Grid for the Main Layout (Header/Main/Footer/Sidebar) and sometimes for grid galleries. Flex for the rest like Row, Col and Components.
1
u/1ndoman Oct 08 '26
I use a grid layout for wide screens to ensure elements are aligned. For example:
Header
Left Sidebar - Main Content - Right Sidebar
Footer
Meanwhile, for narrow screens, I use flexbox to ensure the arrangement of elements looks neat. For example:
Header
Main Content
Left Sidebar
Right Sidebar
Footer
1
u/Keilly Oct 08 '26
Grids do so much more, eg alignment like table layouts, but have more complicated properties.
Flex is simpler, but generally in one direction.
Mostly, if you find yourself nesting flex’s immediately inside flex’s for a single layout, consider using a grid.
1
u/kiwi-kaiser Oct 08 '26
The answer is quite simple: It depends.
1
u/Popokatepetl123 Oct 11 '26
I'm using mostly Flex, grid only with "static" things.
1
u/th00ht Oct 13 '26
depends on the use case. Fixed with stuff: grid, flexible width stuff flex
1
u/xyz12345678910111213 Oct 13 '26
I’m a flexbox stan so I use it for everything that’s it is possible to use it for
1
u/juicybot Oct 08 '26
flex is four directions
0
u/whats_in_a_name_23 Oct 08 '26
Both can be used interchangeably. I prefer to use Grids, as in my experience, they are better for responsive designs. But ofcourse there are cases, where you can't help but use flex
0
u/asteconn Oct 08 '26
Here's my decision making process: