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

Common HTML and CSS Mistakes Beginners Make (With a 15-Question Self-Test)

Every HTML and CSS beginner makes the same twelve mistakes. That is good news: it means the errors are predictable, the fixes are short, and once you have seen each one with the wrong code next to the right code, you stop making it.

This article is not a full HTML and CSS course. It assumes you have built a page or two and you keep hitting the same walls: a layout that is wider than the screen for no visible reason, a button that will not centre, a style that refuses to apply. For each mistake you get four things: the wrong code, what you actually see, why it happens, and the fixed code. At the end there is a 15-question self-test — do it honestly, check your score, and re-read the mistakes you missed.

If your mistakes are mostly about layout — rows that will not line up, cards that wrap badly — read our Flexbox vs Grid mistakes guide after this one; it covers the layout half of this gap and shares no question with this test. If you are also learning Python, the same format is waiting in our common Python mistakes guide.

Close-up of HTML code lines on a computer screen, for common HTML and CSS mistakes beginners make

Photo by Pixabay on Pexels

Mistake 1 — Unclosed and wrongly nested tags

Wrong code:

<div class="card">
  <h2>Our Services</h2>
  <p>We build websites.
</div>

What you see: the paragraph swallows everything after it, or the card's border and background stretch much further down the page than you expected. The browser tries to guess where the </p> should have gone, and its guess is rarely yours.

Why it happens: HTML is forgiving — the browser will not show you an error for a missing closing tag. It silently repairs the document, and the repair changes your layout. Wrong nesting is worse: <b><i>text</b></i> is invalid because tags must close in the reverse order they opened.

Fixed code:

<div class="card">
  <h2>Our Services</h2>
  <p>We build websites.</p>
</div>

Close every tag you open, in reverse order. If your layout suddenly breaks after a small edit, a missing closing tag is the first thing to check — indent your code consistently and the gap becomes visible.

Mistake 2 — Using <div> for everything

Wrong code:

<div class="heading">Welcome</div>
<div class="button" onclick="submitForm()">Sign up</div>

What you see: the page looks fine — until someone navigates it with a keyboard or a screen reader. The "button" cannot be reached with the Tab key, cannot be activated with Enter, and a screen reader announces it as plain text. Search engines also learn nothing about the page's structure.

Why it happens: <div> is the easy default, and it styles exactly like any other element. But HTML elements carry meaning: a <button> is focusable and clickable by keyboard, an <h1> tells search engines and screen readers that this is the page's main heading.

Fixed code:

<h1>Welcome</h1>
<button type="submit">Sign up</button>

Use the element that means what you want: <h1>–<h6> for headings, <button> for actions, <a> for links, <nav>, <header>, <main> and <footer> for page regions. Style them with CSS afterwards — semantics first, appearance second.

Mistake 3 — Forgetting alt text on images

Wrong code:

<img src="team-photo.jpg">

What you see: nothing wrong — until the image fails to load, and your reader sees a broken-image icon instead of any clue about what was there. Screen reader users get nothing at all: the image is simply skipped, or announced as its file name.

Why it happens: the alt attribute is optional in the sense that the page still renders without it, so beginners skip it. But alt is the image's text equivalent: it is what shows when the image cannot load, and what assistive technology reads aloud.

Fixed code:

<img src="team-photo.jpg" alt="Our four-person team smiling in the office">

Write alt text that says what the image shows, briefly. If the image is purely decorative — a background flourish that adds no information — use an empty alt (alt=""), which tells screen readers to skip it deliberately rather than by accident.

Mistake 4 — Writing className in plain HTML

Wrong code:

