Flexbox vs Grid: 5 layouts, built both ways

← Back to the article

Each layout below uses the same HTML. Switch between Flex and Grid to see how each one handles it, then resize the window. The CSS for the active mode is shown under each demo.

2. Card grid

Verdict: Grid (with subgrid)
  • Starter

    One site, basic stats.

    Choose
  • Studio plan for small teams

    Five sites, shared editing, custom domains and form submissions sent to your inbox.

    Choose
  • Agency

    Unlimited sites and client handover.

    Choose
  • Enterprise

    Single sign-on, audit logs and a named contact.

    Contact us
  • Education

    Free for classrooms.

    Apply
.cards { display: flex; flex-wrap: wrap; gap: 16px; }
.card { flex: 1 1 220px; display: flex; flex-direction: column; }
.card a { margin-top: auto; align-self: flex-start; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.card { display: grid; grid-row: span 3; grid-template-rows: subgrid; row-gap: 0; }
.card a { align-self: end; justify-self: start; }

3. Holy-grail page

Verdict: Grid
Header
Main content
Footer
.page { display: flex; flex-direction: column; gap: 12px; }
.middle { display: flex; flex-direction: column; gap: 12px; }  /* extra wrapper needed */
@media (min-width: 700px) {
  .middle { flex-direction: row; }
  nav, aside { flex: 0 0 180px; }  /* only in row mode: flex-basis follows the main axis */
  main { flex: 1; }
}
.page {
  display: grid; gap: 12px; grid-template-columns: 1fr;
  grid-template-areas: "header" "nav" "main" "aside" "footer";
}
.middle { display: contents; }  /* wrapper not needed; ignored */
header { grid-area: header; } nav { grid-area: nav; } main { grid-area: main; }
aside { grid-area: aside; } footer { grid-area: footer; }
@media (min-width: 700px) {
  .page {
    grid-template-columns: 180px 1fr 180px;
    grid-template-areas: "header header header" "nav main aside" "footer footer footer";
  }
}

4. Form

Verdict: Grid for labeled fields
.row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; }
label { flex: 0 0 120px; }   /* a fixed guess at the longest label */
input { flex: 1 1 200px; }
button { margin-left: 132px; }
.form { display: grid; grid-template-columns: max-content minmax(0, 1fr); align-items: center; gap: 10px 12px; }
.row { display: contents; }
button { grid-column: 2; justify-self: start; }