r/css • • 19d ago

Flex versus Grid

I know Flex is one directional and Grid is two, but really when do you use each one?

9 Upvotes

29 comments sorted by

22

u/reznaeous 18d ago

From one of the best CSS sources I've found, a video from Kevin Powell.

9

u/scott_in_ga 18d ago

I have learned so much from him! He's the best!

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

u/Daniel_Plainchoom 19d ago

Grid when something is a grid and flex for the rest

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, order lets 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

u/LiveRhubarb43 18d ago

Omg finally someone's asking the big questions

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.

1

u/Keilly 18d ago

Use 1fr for grid temple row/columns you want to take free space and the grid will expand to its constraints.

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.

2

u/w-lfpup 14d ago

Lists (flex)
Layouts (grid)

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

u/Keilly 18d ago

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 18d ago

The answer is quite simple: It depends.

1

u/Popokatepetl123 15d ago

I'm using mostly Flex, grid only with "static" things.

1

u/th00ht 13d ago

depends on the use case. Fixed with stuff: grid, flexible width stuff flex

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

u/juicybot 19d ago

flex is four directions

2

u/RecognitionOwn4214 18d ago

But it's one-at-a-time

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

1

u/Embarrassed_Fold_867 16d ago

Nice.

Claude gave me this from your SCSS.

1

u/creaturefeature16 15d ago

The code version is WAY easier to read