- The problem: stacked cards and a half-empty screen
- Flexbox and Grid: the two modern layout tools
- Flexbox: the core concept
- Flexbox in practice: a balanced navbar
- Grid: the core concept
- Build a responsive product grid with one line
- See the result yourself
- When to use Flexbox and when to use Grid
- Finer control with media queries
- Inspect the layout in DevTools
- Common Flexbox and Grid mistakes
- Summary and what's next
- FAQ
- 🔗 Official sources
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).

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:

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":
- Open
index.htmland wrap the six product cards in a container:<div class="products-grid">before the first card and</div>after the last one. - In
style.cssadd the grid rule:.products-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 20px; } - Remove
margin-bottomfrom.product-card;gaphandles the spacing now. - 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.
7. See the result yourself
The same six cards, before and after the grid:

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

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
- Putting
display: flexordisplay: gridon the wrong element: the property goes on the container and affects its direct children only, not deeper descendants. - Forgetting that
justify-contentfollows the main axis: withflex-direction: columnit distributes vertically, not horizontally. - Using
repeat(3, 1fr)and then wondering why cards are crushed on mobile: the fix isauto-fitwithminmax, or media queries. - Keeping manual margins between items after adopting
gap, which doubles the spacing. - Trying to align Flexbox items into multiple rows with fixed widths: that is the definition of Grid, so use it.
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
- MDN: Flexbox
- MDN: CSS Grid Layout
- MDN: Basic Concepts of Grid Layout
- web.dev: Learn CSS — Flexbox (Google's official course)
- web.dev: Learn CSS — Grid
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.
Comments (0)
No comments yet. Be the first to comment!
Add Your Comment