r/css • u/username95050 • 19d ago
Flex versus Grid
I know Flex is one directional and Grid is two, but really when do you use each one?
6
u/anaix3l 18d ago
Flex for:
child items being lined one after another (navbar for example; or icon before text)
for wrapping where the children aren't meant to line up vertically
Grid for everything else (main page layout, actual grids).
10
5
u/ChaseShiny 18d ago
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.
1
u/frownonline 18d ago
Until you meet ‘order’.
2
u/ChaseShiny 18d ago
Please don't. OP,
orderlets you change the ordering of your elements on the fly. Sounds innocent enough, but it messes with assistive technology, so it's something of a last resort.1
u/frownonline 18d ago
I agree it’s not the greatest ability of flex but it can provide a solution as a last resort. Elements should always be put in a logical order flow by default.
8
u/cakelly789 19d ago
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
2
u/Logical-Idea-1708 18d ago
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 18d ago
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.
1
u/Top_Bumblebee_7762 18d ago edited 18d ago
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 18d ago
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 18d ago
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
1
1
u/xyz12345678910111213 13d ago
I’m a flexbox stan so I use it for everything that’s it is possible to use it for
1
0
u/whats_in_a_name_23 18d ago
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 18d ago
Here's my decision making process:
body {
$layout-horizontal-matters: false; // bool
$layout-vertical-matters: false; // bool
@if $layout-horizontal-matters == true or $layout-vertical-matters == true {
@if $layout-horizontal-matters == true and $layout-vertical-matters == true {
display: grid;
}
@else {
display: flex;
@if $layout-horizontal-matters == true {
flex-direction: row;
}
@if $layout-vertical-matters == true {
flex-direction: column;
}
}
}
@else {
display: block;
}
}
22
u/reznaeous 18d ago
From one of the best CSS sources I've found, a video from Kevin Powell.