Spot the Error

The mistakes that come up again and again in HTML and CSS — what each one looks like, what it does, and a method for finding them.

Why errors hide

The three languages fail in three different ways:

When something is wrong
HTMLthe browser guesses. It repairs what it can and shows the page anyway — often looking almost right
CSSthe browser skips. The broken declaration, or the whole rule, is ignored — without a message
JavaScriptthe code stops. A red message appears in the Console

So HTML and CSS errors do not announce themselves. You have to see them — which is exactly what the exam asks of you.

Two tools see them for you: the W3C validator for HTML, and DevTools → Styles, which strikes through every CSS declaration it could not use.


A method: two checklists

Go through the code line by line, once for each question. It is slower than reading it once — and it finds what reading once misses.

HTML

  1. Does every tag that opens also close — in the reverse order it opened?
  2. Is every quote closed? href="about.html" — two quotes, always.
  3. Does every <img> have an alt?
  4. Is everything visible inside <body>, and only information about the page inside <head>?
  5. Is every <li> inside a <ul> or an <ol>?
  6. Is every path written with /, in lower case, without spaces, and relative to the site?
  7. Is every id used only once, and does every <label for> match an id?
  8. Are the element names spelled right? <header>, not <heder>.

CSS

  1. Does every { have its }?
  2. Does every declaration end with ;?
  3. Is there a colon between property and value — not =, not nothing?
  4. Is the property name real? color, never colour. background-color, never background-colour.
  5. Does every length have a unit, with no space before it? 16px — not 16, not 16 px.
  6. Does the selector match the HTML? . for a class, # for an id — same spelling, same capitals.
  7. Are the comments /* … */? // and <!-- --> are not CSS.
  8. Is the stylesheet linked, with the right file name?

HTML errors

You seeWhat is wrongWhat happens
<h2>Our places and no </h2>the heading is never closedwhat follows becomes part of the heading — big and bold
<p>Open <strong>daily</p></strong>closed in the wrong orderthe browser repairs it — but it is invalid, and the repair is a guess
<a href="menu.html>Menu</a>a quote is never closedthe address swallows the text after it: the link vanishes, and the next one breaks
<img src="hero.webp">no altinvisible to anyone who cannot see it; the validator reports it
<img src="hero.webp" alt="…"></img><img> has no closing taginvalid: </img> is a stray end tag
<li> directly inside <footer>a list item outside a listinvalid: <li> belongs inside <ul> or <ol>
<h1> inside <head>visible content in the headthe browser moves it into the body — but it is invalid
a <head> with no <title>the title is missinginvalid; the tab shows the file name instead
two elements with id="contact"an id used twiceinvalid: an id is unique on its page
<label for="email"> and <input id="mail">for and id do not matchclicking the label does nothing; a screen reader cannot name the field
<heder>a misspelled elementthe browser does not know it: no meaning, no default style
nav.html starting with <!DOCTYPE html>a fragment written as a whole pagean <html> inside an <html> — the browser throws most of it away

Not every missing end tag is an error. HTML lets you leave out </li> before the next <li>, and </p> before a new block such as another <p> or a heading — the validator accepts both. Close them anyway: it is clearer, and leaving them out is how the real errors hide.

Valid — but wrong

These pass the validator. They are still mistakes, because they break what HTML is for: saying what things are.

You seeWhy it is wrong
an <h1>, then an <h4>headings chosen by size: the outline skips levels. Want smaller text? That is CSS
two <h1> on one pageone page, one main heading
<b>Warning</b>bold pixels with no meaning — <strong> says it matters
<div class="nav">a navigation that does not say so: use <nav>
<a href="work.html">click here</a>link text is read out of context. Say where it goes
alt="image" or alt="IMG_2041.jpg"alt text that describes nothing
<input placeholder="Email"> and no labelthe placeholder vanishes as soon as you type
a <table> holding the page layouttables are for data, never for layout
href="https://mei.example.com/about.html" to your own pagean absolute address to your own page breaks when the site moves

CSS errors

You seeWhat is wrongWhat happens
color: #333 with no ; — then font-size: 16px;a missing semicolonboth lines are lost: the browser reads #333 font-size: 16px as one value for color
a rule with no closing }a missing bracethe rules after it stop working
colour: red;not a property — CSS spells it colorignored
text-color: red; · font-colour: red;not properties eitherignored
font-size: 16;a length with no unitignored. Only 0 may stand alone
font-size: 16 px;a space between the number and the unitignored
color = red; · color red;no colonignored
.Quote for class="quote"class names are case-sensitivematches nothing
#quote for class="quote"# is for an id, . for a classmatches nothing
quote { … } for class="quote"the dot is missinglooks for a <quote> element — there is none
// the headernot a CSS commentthe rule below it breaks. Write /* the header */
href="styles.css" for a file called style.cssthe file name does not matchno styling at all; DevTools → Network shows a 404