<div className="card">Hello</div>
.card { background: #f0f0f0; }

What you see: the style never applies. The div sits there unstyled, and no error appears anywhere.

Why it happens: className is the JavaScript/React way of setting a class. In plain HTML the attribute is class. The browser does not recognise className, so it treats it as an unknown attribute and ignores it — and your .card rule has nothing to attach to.

Fixed code:

<div class="card">Hello</div>

In HTML files, always write class. Use className only inside JSX (React). If a style silently fails to apply, check the attribute name before you blame the CSS.

Mistake 5 — Styling everything inline

Wrong code:

<p style="color: red; font-size: 18px; margin-top: 20px;">Warning</p>
<p style="color: red; font-size: 18px; margin-top: 20px;">Error</p>
<p style="color: red; font-size: 18px; margin-top: 20px;">Alert</p>

What you see: it works — until you need to change the colour. Then you must edit every element by hand, and you will miss one. Inline styles also beat almost every stylesheet rule, so later you cannot override them from your CSS without !important.

Why it happens: inline styles feel faster when you are learning, because the effect is immediate and there is no selector to think about. The cost arrives later, as duplication.

Fixed code:

<p class="notice">Warning</p>
<p class="notice">Error</p>
<p class="notice">Alert</p>
.notice {
  color: red;
  font-size: 18px;
  margin-top: 20px;
}

One class, one place to change. Keep inline styles for the rare case where a single element genuinely needs a one-off value — for example, a width set by JavaScript.

Mistake 6 — The box model surprise

Wrong code:

.box {
  width: 300px;
  padding: 20px;
  border: 5px solid black;
}

What you see: the box is 350px wide on screen, not 300px — and two 300px boxes side by side overflow their 600px container, pushing the second one onto the next line or off the screen entirely.

Why it happens: the default box-sizing value is content-box, which means width applies to the content only. Padding (20px on each side) and border (5px on each side) are added on top: 300 + 40 + 10 = 350px. This is documented MDN behaviour, but it surprises everyone the first time.

Fixed code:

* {
  box-sizing: border-box;
}

.box {
  width: 300px;
  padding: 20px;
  border: 5px solid black;
}

With border-box, the width includes padding and border, so the box is exactly 300px wide. Most developers put the universal box-sizing: border-box rule at the top of every stylesheet — do the same, and this whole class of bug disappears.

Detailed close-up of HTML code on a computer monitor, for the HTML mistakes section

Photo by Pixabay on Pexels

Mistake 7 — Margins that collapse

Wrong code:

h2 { margin-bottom: 40px; }
p  { margin-top: 30px; }

What you see: the gap between the heading and the paragraph is 40px, not 70px. You add more margin, and the gap barely moves.

Why it happens: vertical margins between neighbouring block elements collapse — the browser uses the larger of the two, not the sum. Two adjacent margins of 40px and 30px produce a 40px gap. Margins also collapse through a parent with no padding or border, so a child's top margin can leak out and push the parent down instead.

Fixed code:

h2 { margin-bottom: 40px; }
p  { margin-top: 0; }

Set the margin on one side only — usually margin-bottom — and keep it consistent. If a child's margin is leaking out of its parent, give the parent a little padding or a border, or use overflow: auto on the parent, and the collapse stops.

Mistake 8 — Setting width and height on inline elements

Wrong code:

a.button {
  width: 200px;
  height: 50px;
  background: blue;
}
<a class="button" href="#">Buy now</a>

What you see: the link stays the size of its text. The width and height are ignored completely, and the blue background hugs the words.

Why it happens: <a>, <span>, <strong> and other inline elements do not accept width or height — those properties only apply to block-level and inline-block elements. Inline elements size themselves to their content, and that is by design.

Fixed code:

a.button {
  display: inline-block;
  width: 200px;
  height: 50px;
  background: blue;
}

display: inline-block keeps the element sitting in the line of text like an inline element, but lets it take a width, height, padding and margin like a block. This is the standard fix for styling links as buttons.

Mistake 9 — Centering that does nothing

Wrong code:

.box {
  margin: 0 auto;
}
<div class="box">Hello</div>

What you see: the box stays glued to the left edge. margin: 0 auto is the classic centering trick, and it appears to do nothing.

Why it happens: auto margins can only share out leftover space, and a block-level <div> takes the full width of its parent by default — so there is no leftover space to share. The trick only works when the element is narrower than its parent.

Fixed code:

.box {
  width: 400px;
  margin: 0 auto;
}

Give the element a width (or a max-width), and the auto left and right margins split the remaining space equally. For centering text inside a box, use text-align: center on the parent; for centering a box both ways, Flexbox's justify-content: center with align-items: center is the modern answer — see our Flexbox vs Grid guide.

Mistake 10 — Absolute positioning without a positioned parent

Wrong code:

.card { /* no position set */ }
.badge {
  position: absolute;
  top: 10px;
  right: 10px;
}
<div class="card">
  <span class="badge">New</span>
</div>

What you see: the badge flies to the top-right corner of the whole page, not the corner of the card.

Why it happens: an absolutely positioned element positions itself relative to its nearest positioned ancestor — an ancestor with any position value other than static, which is the default. With no positioned ancestor, it uses the initial containing block, effectively the page itself.

Fixed code:

.card {
  position: relative;
}
.badge {
  position: absolute;
  top: 10px;
  right: 10px;
}

Add position: relative to the parent. It does not move the parent at all — it simply becomes the reference point for the absolutely positioned child. This one line fixes the large majority of "my badge/tooltip is in the wrong place" bugs.

Mistake 11 — z-index that does nothing

Wrong code:

.modal {
  z-index: 9999;
}

What you see: the modal still sits behind the header, despite a z-index of 9999. Raising it to 99999 changes nothing.

Why it happens: z-index only applies to positioned elements — elements with position set to relative, absolute, fixed or sticky. On a statically positioned element it is ignored entirely. The other common cause is stacking contexts: once a parent creates a stacking context (for example with opacity below 1 or a transform), no z-index on its children can ever escape above an element outside that parent.

Fixed code:

.modal {
  position: fixed;
  z-index: 9999;
}

First give the element a position, then set z-index. If it still loses, check whether an ancestor creates a stacking context — move the modal out of that ancestor, or raise the ancestor's own z-index instead of the child's.

Mistake 12 — Specificity wars and the !important habit

Wrong code:

p { color: black; }
.intro { color: blue; }
#lead { color: red; }
<p id="lead" class="intro">Hello</p>

What you see: the text is red, even though .intro comes later in the stylesheet and you expected blue. So you add !important to .intro, and then another !important somewhere else to beat that one, until the stylesheet is a shouting match.

Why it happens: CSS does not resolve conflicts by order alone — it uses specificity. An ID selector beats any number of class selectors, and a class beats any number of element selectors. Inline styles beat IDs. !important beats all normal declarations, which is why it feels like a fix and acts like a trap: every !important forces the next override to be louder.

Fixed code:

.intro { color: blue; }
<p class="intro">Hello</p>

Drop the ID from the styling, and the class rule wins cleanly. Reserve IDs for JavaScript hooks and fragment links, not for styling. Treat !important as a last resort for overriding third-party code you cannot edit — never as your first response to a rule that "won't apply". When a rule will not apply, the cause is almost always specificity, and the fix is a simpler selector, not a louder one.

The 15-question self-test

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

1. What does this render as — a 300px box, or wider?

.box { width: 300px; padding: 20px; border: 5px solid black; }

(a) Exactly 300px wide (b) 350px wide (c) 330px wide (d) It depends on the screen size

Answer: (b) — With the default content-box, width covers content only; padding and border are added: 300 + 40 + 10 = 350px. box-sizing: border-box makes it exactly 300px.

2. Why does this style never apply?

<div className="card">Hello</div>

(a) The CSS file is not linked (b) className is not an HTML attribute — the correct attribute is class (c) The class name is too short (d) Divs cannot have classes

Answer: (b) — className belongs to JSX/React. In plain HTML the attribute is class; the browser ignores the unknown attribute.

3. Which element should a "Sign up" action use?

(a) <div onclick="..."> (b) <span> (c) <button> (d) <h2>

Answer: (c) — A <button> is keyboard-focusable, activatable with Enter, and announced correctly by screen readers. A <div> is none of those.

4. What is the correct alt text approach for a team photo?

(a) Leave alt out — it is optional (b) alt="image" (c) alt="Our four-person team smiling in the office" (d) alt="team-photo.jpg"

Answer: (c) — Alt text should describe what the image shows. The file name and the word "image" tell the reader nothing.

5. Two adjacent elements have margin-bottom: 40px and margin-top: 30px. What is the gap between them?

(a) 70px (b) 40px (c) 30px (d) 10px

Answer: (b) — Vertical margins collapse to the larger of the two, not the sum.

6. Why is this link still text-sized?

a.button { width: 200px; height: 50px; }

(a) Links cannot be styled (b) The width is too large (c) <a> is an inline element, and inline elements ignore width and height (d) The CSS selector is wrong

Answer: (c) — Add display: inline-block and the width and height will apply.

7. Why does margin: 0 auto fail to centre this box?

.box { margin: 0 auto; }

(a) Auto margins do not work on divs (b) The div is full-width by default, so there is no leftover space to share (c) You need margin: auto on all four sides (d) Centering requires Flexbox

Answer: (b) — Give the box a width (or max-width) narrower than its parent, and the auto margins will centre it.

8. Where does this badge end up?

.badge { position: absolute; top: 10px; right: 10px; }

inside a .card with no position set?

(a) The top-right corner of the card (b) The top-right corner of the page (c) It disappears (d) Below the card

Answer: (b) — With no positioned ancestor, an absolutely positioned element uses the initial containing block — effectively the page. Add position: relative to .card.

9. Why does z-index: 9999 fail on this element?

.modal { z-index: 9999; }

(a) The number is too large (b) z-index only works on positioned elements, and this one has no position set (c) Modals cannot use z-index (d) You need z-index: 99999

Answer: (b) — Add position: relative, absolute, fixed or sticky first. If it still loses, an ancestor may be creating a stacking context.

10. In this CSS, what colour is the paragraph?

p { color: black; }
.intro { color: blue; }
#lead { color: red; }
<p id="lead" class="intro">Hello</p>

(a) Black (b) Blue (c) Red (d) It depends on the order in the file

Answer: (c) — An ID selector beats any number of classes, regardless of order. Remove the ID from styling and .intro wins.

11. Which closing order is valid?

(a) <b><i>text</b></i> (b) <b><i>text</i></b> (c) <b><i>text</b> (d) <i><b>text</i></b>

Answer: (b) — Tags must close in reverse order of opening. (a) and (d) cross the tags; (c) leaves <i> unclosed.

12. Three identical paragraphs are styled inline with the same three declarations. What is the main problem?

(a) Inline styles do not work on paragraphs (b) Changing the style means editing every element by hand, and inline styles are hard to override later (c) The page loads more slowly (d) Inline styles are invalid HTML

Answer: (b) — Use one class in the stylesheet instead; inline styles are for genuine one-offs.

13. A child's top margin pushes its parent down instead of spacing the child from the parent's top edge. What is happening?

(a) The parent has no height set (b) The margins are collapsing through the parent (c) The child is absolutely positioned (d) The parent is a flex container

Answer: (b) — A child's margin can collapse through a parent with no padding or border. Give the parent padding, a border, or overflow: auto.

14. Which image markup is best for a purely decorative flourish that adds no information?

(a) <img src="flourish.png"> (b) <img src="flourish.png" alt="flourish"> (c) <img src="flourish.png" alt=""> (d) <img src="flourish.png" alt="decorative image">

Answer: (c) — An empty alt tells screen readers to skip the image deliberately. Omitting alt or writing "flourish" both add noise.

15. A style "won't apply" and your first instinct is !important. What should you check first?

(a) Whether the stylesheet is linked (b) Specificity — a more specific selector elsewhere is probably winning (c) Whether the browser is up to date (d) Whether the property name is spelled correctly

Answer: (b) — A rule that will not apply is almost always losing on specificity. !important papers over it and starts a war. (a) and (d) are worth a glance too, but specificity is the usual cause.

Close-up of HTML code on a computer screen in dark mode, for the CSS self-test section

Photo by César Gaviria on Pexels

Your score

Score (out of 15) What it means
13–15 Strong — these mistakes are behind you. Move on to layout: our Flexbox vs Grid guide covers the mistakes this article deliberately left out.
9–12 Solid — re-read the mistakes you missed, especially the box model and specificity. Those two cause the most real-world debugging time.
5–8 Shaky — rebuild one small page from scratch using only the fixed code in this article, and check each rule as you add it.
0–4 Start again at Mistake 6 (the box model) and Mistake 12 (specificity). Those two ideas unlock most of CSS.

What to do next

The fastest way to make these fixes permanent is to use them on a real page today: add box-sizing: border-box to your stylesheet, replace one <div> button with a real <button>, and give every image its alt text. Then take the layout half of this gap — the Flexbox vs Grid mistakes guide — and its own self-test. If you have not yet planned your learning order, our 90-day web development roadmap shows where HTML and CSS sit in your first three months, and if you want the same wrong-code / fixed-code treatment for Python, it is in our common Python mistakes guide.

Twelve mistakes, fifteen questions, and no new concepts to memorise — just the ones that were already tripping you up, named and fixed.

Post a Comment

Previous Post Next Post

Featured Post