Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

React
  • One Request, Two Answers: the Same Post as an HTML Page and as JSON
  • A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx
  • How a Web Page Works: HTML Tags, Elements and Attributes
  • Semantic HTML: header, nav, main, aside and footer as Page Landmarks
  • HTML Forms: Labels, Names, Input Types and Where the Data Goes (GET vs POST)
  • CSS Basics: Selectors, Which Rule Wins, and the Box Model
  • CSS Flexbox vs Grid: A Nav Bar, a Card Row and a Dashboard
  • Responsive CSS: Viewport, Mobile-First Media Queries, clamp() and Dark Mode with Tokens
  • JavaScript for People Who Already Code: Syntax, Modules and Five Surprises
  • JavaScript Arrays the Functional Way: map, filter, reduce, find, sort and Object.entries
  • Async JavaScript: Promises, async/await, fetch and Promise.all
  • JavaScript in the Page: the DOM, querySelector and Events
  • npm, package.json and Vite: What npm install Fetches and How Vite Serves and Builds Your Page
  • TypeScript Essentials: Types Checked, Then Erased (Interfaces, Unions, Generics)
  • Why React, JSX and Components: The Page as a Function of Data
  • React Props and Composition: Data Down, Calls Up, Children in the Slot
  • Routing With React Router: One Page, Many Addresses
  • React Component Tests With Vitest and Testing Library: Test What the User Sees
  • End-to-End Tests for a React App With Playwright: A Full Dress Rehearsal
  • Why AI-Built Websites All Look the Same, and Four Levels That Fix It
  • Visual Hierarchy, Spacing and the 8-Point Grid: Tell the Eye What to Read First
  • Colour, Contrast and Dark Palettes: 60-30-10, Semantic Tokens and Ratios You Can Compute
  • Typography That Sells: Type Scale, Line Length and Free Font Pairings
  • The UI Element Catalogue: Buttons to Toasts, Their Jobs, States and shadcn/ui Names
  • Design Tokens: Primitive and Semantic Tokens, from Figma to CSS to Tailwind
  • Tailwind CSS 4 in React: Utility Classes, Responsive Prefixes, Hover and Dark Mode
  • shadcn/ui in React: Components You Own, Edit and Commit
  • Design Tokens to a Tailwind Theme: Light and Dark Mode with @theme inline
  • Motion in React: Entrance, Hover, Exit and Layout Animations
  • A React Dashboard: Stat Cards, a Recharts Line Chart and a Sortable Table
  • Accessibility in Practice: Dialogs, Menus and the Keyboard in React
  • Reference-Driven Design: Extract a DESIGN.md From a Site You Admire and Build From It
  • Components, Variants and Variables: One Design System in Figma and in CSS
  • Figma MCP Server: Build a React Component From a Frame, Then Write It Back
  • Claude Design: From a Brief to a Clickable Prototype, Then Hand It Off to Claude Code
  • Critiquing AI-Generated UI: Six Checks for Every Screen
  • The AI Design Landscape: Prompt to UI, Prompt to App, and Design to Code
  • Your Brand Rules Folder: One Folder Every AI-Built Page Follows
  • The Best-Practice UI Prompt: Plain Sentences That Beat the Average AI Page
  • UI Ideas From Figma Make, Google Stitch and ChatGPT: One Prompt, Three Designs
  • Google Stitch From a Sketch or Screenshot: Show the Layout, Say What to Keep
  • Stitch to Figma to Code: Turn a Stitch Screen Into Code That Uses Your Tokens
  • Figma Make: Prompt to Prototype With a Four-Part Prompt
  • Upgrade a Dated UI From One Screenshot (or a Reference Image)
  • States and Feedback: Make Every Click Get an Answer
  • A Design-Loop Skill: Claude Screenshots, Scores and Fixes Its Own UI
  • Copy Without the AI Tells: A Copy Pass on a Landing Page
  • React State, Events, Lists and Conditional Rendering: Follow One Click
  • React useEffect: When It Runs, What Cleanup Is For, and When You Don't Need One
  • Lifting State Up, Refs and React DevTools: Put State Where It Is Needed
  • Forms and Actions in React 19: Controlled Inputs, Form Actions, useActionState and useFormStatus
  • React Context and Custom Hooks: Share One Value, Reuse the Logic
  • Server State With TanStack Query: One Key, One Request, One Cache
  • Mutations and Optimistic Updates in TanStack Query: Change Data, Then Tell the Cache
  • Generate TypeScript Types, Query Hooks and Zod From an OpenAPI Spec With Orval
  • A Mock Backend in the Browser With Mock Service Worker, Generated From the Spec
  • A React Client for a FastAPI API: The CORS Wall, CORSMiddleware and the Vite Proxy
  • Forms With React Hook Form and Zod: The API's Rules in the Browser, the Server's Errors Under the Field
  • JWT Auth in a React Client With a FastAPI Backend: Log In, Carry the Token, Let the Server Decide
  1. Blog
  2. /
  3. React
  4. /
  5. Fundamentals

