Thuta Learning
CSS
ProjectsWeb DevelopmentBeginner

Common Mistakes

CSSLesson 2930 min

What you'll walk away with

  • Predict which declarations the browser drops when one semicolon is missing
  • Check in DevTools, in order, whether a rule matches, whether it is crossed out, and its computed value
  • Write a defensive base stylesheet with box-sizing, max-width, overflow-x and overflow-wrap
  • Avoid fixes that only hide symptoms, such as !important, fixed heights and z-index: 99999

Explain it simply

Explain it simply

When JavaScript goes wrong, a red error appears in the console. CSS says nothing. It quietly skips any line it does not understand and carries on with the rest. That is why beginners often think "but I wrote it, why doesn't it show?" and start adding random properties.

Debugging CSS is a detective game: check the clues one at a time, in order, and the culprit turns up. The usual suspects are a class name that does not match, a missing semicolon, a misunderstood specificity battle, a box that overflows on a phone, an image with no width limit and a code block wider than the screen.

Problem → Why → Solution

The problem: one semicolon erases two declarations

The button below gets its padding but no blue background, and its text never turns white. The developer tries color: white !important and nothing changes. Then they add a second .button { background: blue; } rule further down and decide it is fixed, while the real bug is still sitting there.

The real cause is the missing semicolon after the background line. The browser reads the background value as "#2563eb color: white", which is not a valid colour, so it drops that whole declaration. The color property was swallowed into that value, so it disappears with it.

button.css
.button {
  background: #2563eb
  color: white;
  padding: 12px 20px;
}

How CSS handles errors

The CSS parser drops an invalid declaration up to the next semicolon (or the closing } of the block) and keeps reading. This is deliberate: it lets older browsers survive properties they do not know. The price is that there is no error message, so you have to open the Styles panel in DevTools yourself, where an invalid declaration is shown struck through with a warning icon.

Core concept: a five-step debugging routine

Adding random properties can hide a symptom, but it never fixes the cause. Right-click the broken element, choose Inspect, and ask these five questions in order. The first one that gets a "no" is where the bug lives.

1. Does the rule match the element?

If your rule is missing from the Styles panel entirely, the selector does not match. Typical causes: class="user-name" in the HTML but .username in the CSS, a forgotten dot, or a stylesheet that is not linked at all.

2. Is the declaration valid?

If the rule appears but a line is struck through with a warning icon, look for a misspelled property (backgound), a missing unit (width: 300) or a missing semicolon on the line above.

3. Did it lose the cascade?

A plain strike-through with no warning means another rule won, and the winner is listed above it. #sidebar a (one ID) beats .active (one class) wherever .active is written. Adjusting selectors beats adding !important.

4. What is the computed value?

The Computed tab shows the value the browser finally used, plus the box-model diagram. This is where you notice that width: 100% plus padding and border added up to 348px.

5. Is the layout context right?

Some properties only work in the right context. z-index affects positioned elements (or flex and grid items), width is ignored on inline elements, and height: 50% does nothing when the parent has no definite height.

SymptomMost likely causeSmallest fix
Rule never appearsClass name mismatchMake the names match
Two lines ignoredMissing semicolonAdd the ;
Sideways scroll on phoneswidth + padding + borderbox-sizing: border-box
Long URL leaves the boxNo space to break atoverflow-wrap: anywhere
z-index has no effectElement is not positionedposition: relative
Mental model: a doctor's checklist
  1. Does the rule match?
  2. Is the declaration valid?
  3. Did it lose the cascade?
  4. Check the computed value
  5. Check the layout context

A doctor diagnoses before prescribing. Do not reach for the painkiller called !important before you have found the cause.

General pattern

css
*, *::before, *::after { box-sizing: border-box; }

img, svg, video { max-width: 100%; height: auto; }

pre { overflow-x: auto; }

.text-container { overflow-wrap: anywhere; }

.layer { position: relative; z-index: 1; }

Defensive rules that sit at the top of most projects: predictable box sizes, media, code and long words that cannot overflow, and a z-index that actually applies because the element is positioned.

A base stylesheet that prevents most of these bugs

This page contains four kinds of content that regularly break phones: a full-width box with thick padding and border, a 900px SVG, a long URL without spaces and a long line of code. Thanks to four defensive rules, nothing scrolls sideways at 320px. Comment out one rule and run it again to see which content spills over.

index.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Defensive base</title>
  <style>
    /* 1. Padding and border stay inside the declared width */
    *, *::before, *::after { box-sizing: border-box; }

    body { margin: 0; padding: 16px; font-family: system-ui, sans-serif; }

    /* 2. Media is never wider than its parent */
    img, svg, video { display: block; max-width: 100%; height: auto; }

    /* 3. Long code scrolls inside its own box */
    pre { overflow-x: auto; padding: 12px; background: #f1f5f9; }

    /* 4. Long words and URLs may break instead of overflowing */
    p { overflow-wrap: anywhere; }

    .notice { width: 100%; padding: 24px; border: 4px solid #f59e0b; }
  </style>
</head>
<body>
  <div class="notice">Full-width box with thick padding and border</div>
  <svg viewBox="0 0 900 300" width="900" height="300" role="img" aria-label="Chart">
    <rect width="900" height="300" fill="#c7d2fe" />
  </svg>
  <p>Docs: https://example.com/?ref=defensive_styles_overflow_and_sizing_guide_mobile</p>
  <pre><code>grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));</code></pre>
</body>
</html>

