About Us Contact Us Privacy Policy Disclaimer Pakistan Teaching Jobs 2026-27

HTML and CSS Common Mistakes Beginners Make (With a Self-Test): Flexbox vs Grid and More

Flexbox and Grid are the two layout tools in modern CSS, and beginners use them the way they use a hammer and a screwdriver before anyone explains which is which: the wrong one, held the wrong way, with growing force. The result is familiar — a row of cards that will not line up, a navigation bar that overflows on mobile, a grid that breaks the moment one card has a longer title than the others.

This article is the layout companion to our general HTML and CSS mistakes guide. That guide covers the box model, specificity, positioning and the rest; this one covers only layout. Eight mistakes, each with the wrong code, what you actually see, why it happens, and the fixed code — then a 12-question self-test. It assumes you already know what display: flex and display: grid do; if you do not, learn the basics first and come back, because this article is about the mistakes, not the tutorial.

HTML code displayed on a wide computer screen, for Flexbox vs Grid CSS layout mistakes

Photo by anshul kumar on Pexels

The decision first: Flexbox or Grid?

MDN puts the difference in one sentence: Flexbox is a one-dimensional layout method, Grid is a two-dimensional one. That is the whole decision, and it is the question to ask before you write a line of layout CSS: do these items need to line up with items in other rows or columns?

Layout need Use Why
Items flow along one line and distribute around their content Flexbox It manages one axis at a time and suits content-driven distribution
Items in several rows must share column alignment Grid Grid defines tracks across rows and columns, so items align to shared columns
Items need explicit placement, spanning or deliberate overlap Grid Grid supports placement across its two-dimensional track system
A component needs a one-axis arrangement inside a larger grid Both, nested Grid for the container's shared tracks, Flexbox for the component's internal row or column
A long article of headings and paragraphs Neither Normal flow is enough; a layout module only adds rules to maintain

Two warnings from MDN belong here before the mistakes begin. First, size is not the deciding factor: a tiny component can need two-dimensional placement, and a whole page section can need only one row. Second, if you find yourself setting careful widths on flex items just to make one row line up with the row above it, MDN's advice is direct — you should be using Grid instead. Most production layouts use both: Grid for the frame, Flexbox inside it.

Flexbox mistake 1 — Forgetting flex-wrap

Wrong code:

.nav {
  display: flex;
}
<nav class="nav">
  <a href="#">Home</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Blog</a>
  <a href="#">Contact</a>
</nav>

What you see: on a narrow screen the links squeeze together, shrink to unreadable widths, or overflow off the right edge of the screen. On a wide screen everything looks fine, which is why this mistake ships.

Why it happens: flex-wrap defaults to nowrap. A flex container keeps all its items on one line and shrinks them to fit, however narrow the container gets. Wrapping is opt-in, not the default.

Fixed code:

.nav {
  display: flex;
  flex-wrap: wrap;
}

With wrap, items that no longer fit move onto a new line instead of being crushed. Check every flex row on a 360px-wide screen — if it cannot wrap, it will break on a phone.

Flexbox mistake 2 — Mixing up justify-content and align-items

Wrong code:

.hero {
  display: flex;
  justify-content: center; /* trying to centre vertically */
  height: 400px;
}

What you see: the content centres horizontally but stays glued to the top of the hero. You add more justify-content values and nothing vertical changes.

Why it happens: justify-content aligns items along the main axis, and align-items aligns them along the cross axis. With the default flex-direction: row, the main axis is horizontal — so justify-content moves things left and right, and align-items moves them up and down. The names do not change when the direction changes: set flex-direction: column and suddenly justify-content is vertical and align-items is horizontal, because the axes have swapped.

Fixed code:

.hero {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 400px;
}

For a row: justify-content is horizontal, align-items is vertical. For a column: the reverse. When centring fails, say the axis out loud before you touch the property — "I want this centred vertically, so I need the cross-axis property" — and the right one follows.

Flexbox mistake 3 — Misunderstanding flex: 1

Wrong code:

.sidebar { flex: 1; }
.content { flex: 1; }

What you see: the sidebar and the content come out exactly the same width, even though the content obviously needs more room. Or one item refuses to shrink and overflows, even though you set flex: 1 on everything.

Why it happens: flex: 1 is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%. The flex-basis: 0% part is the surprise: it means every item starts from zero width and then shares out the free space equally, ignoring how much content each one holds. That is exactly right for equal columns and exactly wrong when the items need different shares. And flex: 1 does not override an item's minimum content size — a long unbreakable word can still force its item wider.

Fixed code:

.sidebar { flex: 1 1 200px; }
.content { flex: 3 1 400px; }

The three values are grow, shrink, basis. Here the content starts at 400px and grows three times as fast as the sidebar; both can shrink. When you want equal columns, flex: 1 is correct — when you want unequal ones, write the three numbers out and say what each means.

Flexbox mistake 4 — align-content when there is only one line

Wrong code:

.tags {
  display: flex;
  align-content: space-between;
  height: 200px;
}

