Back
Interactive Explainer

CSS Layout (Flexbox & Grid)

Flexbox for rows or columns; Grid for 2D layouts. Use both together.

🎯Key Takeaways
Flexbox: one axis (row or column).
Grid: rows + columns.
Combine both; media queries for breakpoints.

CSS Layout (Flexbox & Grid)

Flexbox for rows or columns; Grid for 2D layouts. Use both together.

~2 min read
Be the first to complete!
What you'll learn
  • Flexbox: one axis (row or column).
  • Grid: rows + columns.
  • Combine both; media queries for breakpoints.

Lesson outline

The big picture

Layout used to mean floats and hacks. Today you have Flexbox (one direction: row or column) and Grid (rows and columns). Use both: Grid for the page, Flexbox for the pieces inside.

Flexbox answers “how do I line these up or space them out?” Grid answers “where does this block sit in a 2D grid?”

Layout in two tools

Flexbox = one direction (row or column). Grid = rows + columns. Use both: Grid for the page, Flexbox inside.

Flexbox basics

display: flex on a container lays its children in a row (default) or column. justify-content aligns along the main axis (e.g. space-between); align-items on the cross axis (e.g. center).

flex-grow, flex-shrink, and flex-basis control how items share space. Great for nav bars, card rows, and centering. It does not handle “row and column at once” the way Grid does.

Flexbox

One axis: row or column. justify-content, align-items.

Grid

Two axes: rows + columns. grid-template-*, fr, minmax().

CSS Grid basics

display: grid with grid-template-columns and grid-template-rows defines the grid. Use fr (fraction of space) and minmax() for flexible, responsive tracks. Children go into cells; grid-column and grid-row place them.

Grid is ideal for page layouts, dashboards, and any design with a clear row/column structure. Auto-placement flows items if you do not specify; use gap for spacing.

Responsive layout

Use Grid for the overall page structure and Flexbox for components inside a cell. Media queries (e.g. @media (min-width: 768px)) change the template or direction at breakpoints so layout adapts to screen size.

Keep breakpoints consistent (e.g. 640, 768, 1024 px). [Learn more on MDN – CSS layout](https://developer.mozilla.org/en-US/docs/Learn_web_development/Getting_started) (Flexbox, Grid, RWD).

Key takeaways

  • Flexbox: one axis (row or column).
  • Grid: rows + columns.
  • Combine both; media queries for breakpoints.

Related concepts

Explore topics that connect to this one.

Suggested next

Often learned after this topic.

JavaScript basics

Ready to see how this works in the cloud?

Switch to Career Paths for structured paths (e.g. Developer, DevOps) and provider-specific lessons.

View role-based paths

Sign in to track your progress and mark lessons complete.

Discussion

Questions? Discuss in the community or start a thread below.

Join Discord

In-app Q&A

Sign in to start or join a thread.