Areas covered by Flexbox which are difficult or impossible to achieve with Grid
Advantage Flexbox
Here are 13 areas where flexbox comes out ahead of Grid (Level 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.Aligning grid items across the entire row/column (like flex items can)
How to center elements on the last row in CSS Grid?
How to offset a grid item, also shifting its siblings?
Can I have a varying number of columns per row in a CSS grid?
Wrapping. Flex items of variable lengths have no problem wrapping. Try getting grid items of variable lengths to wrap. Not possible.
- How to get grid items of different lengths to wrap?
- Can grid items wrap?
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 ofauto
margins.- Can auto margins work in CSS Grid like they do in Flexbox?
Min, Max, Default – all in one. Setting the
min-width
,max-width
and defaultwidth
of a flex item is easy. How can all three lengths be set on a grid column or row? They can't.- Setting the minimum, maximum and default length of a grid column / row
- Set minimum and maximum widths to grid column using percentages (related, but not exactly the same problem)
Sticky footer / header. It's just so much simpler and easier to pin a footer or header with flexbox.
- How can I have a sticky footer with my CSS Grid layout?
Consuming remaining space. A flex item can consume remaining space with
flex-grow
. Grid items have no such function.Aligning grid items across the entire row/column (like flex items can)
Make grid item use remaining space like flex item with flex-grow: 1
How to make the items in the last row consume remaining space in CSS Grid?
How to make CSS Grid last row to take up remaining space
Hiding a left column in CSS Grid
How to get the effect of grid layout's grid-template-columns with a variable number of columns?
CSS fr / fractional units minimum too large
Shrinking. Flex has
flex-shrink
. Grid has... nothing.- Shrink grid items just like flex items in css
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
andminmax()
, it can't be done.Make CSS Grid auto-fill only 2 columns
CSS grid - maximum number of columns without media queries
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.
- Add space before and after first and last grid items
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 ongrid-auto-columns
is one column. In at least some cases, flexbox fixes the problem.- How to make a grid container shrink to fit the content?
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
andauto-fit
functions will not work because the locations of grid areas have been specified. If you want to avoid a media query, flexbox'sflex-wrap
function may be useful.- Two-Column grid should wrap into One-Column grid
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 withflex-direction: column-reverse
.Filling cells starting from the bottom in CSS Grid
https://stackoverflow.com/q/67620185/3597276
The
resize
property on a grid item has no effect on the track. Unless a column or row track is set toauto
(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.- Resize property on grid items results in overlap of other grid items
what is main difference between flexbox and grid in CSS?
The Flexbox works for layout in 1 dimension and the grid in 2.
Is it safe to use Flexbox and Grid freely by now (2020)?
Whether it's "safe" depends on the percentage of browsers your target audience is using. So the problem can be considered subjective.
There is no fatal instability because at the moment it is rich in polyfills and most modern browsers support flexbox and grid layout.
Support tables of flexbox and grid layout:
- https://caniuse.com/#feat=flexbox
- https://caniuse.com/#feat=css-grid
Polyfills:
- https://github.com/FremyCompany/css-grid-polyfill
- https://github.com/jonathantneal/flexibility
Shrink grid items just like flex items in css
New solution
An optimized version of the initial solution using min()
.container {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(auto-fill, minmax(min(200px,100%), 1fr));
}
.child {
display: flex;
align-items: center;
justify-content: center;
padding: 5px;
border: 3px solid #a07;
}
body {
margin:0;
}
<div class="container">
<div class="child">
text
</div>
<div class="child">
text
</div>
<div class="child">
text
</div>
<div class="child">
text
</div>
<div class="child">
text
</div>
</div>
Related Topics
Why Do the :Before and :After Pseudo-Elements Require a 'Content' Property
Does Firefox Support Position: Relative on Table Elements
How Does Flex-Shrink Factor in Padding and Border-Box
What Is Webkit and How Is It Related to Css
Should I Use Max-Device-Width or Max-Width
How to Reverse an Animation on Mouse Out After Hover
How to Center Align Horizontal ≪Ul≫ Menu
Css Z-Index Lost After Webkit Transform Translate3D
What Is the Meaning of "Cascading' in Css
Make Div Stay At Bottom of Page'S Content All the Time Even When There Are Scrollbars
Are Eot, Ttf, and Svg Still Necessary in the Font-Face Declaration
Prevent Flicker on Webkit-Transition of Webkit-Transform
Prevent Body Scrolling But Allow Overlay Scrolling
Make a Div Span Two Rows in a Grid
How to Style Child Components from Parent Component'S CSS File