What you see: nothing happens. The tags sit in one row at the top, and align-content appears to be broken.

Why it happens: align-content distributes lines of flex items — it only has an effect when the container has wrapped onto more than one line. With a single line there is nothing to distribute, so the property is silently ignored. align-items, by contrast, aligns items within their line, and works with one line or many.

Fixed code:

.tags {
  display: flex;
  flex-wrap: wrap;
  align-content: space-between;
  height: 200px;
}

Add flex-wrap: wrap so there are multiple lines for align-content to space out — or, if you only have one line, use align-items instead. The rule of thumb: align-items is for items in a line, align-content is for the lines themselves.

Close-up of colourful programming code on a screen, for the Flexbox mistakes section

Photo by Leonid Altman on Pexels

Grid mistake 5 — Using Flexbox for a card grid that must align

Wrong code:

.cards {
  display: flex;
  flex-wrap: wrap;
}
.card {
  width: 30%;
}

What you see: the cards look like a grid until one card has a longer title or a taller image — then the rows stagger, cards in the second row start at different heights, and the columns no longer line up. You fix it by tweaking widths, and the next card breaks it again.

Why it happens: once flex items wrap, each line lays itself out independently. Flexbox never looks at the placement of items in other rows, so it cannot keep columns aligned across rows. This is the exact situation MDN's rule describes: if you are setting widths on flex items to make items in one row line up with the items above them, you should use Grid.

Fixed code:

.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

Grid defines shared tracks, so every row uses the same three columns and everything lines up no matter how tall any single card is. Keep Flexbox for the inside of each card — the title, the text and the button in a column — and let Grid own the rows and columns.

Grid mistake 6 — auto-fill vs auto-fit

Wrong code:

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 16px;
}

What you see: with only two images in a wide container, they sit in the first two columns and a large empty space stretches to the right — the columns exist, but nothing fills them.

Why it happens: auto-fill creates as many tracks as fit, including empty ones. With two items in a container wide enough for four tracks, you get four tracks and two empty ones. auto-fit creates the same tracks but collapses the empty ones to zero, so the filled tracks stretch to take the whole width.

Fixed code:

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 16px;
}

Use auto-fit when you want the items you have to stretch across the container — the usual case for a gallery or card list. Use auto-fill when you deliberately want empty columns held open, for example when items will be added later and you do not want the layout to jump.

Grid mistake 7 — A grid item that overflows its track

Wrong code:

.layout {
  display: grid;
  grid-template-columns: 1fr 3fr;
}
<div class="layout">
  <aside>Menu</aside>
  <main>
    <img src="wide-chart.png" alt="Sales chart">
  </main>
</div>

What you see: the main column blows out wider than its 3fr share, the sidebar is squeezed, and the page gains a horizontal scrollbar. The fr units look like they are being ignored.

Why it happens: grid items have an automatic minimum size — min-width: auto — which means an item cannot shrink below the size of its content. A wide image, a long unbroken word or a fixed-width child forces its track wider than the fr share, and the fr calculation happens after that minimum is respected.

Fixed code:

.layout {
  display: grid;
  grid-template-columns: 1fr minmax(0, 3fr);
}

minmax(0, 3fr) lets the track shrink to zero if it must, so the fr share is honoured and the overflowing content is contained by its track (add max-width: 100% to images inside, so they scale down instead of overflowing). This is the single most common Grid bug in real projects, and the fix is seven characters.

Grid mistake 8 — Spanning without understanding grid lines

Wrong code:

.featured {
  grid-column: 1 / 3;
}

in a grid defined as grid-template-columns: repeat(3, 1fr).

What you see: the featured card spans two columns — but not the two you wanted. Or you change the grid to four columns and the featured card no longer spans the full width, because 1 / 3 is now two of four.

Why it happens: grid-column: 1 / 3 means "from line 1 to line 3" — line numbers, not column counts. In a three-column grid the lines are numbered 1 to 4, so 1 / 3 covers columns one and two. The numbers are positions on the grid's lines, and they go stale the moment the column count changes.

Fixed code:

.featured {
  grid-column: 1 / -1;
}

-1 means the last line, so 1 / -1 always spans the full width whatever the column count. For a named, self-documenting version, use grid-template-areas and place the card by its area name — then the layout reads as a picture of the page instead of a set of numbers.

The 12-question self-test

Answer before you look. Each question has one correct answer.

1. Cards in several rows must line up in the same columns. Which layout do you use?

(a) Flexbox with flex-wrap: wrap (b) Grid (c) Floats (d) display: inline-block

Answer: (b) — Wrapped flex lines lay themselves out independently and cannot keep columns aligned across rows. Grid defines shared tracks, so it is the right tool.

2. What is the default value of flex-wrap?

(a) wrap (b) nowrap (c) wrap-reverse (d) There is no default — it must be set

Answer: (b) — The default is nowrap: items stay on one line and shrink to fit. Wrapping is opt-in.

3. With flex-direction: row, which property centres items vertically?

(a) justify-content (b) align-items (c) align-content (d) text-align

