HTML & CSS Course
HTML & CSS
/
Intermediate

Grid vs Flexbox

Definition

Understanding when to use CSS Grid (2-Dimensional) versus Flexbox (1-Dimensional).

Explain Like I'm New

Use Flexbox when you have a row of items (like a navbar) and you just want to space them out evenly without caring about exact widths. Use Grid when you want to explicitly define the entire layout of the page (Sidebar here, Header there) and lock elements into a rigid structure.

Real World Example

They are meant to be used together! You use CSS Grid on the `<body>` to set up the Header, Sidebar, and Main content areas. Inside the Header, you use Flexbox to align the Logo and the Profile Avatar.

Common Use Cases

  • •Architecting web applications

Interactive Example

Interview Questions

basic

  • Which one is 1-Dimensional and which is 2-Dimensional?

intermediate

  • If I want a row of tags (`#html`, `#css`) to automatically wrap to the next line when they run out of space, which should I use?

Flash Cards

Question

1D vs 2D?

Click to reveal answer
Answer

Flexbox is 1D (operates in a single row or column). Grid is 2D (operates in rows AND columns simultaneously).

Question

Wrapping tags?

Click to reveal answer
Answer

Flexbox (`flex-wrap: wrap`). You don't care about a rigid column structure here, you just want them to flow naturally like text. Flexbox is perfect for this.