dev3 ·
I use flexbox for everything, including full page layouts, and the nested wrappers are getting out of hand. When is Grid actually the better tool?
0
DEV3 Developer Forum
Every post is reviewed first. Usually reviewed within 24 hours.
I use flexbox for everything, including full page layouts, and the nested wrappers are getting out of hand. When is Grid actually the better tool?
Rule of thumb
Use flexbox for
flex-wrap).margin-left: auto.Use grid for
.page {
display: grid;
grid-template-columns: 240px minmax(0, 1fr);
grid-template-areas:
"header header"
"nav main"
"footer footer";
min-height: 100dvh;
}
.page > header { grid-area: header; }
.page > nav { grid-area: nav; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }
They combine well
A grid for the page, flexbox inside each cell. Subgrid (grid-template-columns: subgrid, supported in all current evergreen browsers) lets nested items align with the parent's tracks, which removes many wrapper hacks.
Test
If you find yourself setting fixed widths on flex children to make columns line up, you want grid. If you find yourself adding empty cells to make a grid behave, you want flex.
Try layouts visually with the CSS grid generator.