Answer: (b) — In a row, the main axis is horizontal (justify-content) and the cross axis is vertical (align-items). Set flex-direction: column and the two swap.

4. What does flex: 1 expand to?

(a) flex-grow: 1; flex-shrink: 0; flex-basis: auto (b) flex-grow: 1; flex-shrink: 1; flex-basis: 0% (c) flex-grow: 1; flex-shrink: 1; flex-basis: auto (d) flex-grow: 0; flex-shrink: 1; flex-basis: 100%

Answer: (b) — The 0% basis is the surprise: items start from zero and share free space equally, ignoring their content size.

5. You set align-content: space-between on a flex container and nothing happens. Why?

(a) The property is misspelled (b) align-content only works when the container has wrapped onto more than one line (c) The container needs a width (d) space-between is not a valid value

Answer: (b) — align-content distributes lines, not items. With a single line there is nothing to distribute; use align-items instead, or add flex-wrap: wrap.

6. Which snippet makes a gallery of two images stretch across a wide container?

(a) repeat(auto-fill, minmax(250px, 1fr)) (b) repeat(auto-fit, minmax(250px, 1fr)) (c) repeat(2, 250px) (d) repeat(auto-fill, 250px)

Answer: (b) — auto-fit collapses empty tracks so the filled ones stretch. auto-fill keeps the empty tracks open, leaving blank space.

7. A grid column defined as 3fr is wider than its share and the page scrolls sideways. What is the most likely cause?

(a) The fr unit is broken in this browser (b) A grid item's content forces a minimum size larger than the fr share — grid items default to min-width: auto (c) The gap is too large (d) The grid needs flex-wrap: wrap

Answer: (b) — Fix it with minmax(0, 3fr), and give images inside max-width: 100%.

8. In a three-column grid, what does grid-column: 1 / -1 do?

(a) Spans the first column only (b) Spans columns one and two (c) Spans the full width, from the first line to the last line (d) Moves the item to the last column

Answer: (c) — -1 is the last grid line, so 1 / -1 always spans the full width, whatever the column count.

9. You are setting careful widths on flex items so one wrapped row lines up with the row above it. What does MDN advise?

(a) Keep adjusting the widths until they match (b) Switch to Grid (c) Add align-content: center (d) Use flex-basis instead of width

Answer: (b) — MDN's rule: if you are setting widths on flex items to make items in one row of a wrapped flex container line up with the items above them, use a two-dimensional grid layout instead.

10. A navigation bar, a row of buttons and a list of tags — which layout fits best?

(a) Grid, because there are several items (b) Flexbox, because each is a one-axis flow where items distribute around their content (c) Neither — use absolute positioning (d) Grid, because Flexbox is only for pages

Answer: (b) — These are one-axis distributions: Flexbox's home ground. Grid would work, but it adds track management for a job that needs none.

11. Inside a Grid card layout, each card holds a title, some text and a button in a column. What is the best structure?

(a) Grid for everything, including inside the cards (b) Grid for the card collection, Flexbox inside each card (c) Flexbox for the collection, Grid inside each card (d) Floats for the collection

Answer: (b) — The standard production pattern: Grid owns the shared rows and columns, Flexbox arranges each card's contents along one axis.

12. Which statement about choosing between Flexbox and Grid is correct?

(a) Grid is for big layouts and Flexbox is for small ones (b) The deciding question is whether items must line up with items in other rows or columns — not the size of the layout (c) You must pick one for the whole page and never mix them (d) Flexbox is newer, so it replaces Grid

Answer: (b) — MDN is explicit that size is not the deciding factor: a tiny component can be two-dimensional. Ask about alignment across rows and columns, and mix the two freely.

Colourful HTML code displayed on a computer screen, for the CSS Grid self-test section

Photo by Bibek ghosh on Pexels

Your score

Score (out of 12) What it means
10–12 Strong — you choose the right tool and debug it when it misbehaves. Build a real page layout with both and you will keep this.
7–9 Solid — re-read the mistakes you missed. The flex: 1 shorthand and the minmax(0, 1fr) fix cause the most real-world debugging time.
4–6 Shaky — rebuild the card-grid example from Mistake 5 twice, once with Flexbox and once with Grid, and watch where each one breaks.
0–3 Start again at the decision table. Until the one-axis vs two-axis question is automatic, every layout will feel like guessing.

What to do next

The fastest way to make this stick is to fix one real layout: take a card list you built with Flexbox and widths, rebuild it with repeat(auto-fit, minmax(250px, 1fr)), and compare the two at 360px wide. Then go back to the general HTML and CSS mistakes guide if you have not done its 15-question self-test — the box model and specificity mistakes there cause just as much debugging time as these layout ones. And if you have not yet planned your learning order, our 90-day web development roadmap shows where Flexbox and Grid sit in your first three months.

Eight mistakes, twelve questions, and one decision to memorise: do these items need to line up with items in other rows? If yes, Grid. If no, Flexbox.

Post a Comment

Previous Post Next Post