Build Your Website from Scratch (6): Flexbox and Grid — A Responsive Product Grid

Understand the two Flexbox axes and its distribution properties, build a balanced navbar, and a responsive product grid with one line of Grid and no media queries.

Build Your Website from Scratch (6): Flexbox and Grid — A Responsive Product Grid
Understand the two Flexbox axes and its distribution properties, build a balanced navbar, and a responsive product grid with one line of Grid and no media queries.

Build Your Website from Scratch (6): Flexbox and Grid — A Responsive Product Grid

Share:
⏱ 8 min read🔄 Last updated: 2026-12-05✍ NOOR 4 TECH Team

Flexbox arranges items along one axis (a row or a column); Grid arranges them in rows and columns together.
The short version: display: flex for bars and rows, and display: grid with repeat(auto-fit, minmax(220px, 1fr)) for a product grid that adapts to any screen.
In this part you will turn your stacked store cards into a clean three-column grid on wide screens and a single column on phones, without writing a single media query.

In part 5 we styled the cards and the form with colors and fonts, but one problem remained: every card stretches across the full page width and stacks on top of the next, so the page looks long and empty on wide screens, and the navbar links sit without balance. Today we fix both with the two layout tools every modern site is built on: Flexbox and Grid.

1. The problem: stacked cards and a half-empty screen

Elements like article are block-level: each one takes a full line no matter how small its content is. On a 1200px laptop screen the result is a card whose text spans barely 400px with dead space beside it, then the next card below it, and so on. Before Flexbox and Grid, developers solved this with fragile float and table tricks that broke at the first edit. Today the fix is a single property.

2. Flexbox and Grid: the two modern layout tools

The rule of thumb:

  • Flexbox for layout in one dimension: a row of links, a row of buttons, a column of cards, distributing navbar items.
  • Grid for layout in two dimensions: a product grid of rows and columns, or a full page layout with header, sidebar, content and footer.

They are not rivals but complements: in this part we use Flexbox for the navbar and Grid for the product grid, which is exactly how real sites combine them. That is why "flexbox vs grid" is such a persistent search question: the answer is not one or the other, but each in its place.

3. Flexbox: the core concept

When you write display: flex on a container, its direct children become flex items lined up along a main axis (horizontal by default) and a perpendicular cross axis. Two properties then cover most of what you need:

  • justify-content: distributes items along the main axis (pack at the start, center, or space them out evenly).
  • align-items: aligns items along the cross axis (vertical centering, or stretch to fill the height).
Diagram of the Flexbox axes: a container holding three items in a row, with an arrow showing the horizontal main axis and another showing the vertical cross axis
Figure 1: the two Flexbox axes: the main axis along the row, the cross axis perpendicular to it
💡 A bonus for Arabic sites: Flexbox respects the page direction automatically: in a dir="rtl" page the main axis starts from the right and items flow right to left with no extra code. Logical properties such as margin-inline-start adapt the same way.

4. Flexbox in practice: a balanced navbar

We want a bar with the store name on one side and the nav links on the other, all vertically centered. Three properties do it:

header {
  display: flex;
  justify-content: space-between; /* name on one side, links on the other */
  align-items: center;            /* vertical centering */
  padding: 16px 24px;
  background: #0f1d33;
  border-bottom: 3px solid #f5b43c;
}

nav {
  display: flex;
  gap: 20px;   /* fixed spacing between links, no margin hacks */
}

nav a {
  color: #eef2f7;
  text-decoration: none;
}

The gap property here is a small discovery that changes your life: fixed spacing between items with no margin fiddling, and it works in both Flexbox and Grid. The result:

A navbar styled with Flexbox: the store name at the far right and three links at the far left, all vertically centered on a dark background with a gold bottom border
Figure 2: the navbar after Flexbox: name on one side, links on the other, perfectly balanced

5. Grid: the core concept

When you write display: grid on a container, you describe it as a flexible table: you define the columns and rows, and the browser places the items for you. The three essentials:

.products-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* three equal columns */
  gap: 20px;                             /* spacing between cells */
}

The fr unit means "a fraction of the available space": 1fr 1fr 1fr splits the width in thirds, while 2fr 1fr gives the first column twice the second. The repeat(3, 1fr) function is shorthand for repeating a column three times.

6. Build a responsive product grid with one line

Now the most important step in the whole article. Instead of always forcing three columns (cramped on phones) or always one column (wasteful on wide screens), we tell the browser: "fill the space with columns no narrower than 220px, and grow them to fill the row":

  1. Open index.html and wrap the six product cards in a container: <div class="products-grid"> before the first card and </div> after the last one.
  2. In style.css add the grid rule:
    .products-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 20px; }
  3. Remove margin-bottom from .product-card; gap handles the spacing now.
  4. Save and reload: three columns on a wide screen. Shrink the browser window gradually and watch the columns collapse to two, then one, all by themselves.

How does the line work? auto-fit means "create as many columns as fit in the available width", and minmax(220px, 1fr) means "each column is at least 220px and grows to fill its share of the row". On a 900px screen three columns fit, on 500px two fit, on a phone one fits, and none of it needs an @media rule.

💡 Why 220px? It is the smallest width at which the card stays readable: its title, price and button do not crowd each other. Tune the value to your content; the rule is to set it from the smallest width that respects your card, not from specific device numbers.