Code breakdown

  1. Predictable sizing*, *::before, *::after { box-sizing: border-box; }

    Without it, .notice's width: 100% (288px) gets 48px of padding and 8px of border added on top, making 344px. With border-box, padding and border live inside the 288px.

  2. Media capimg, svg, video { display: block; max-width: 100%; height: auto; }

    max-width shrinks the SVG's width attribute of 900 down to 288px, and height follows the 3:1 ratio to 96px.

  3. Scroll inside the boxpre { overflow-x: auto; padding: 12px; background: #f1f5f9; }

    <pre> never wraps lines. overflow-x: auto keeps the long line inside the box with its own scrollbar, so the page itself does not widen.

  4. Break long wordsp { overflow-wrap: anywhere; }

    Browsers often wrap a URL after a slash, but the long underscore-joined part after ?ref= offers no break point at all. overflow-wrap: anywhere allows a break inside the word when nothing else fits.

You should see
At 320px: the notice box, the SVG (288×96), the URL paragraph and the code box all fit within 288px and the page has no horizontal scrollbar. The URL wraps onto a couple of lines, and the code line scrolls inside its own box.

In real projects

In real projects

Most modern CSS resets, such as the ones by Andy Bell and Josh Comeau, contain this lesson's box-sizing and media rules, and teams drop one in as the first file of every new project. Linters such as Stylelint catch missing semicolons, unknown properties and duplicate selectors before a commit, which partly solves the "CSS has no errors" problem.

When fixing a bug report, experienced developers often write a short note in the pull request: the symptom, the matching rule, the computed value, the layout context and the fix. A reviewer then understands at once why a single line was enough. A growing count of !important or z-index: 9999 in a codebase is a warning sign that a team's CSS architecture is under strain.

Common mistakes

"Fixes" that only hide the symptom

Winning with !important

!important solves a specificity problem for a moment, but the next person who needs to override that style needs another !important, and soon the stylesheet is an !important arms race. Look in DevTools at which rule wins, then adjust the selector or lower the existing rule's specificity.

Fixed heights on boxes that hold text

Giving cards height: 200px so they line up works with short English text, but with a Myanmar translation or a larger user font the text spills out of the box and overlaps the next element. Use min-height, or place the cards in a grid or a flex row, which makes them equal height automatically.

Escalating to z-index: 99999

When z-index seems ignored, the element is usually not positioned, or it is trapped in another stacking context. A parent with a transform, an opacity below 1, or a position plus z-index starts a new stacking context, and no child z-index, however large, can escape it. 99999 never fixes that.

Mini challenge

Repair a broken comment card

The starter CSS styles a comment card with five symptoms. Diagnose each one with the five-step routine and fix it with the smallest change, then combine it with the markup and test at 320px.

  • The card must not be wider than a 320px screen (border-box)
  • The username must be blue and bold (check both the selector and the semicolon)
  • The long URL must wrap inside the card
  • The long code line must scroll inside its own box
  • The reply button must be at least 44px tall
comment.css
/* Markup: article.comment > strong.name, p.text, pre > code, button.reply */

/* Symptom 1: the card is wider than a 320px screen */
.comment {
  width: 100%;
  padding: 20px;
  border: 2px solid #cbd5e1;
}

/* Symptom 2: the name is neither blue nor bold */
.comment .user-name {
  color: #1d4ed8
  font-weight: 700;
}

/* Symptom 3: a long URL in .text runs off the screen */
/* Symptom 4: the code line widens the whole page */
/* Symptom 5: .reply is only about 21px tall, hard to tap */

/* TODO: fix each symptom with the smallest change */

Expected: In a 320px viewport the card is 304px wide (body margin of 8px on each side) and the page has no horizontal scroll. The username is blue and bold, the URL wraps inside the card, the code scrolls in its box, and the Reply button is 44px tall.

Show hint

Symptom 1 is box-sizing. Symptom 2 hides two bugs: compare the class name with the markup, and check the line endings. For the button, use min-height.

Show solution
comment.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    *, *::before, *::after { box-sizing: border-box; }

    .comment {
      width: 100%;
      padding: 20px;
      border: 2px solid #cbd5e1;
    }

    .comment .name {
      color: #1d4ed8;
      font-weight: 700;
    }

    .text { overflow-wrap: anywhere; }

    pre { overflow-x: auto; background: #f1f5f9; padding: 8px; }

    .reply { min-height: 44px; padding-inline: 16px; }
  </style>
</head>
<body>
  <article class="comment">
    <strong class="name">mya_codes</strong>
    <p class="text">https://example.com/?a=box_sizing_border_box_fix_for_every_card</p>
    <pre><code>*, *::before, *::after { box-sizing: border-box; }</code></pre>
    <button class="reply">Reply</button>
  </article>
</body>
</html>

Quick quiz 1 / 3

In the problem section's .button rule (no semicolon after the background line), which declarations actually apply?

Quick quiz 2 / 3

With #sidebar a { color: gray; } and .active { color: red; }, an <a class="active"> sits inside #sidebar. What is the best way to make the link red?

Quick quiz 3 / 3

A <div class="badge"> in normal flow has only z-index: 10, and nothing changes. Why?

What to remember

  • CSS silently drops invalid declarations; one missing semicolon can cost two lines
  • Debug in order: match, valid, cascade, computed value, layout context
  • box-sizing, max-width: 100%, overflow-x: auto and overflow-wrap: anywhere prevent most mobile overflow
  • !important, fixed heights and z-index: 99999 hide symptoms; fix the cause instead

CSS cascade and specificity — MDN Web Docs

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Common Mistakes | Thuta Learning