CSS Flexbox vs Grid: A Nav Bar, a Card Row and a Dashboard

Flexbox lines things up in one direction, Grid lays out rows and columns at once. Build a nav bar, a wrapping card row and a five-part dashboard with named grid areas, and see what breaks without flex-wrap.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Fundamentals

What you will learn

  • Explain normal flow: block boxes and inline boxes
  • Use display flex with justify-content, align-items and gap on the main and cross axes
  • Make a card row wrap with flex-wrap and flex: 1 1 160px
  • Lay out a page with grid-template-columns, rows and named areas, and pick flex or grid

Before you start

  • CSS selectors and the box model

We want a top bar with the brand on the left and three links on the right, all in one row. Here's the HTML:

  <header class="bar">
    <strong class="brand">Team Tasks</strong>
    <!-- the second item is a flex container too: three links -->
    <nav class="links">
      <a href="#">Today</a>
      <a href="#">This week</a>
      <a href="#">Done</a>
    </nav>
  </header>

With no layout CSS, the brand sits on one line and the links drop under it. Why?

What the browser does on its own

header and nav are block boxes. A block box always starts on a new line and takes the full width. a and strong are inline boxes: they stay in the line, side by side, like words in a sentence. This default is called normal flow, and Flexbox and Grid are the two tools CSS gives you to change it.

Two everyday pictures help. Flexbox is books on a shelf: one line of things that you push left, centre or spread out. Grid is a car park with marked bays: the rows and columns are painted first, and each car parks in the bay with its name. One direction means Flexbox; rows and columns at once means Grid.

The main axis and the cross axis

A flex container with two flex items, brand and links. The main axis runs left to right and is controlled by justify-content. The cross axis runs top to bottom and is controlled by align-items. flex container (.bar) brand links main axis: justify-content cross axis: align-items
The two children of a flex container are flex items. Add flex-direction: column and the two axes swap.

The flex container is the shelf, and its children are the flex items. The main axis runs along the shelf, left to right, and justify-content moves items along it. The cross axis goes across it, up and down, and align-items moves items on that one.

display: flex on the container

/* flex goes on the container, never on the items */
.bar {
  display: flex;
  justify-content: space-between; /* main axis: ends apart */
  align-items: center;            /* cross axis: centred */
  height: 64px;
  padding: 0 24px;
  background: #1E3A8A;
  color: white;
}
.brand {
  font-size: 22px;
}
/* a flex container inside a flex item */
.links {
  display: flex;
  gap: 16px;                      /* space between items only */
}
.links a {
  color: #BFDBFE;
}

display: flex goes on .bar, the container, and now the brand and the links share one row. justify-content: space-between pushes them to the two ends, and align-items: center centres them up and down in the 64px bar.

The .links box is a flex container too, sitting inside a flex item. It lines up its three links, and gap: 16px puts space between them (only between, not at the ends).

Try the break the file suggests: move display: flex; from .bar to .brand. The brand and the links stack again, because flex only arranges the children of the box it's on.

Cards that grow and wrap

The second example is four cards in a section class="cards":

.cards {
  display: flex;
  flex-wrap: wrap;   /* no room left? the next card starts a new line */
  gap: 16px;
}
/* each card: may grow, may shrink, starts at 160px */
.card {
  flex: 1 1 160px;
  border: 1px solid #CBD5E1;
  border-radius: 8px;
  padding: 0 14px;
}

display: flex lines the four cards up in a row. flex-wrap: wrap says that when there's no room left, the next card starts a new line. And flex: 1 1 160px is three values in one: each card may grow, may shrink, and starts at 160 pixels, then grows to share the spare room.

At 900 pixels wide all four cards fit in one row. Shrink the window to 480 pixels and you get two lines of two cards, with no extra code. Now delete flex-wrap: wrap: all four cards squeeze into one line, only 96 pixels each. With wrap the cards move to a new line; without it they squeeze.