Two notes that stop you finding errors that are not there:

  • The last semicolon in a rule is optional. That is why a missing one hides: nothing breaks until someone adds a line below it.
  • Some properties take plain numbers, and that is correct: line-height: 1.5, font-weight: 800.

Works — but not how you think

You seeWhat actually happens
color: red; to make a box redcolor is the text colour. The box is background-color
footer { … } to style the site footerit also styles every <footer> inside an <article> — every quote card. Use #site-footer
style="…" on twenty elementstwenty places to change. One class, one rule

Paths and file names

Your site will live on a Linux server, where Photo.jpg and photo.jpg are two different files. Windows and macOS treat them as the same file — which is why a site can work on your laptop and break online.

PathVerdict
images/hero.webp✓ relative, lower case, forward slash
Images/Hero.webp — but the folder is images, the file hero.webp✗ capitals: works on your laptop, 404 on the server
images\hero.webp✗ a backslash is a Windows habit. Addresses use /, and the validator rejects \
C:\Users\mei\Desktop\my-site\images\hero.webp✗ a path on your own disk. Nobody else has your C: drive
www.example.com/logo.png✗ no https://, so the browser looks for a folder called www.example.com inside your site
https://example.com/logo.png✓ a valid absolute address — but on someone else's server
images/my photo.webp✗ a space: it becomes %20, and the validator rejects it
File nameVerdict
about.html✓
index.html✓ — and special: the front page of its folder
beach-photo-2024.webp✓ lower case, hyphens
About Me.html✗ a capital and a space
contact_form.htmlworks — but the rule is hyphens
café-menu.html✗ é is not in a–z; in an address it turns into caf%C3%A9-menu.html
关于我们.html✗ the same, for every character. Write the page in Chinese; name the file in a–z
photo.JPG✗ capitals in the extension count too

Practice

The code is shown without colours, as it will be on paper. Colours would give some errors away.

1 · Five errors in HTML

Give the line number, say what is wrong, and say what would fix it.

1   <!DOCTYPE html>
2   <html lang="en">
3   <head>
4     <meta charset="UTF-8">
5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
6     <link rel="stylesheet" href="style.css">
7   </head>
8   <body>
9     <header>
10      <h1>Coconut Radio
11    </header>
12    <main>
13      <section id="gigs">
14        <h2>Next gigs</h2>
15        <img src="images/stage.webp" alt="The band on stage at a beach bar in Haikou"></img>
16      </section>
17      <section id="gigs">
18        <h2>Join the mailing list</h2>
19        <label for="email">Your email</label>
20        <input id="mail" type="email">
21      </section>
22    </main>
23  </body>
24  </html>
Show the answers
  1. Lines 3–7: there is no <title>. Every page needs one — without it, the tab shows the file name. Add <title>Coconut Radio</title> to the head.
  2. Line 10: the <h1> is never closed. Here the browser closes it at </header>, so it happens to look right — but it is invalid, and one line later it would not look right. Add </h1>.
  3. Line 15: </img>. <img> has no closing tag. Delete it.
  4. Line 17: id="gigs" is already used on line 13. An id is unique on its page. Give this section its own, such as id="mailing-list".
  5. Lines 19–20: for="email", but id="mail". They must match, or the label is not connected to the field.

2 · Five errors in CSS

The HTML this styles uses <article class="quote"> for each quote.

1   body {
2     font-family: system-ui, sans-serif;
3     line-height: 1.5;
4     background-colour: #fbfaf7;
5   }
6
7   main {
8     max-width: 48rem
9     margin: 0 auto;
10  }
11
12  #quote {
13    padding: 1.5rem;
14    border-left: 4px solid #c2410c;
15  }
16
17  .quote cite {
18    color: #6b7280;
19    font-size: 14;
20  }
21
22  nav a {
23    text-decoration: none;
24
25  nav .logo {
26    font-weight: 800;
27  }
Show the answers
  1. Line 4: background-colour. CSS spells it background-color. Ignored.
  2. Line 8: no semicolon. The browser reads 48rem margin: 0 auto as one value for max-width, so both lines are lost. Add ;.
  3. Line 12: #quote. The HTML uses class="quote", so the selector is .quote. As it is, it matches nothing.
  4. Line 19: 14 has no unit. Write 14px, or 0.875rem. Ignored as it is.
  5. Line 24: the nav a rule is never closed. The nav .logo rule ends up inside it and stops working. Add } after line 23.

Not errors: line-height: 1.5 and font-weight: 800 take plain numbers. That is correct.