10

It is wildly highlighted that Flexbox is for 1-D and Grid for 2-D but I have not found a clear explanation why Grid could not be used for 1-D and replace Flexbox. The closest I came to is

But you could also argue that purely 1D layout like this is more powerful in Flexbox, because Flexbox allows us to move those elements around easier (e.g. move them all to one side or another, change their order, space them out evenly, etc).

I use Grid and Flexbox for basic layout: a general placement of the boxes on the page and some dynamic ones, usually stacked. The esthetical ones (toasters, modals, ...) are managed through a framework. I have not yet found a case where Grid could not replace Flexbox out of the box (that is without advanced CSS gymnastics or a lot of code).

To take the example of the quote above, all the "moves" mentioned are directly available in Grid, usually with the same semantics as in Flexbox.

What are the fundamental areas covered by Flexbox which are difficult or impossible to manage with Grid?

EDIT: the browser support is not important (I only use evergreen browsers and can switch if needed)

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
WoJ
  • 27,165
  • 48
  • 180
  • 345
  • The simplest reason is grids really require you to know dimensions ahead of time whereas flexbox does not. – TylerH Jan 21 '20 at 19:01

3 Answers3

27

Advantage Flexbox

Here are 14 areas where flexbox comes out ahead of Grid (Level 1):

  1. Centering wrapped items. Imagine five elements. Only four per row. The fifth one wraps. In a flex container, that fifth one can be easily aligned across the entire row with justify-content. Try centering this fifth item in a grid container. Not a simple matter.


  1. Wrapping. Flex items of variable lengths have no problem wrapping. Try getting grid items of variable lengths to wrap. Not possible.


  1. Auto margins. Flex items can be placed, packed and spaced away throughout their container with auto margins. Grid items, however, are confined to their tracks, greatly diminishing the utility of auto margins.


  1. Min, Max, Default – all in one. Setting the min-width, max-width and default width of a flex item is easy. How can all three lengths be set on a grid column or row? They can't.


  1. Sticky footer / header. It's just so much simpler and easier to pin a footer or header with flexbox.


  1. Consuming remaining space. A flex item can consume remaining space with flex-grow. Grid items have no such function.


  1. Shrinking. Flex has flex-shrink. Grid has... nothing.


  1. Limiting the column count in a dynamic layout. With flexbox, creating a wrapping two-column grid that remains fixed at two-columns across screen sizes is no problem. In Grid, despite having all these great functions, such repeat(), auto-fill and minmax(), it can't be done.


  1. Creating space between first and last items. In a grid container with a variable number of columns, it's not easy to add an empty first and last column. Margins, padding, columns and pseudo elements each have their limitations. It's simple and easy with flexbox.


  1. An important benefit of the inline-level container is lost in some cases. If you have a Grid layout with a dynamic number of columns – meaning you cannot set the number of columns or a width for the container – then display: inline-grid doesn't work. All items stack in a single column. This is because the default setting on grid-auto-columns is one column. In at least some cases, flexbox fixes the problem.


  1. Getting columns with author-defined grid areas to wrap without media queries. Let's say you have a two-column grid containing grid areas that have set locations, and want the grid to automatically transition to a single column (with the second column wrapping below the first) on smaller screens. With grid, you would need a media query. The auto-fill and auto-fit functions will not work because the locations of grid areas have been specified. If you want to avoid a media query, flexbox's flex-wrap function may be useful.


  1. There is no column-reverse function in CSS Grid. Getting items to populate a container starting from the bottom isn't possible with a single rule applied to the grid container. With flexbox, however, the task is simple with flex-direction: column-reverse.


  1. The resize property on a grid item has no effect on the track. Unless a column or row track is set to auto (content-based sizing), resizing a grid item will overflow the track. Since flexbox doesn't have column and row tracks, it may be a useful alternative.


  1. Maintaining item heights in dynamic layouts. Let's say that item 3, which is in row 3, must maintain its height whether in row 3 or row 2 (if item 2 is missing; so 3 shifts up). Grid layout can't do this, because row heights are set at the container level and can't be adjusted at the item level. Flexbox can do this because there are no rows or columns, and item heights can be manipulated at the item level.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • theres a few in this list that are iffy like the "min, max, default -- all in one". you can simply apply the same styles to the grid container – oldboy Jun 12 '19 at 03:41
  • @BugWhisperer, not sure how your suggestion provides a solution to that particular problem. If you have a solution, please go to the [original question](https://stackoverflow.com/q/47612711/3597276) and post an answer. Thanks. – Michael Benjamin Jun 17 '19 at 17:41
6

Flexbox and CSS grid are two different features and I don't agree about saying that one can replace another or that CSS grid is a superset of flexbox.

You said:

I have not found a clear explanation why Grid could not be used for 1-D and replace Flexbox.

Here is a basic flexbox example that involve wrapping that you cannot achieve (or probably difficult to achieve) using CSS grid. Reduce the window size and see how the elements will behave.

.box {
  display: flex;
  flex-wrap: wrap;
}

.box>span {
  border: 1px solid;
  padding: 10px;
  flex-grow: 1;
}
<div class="box">
  <span>some text very long here some text very long here </span>
  <span>text here</span>
  <span>A</span>
  <span>B</span>
</div>

This is a 1-D layout where each line may have elements resized differently depending on the free space without being inside a 2-D grid. It will be difficult to achieve the same output using CSS-grid.

Basically flexbox is more suited when it comes to multiline/multirow content following one direction whereas CSS grid is more about a Grid with row and columns. Of course, when it comes to only one line a 2D grid can be considerd as 1D thus flexbox and CSS grid may achieve the same thing.

It's like comparing a table with only one tr and multiple td with a set of inline-block element inside one line BUT when it comes to wrapping and multiple tr it's clear that table and inline-block are different.


Worth to note that you can achieve what you want using any techniques in general. In the past, Flexbox wasn't there and developers were able to build layout using float (Boostrap is the best example). Then flexbox come with more powerful features to make things easier. Same thing for CSS grid and for future features.

Here is an example : https://drafts.csswg.org/css-align-3/

This Draft is talking about a future enhancement of alignment where we can consider jutify-content, align-items, etc without even using flexbox or CSS grid but directly on block elements.

If implemented, will this make Flexbox and CSS Grid useless? I don't think so.

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
2

I should probably point out that CSS Grid is MUCH newer than CSS Flexbox and therefore may actually be intended as a replacement to some extent. Much like Flexbox replaced the typical use case for float (not the intended use).

I'm going to preface this by saying, I rarely NEED the complexities/capabilities of Grid so I still use Flexbox for most things.

However, in my experience, the flex-wrap capabilities, especially with variable screen sizes, is one place that I feel Grid isn't a great replacement.


As a reminder: Flexbox vs Grid IS NOT an all-or-nothing question. You can, and sometimes should, use grids inside flexboxes or even add tables to grids or floats to tables. The driving factor should be to use the right tool for the job NOT to simply discard everything else in favor of the newest option.

Bryce Howitson
  • 7,339
  • 18
  • 40