TechnicalEasy HTML & CSS

When would you use Flexbox and when would you use CSS Grid?

Asked for Frontend Developer, Full-Stack Developer

Why they ask this: Checks you can choose the right layout tool instead of fighting CSS.

Answer

  • Flexbox is one-dimensional: it lays items out in a row or a column. It is great for navbars, toolbars, centring, and spacing items whose size comes from their content.
  • Grid is two-dimensional: rows and columns together. It is great for page layouts, card galleries and dashboards.
css
/* Flexbox: navbar */
.nav { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }

/* Grid: responsive cards */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 1rem; }

They work well together, for example a Grid page layout with Flexbox inside each card.

Can you explain this in your own words?

Back to question bank