7. See the result yourself

The same six cards, before and after the grid:

Comparison: on one side the product cards stacked in one long full-width column, on the other the same cards arranged in a tidy three-column grid
Figure 3: the product cards before and after Grid, same content with a completely different layout

And on a 390px phone screen, the grid collapses to one comfortable column automatically, with no extra code:

The product grid at a 390px mobile width: the cards in one tidy full-width column
Figure 4: the same grid on mobile: one column automatically, thanks to auto-fit and minmax

8. When to use Flexbox and when to use Grid

The practical decision table:

  • Navbar, button row, a card with an image beside text: Flexbox.
  • Product grid, image gallery, a page layout with several regions: Grid.
  • Items in one direction with flexible space distribution: Flexbox.
  • Rows and columns that must stay aligned with each other: Grid.

A practical signal: if you find yourself tuning each item's width so it lines up with the one above it, you are fighting Flexbox in Grid's battle. And if you only need a row of freely distributed items, Grid is heavier than you need.

9. Finer control with media queries

The auto-fit grid covers most cases, but sometimes you want an exact column count per screen size. This is where part 5's @media meets Grid:

.products-grid {
  display: grid;
  gap: 20px;
  grid-template-columns: 1fr;            /* mobile first: one column */
}

@media (min-width: 600px) {
  .products-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (min-width: 900px) {
  .products-grid { grid-template-columns: repeat(3, 1fr); }
}

Same Mobile First approach from part 5: the base styles serve phones, and each @media (min-width: ...) adds an enhancement for larger screens. Use whichever style serves your content: auto-fit for full flexibility, explicit columns for precise control.

10. Inspect the layout in DevTools

Open DevTools (F12) → Elements and select the grid container. Next to it in the element tree you will see a grid badge (or flex): click it and the browser draws the grid lines and column numbers right on the page, so you see the tracks and gaps with your own eyes. This is how professionals debug layout: do not guess why a column shifted, show the grid lines and look. And remember the final check with the W3C CSS Validator after adding today's rules.

11. Common Flexbox and Grid mistakes

  1. Putting display: flex or display: grid on the wrong element: the property goes on the container and affects its direct children only, not deeper descendants.
  2. Forgetting that justify-content follows the main axis: with flex-direction: column it distributes vertically, not horizontally.
  3. Using repeat(3, 1fr) and then wondering why cards are crushed on mobile: the fix is auto-fit with minmax, or media queries.
  4. Keeping manual margins between items after adopting gap, which doubles the spacing.
  5. Trying to align Flexbox items into multiple rows with fixed widths: that is the definition of Grid, so use it.
⚠️ Warning: if the page does not change at all after writing display: grid, the most common cause is that the element you put the class on is not the direct parent of the cards. Inspect the HTML structure in DevTools and confirm the cards are direct children of the grid container.

12. Summary and what's next

Today your site moved from "stacked boxes" to a professional layout: you understood the two Flexbox axes and its distribution properties, built a balanced navbar, turned the product cards into a responsive grid with a single line of Grid, and learned when to pick each tool. Your site now looks complete on every screen. In part 7 we leave your computer for the world: we publish the site to real hosting and connect it to a domain, so any visitor on the internet can reach it.

FAQ

Do I need to learn both Flexbox and Grid, or is one enough?

Learn both, because they solve different problems: Flexbox for one dimension and Grid for two. A single project uses them in different places, exactly as we did in this part.

Does anyone still use floats for layout?

Not for layout. The float property survives only for its original purpose: wrapping text around an image. Every modern page layout is built with Flexbox and Grid, and all current browsers support them fully.

How many columns should my product grid have?

Do not think in column counts; think in the smallest readable card width, then let minmax decide the count on each screen. A card with a title, two lines and a button usually stays comfortable between 220 and 280 pixels minimum.

Why does the last row's card stretch wider than its siblings?

Because auto-fit stretches existing columns to fill the row. The common fix is switching to auto-fill with the same minmax, so the browser reserves space for the empty columns and all cards keep an equal width.

🔗 Official sources

Target keywords and how we chose them: Arabic: "شرح flexbox", "flexbox css شرح" and "css grid شرح" as suggested by Google autocomplete (volume unmeasured), plus the series staples "تعلم css" and "تنسيق صفحات html". English: "flexbox vs grid" and "flexbox vs grid when to use", among the richest autocomplete suggestions in this topic, with "CSS flexbox tutorial", "CSS grid tutorial" and "responsive grid layout" as clear-intent terms.

⭐ Featured Recommendation

📈 Double Your Site Traffic & Revenue with Featured Backlinks & SEO

Get a pinned Featured Backlink in our directory or a complete SEO audit by NOOR 4 TECH experts to dominate rankings.

Explore Featured Package ⭐
💼 NOOR 4 TECH Professional Services

Looking for a Technical Partner for Your Next Project?

We deliver enterprise web development, mobile apps, technical SEO, and AI automation.

Author

Ibrahim Moustafa

Electronics and communications engineer, working in IT since 2004. He reviews NOOR 4 TECH articles and tests its tools himself before publication.

Comments (0)

No comments yet. Be the first to comment!

Add Your Comment

Your email will not be published. Comments require review before publishing.