Grid: paint the bays, then park in them

The third example is a small orders dashboard. The HTML is five children of body: header, nav, main, aside and footer. The whole layout lives in CSS:

/* the whole page is one grid: 3 columns x 3 rows */
body {
  margin: 0;
  padding: 12px;
  font-family: system-ui, sans-serif;
  display: grid;
  grid-template-columns: 160px 1fr 200px;
  grid-template-rows: auto 1fr auto;
  min-height: 320px;
  gap: 12px;
  /* one string per row, one name per column */
  grid-template-areas:
    "top  top  top"
    "side main stats"
    "foot foot foot";
}
/* each part says which area it fills */
header { grid-area: top; }
nav    { grid-area: side; }
main   { grid-area: main; }
aside  { grid-area: stats; }
footer { grid-area: foot; }
header, nav, main, aside, footer {
  padding: 8px 12px;
  border-radius: 8px;
  background: #E2E8F0;
}
nav a { display: block; }

First the columns are painted: 160px, then 1fr (the rest of the space), then 200px. Then the rows: auto 1fr auto, so the top and bottom rows are only as tall as their content.

Then the bays get names in grid-template-areas. The template is a map: one string per row, one name per column. top is written three times, so the header spans all three columns. Finally each part picks its bay with grid-area: header goes to top, nav to side, aside to stats. No row or column numbers anywhere.

The grid map: top row spans three columns for the header; middle row is side 160px, main 1fr, stats 200px; bottom row spans three columns for the footer. top: header (spans 3 columns) side: nav160px main: main1fr stats: aside200px foot: footer (spans 3 columns)
"top top top" / "side main stats" / "foot foot foot": the template string is a drawing of the page.

The file's break is a good one: write "side main stat" (no s). No area is called stats any more, so the aside leaves its place.

Flexbox or Grid?

Ask one question: do the items line up in one direction, or in rows and columns at once?

UseWhenExamples here
display: flexone direction, a shelf of itemsthe nav bar, the card row
display: gridrows and columns at once, marked baysthe dashboard, a whole page layout

You'll often use both: a grid for the page, and a flex row inside one of its bays.

Common mistakes

  • Flex on the wrong box. display: flex goes on the parent of the things you want to arrange.
  • No flex-wrap. Items squeeze instead of moving to a new line on narrow screens.
  • A typo in an area name. The element silently leaves its bay. Area names in grid-area must match the template exactly.
  • Using margins between items. gap is simpler and only adds space between items.

Try it yourself

Make a top bar with flex, your name on the left and two links on the right, then a row of three course cards that wraps on a narrow window. Resize the window to check. To make the same page adapt to phones and add a dark mode, continue with responsive design, themes and dark mode.

Tried the exercise? Share your answer or ask a question.

The StudyEasy AI Community is free to join, with the lesson files, notebooks and code.

Join free on Skool

Keep going in React · 7 of 59

Previous
CSS Basics: Selectors, Which Rule Wins, and the Box Model
Next post
Responsive CSS: Viewport, Mobile-First Media Queries, clamp() and Dark Mode with Tokens

Related course on Udemy

The Python Architect Masterclass: Full Stack to AI Engineer

Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)

Related posts

One Request, Two Answers: the Same Post as an HTML Page and as JSON

Ask a Django blog for the same post twice: once as a 12,731-byte HTML page built by a template, once as 2,976 bytes of JSON built by a serializer. Compare the headers, see who builds the page, and why the server still owns every write.

A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx

Make a blog design from a prompt in Figma Make, download src.zip, unzip it with Python's zipfile module, and read the two files that matter: App.tsx with its selectedPost state and BlogCard.tsx with its props.

How a Web Page Works: HTML Tags, Elements and Attributes

A web page is a text file full of labelled pieces. Learn tags, attributes and nesting with three tiny pages, and see what breaks when an attribute or a closing tag is wrong.

Semantic HTML: header, nav, main, aside and footer as Page Landmarks

The same page written as div soup and with semantic tags looks identical, but only one gives screen readers and search engines landmarks and an outline. Plus tables, div, span and the one-main rule.

On this page

  • What the browser does on its own
  • The main axis and the cross axis
  • display: flex on the container
  • Cards that grow and wrap
  • Grid: paint the bays, then park in them
  • Flexbox or Grid?
  • Common mistakes
  • Try it yourself
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

The Python Architect Masterclass: Full Stack to AI Engineer

Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.