Learn HTML and CSS: Valid HTML
Attributes and valid HTML
The browser forgives a lot: a forgotten quote, an id that appears twice. But whoever forgives has to guess. On Hilary's site it guessed wrong, and now the link to the cats jumps to the dogs.
The rules of an attribute
An attribute sits in the opening tag: a name, an = and a value in quotes, like alt="Misty, a grey cat". Two attributes are separated by a space.
Without quotes, the value stops at the first space: alt=Misty sleeps gives an alt of just Misty. The browser reads the rest as new, unknown attributes.
Each attribute appears only once in a tag. If it's there twice, only the first one counts.
<main> <img src="kat.svg" alt="Misty, a grey cat" width="96" height="96"> <p>Misty is looking for a home.</p></main>Characters with a meaning
Two characters mean something in HTML. A < followed by a letter starts a tag: in cat<dog everything from the < up to the next > disappears.
And an & starts a character reference. < becomes <, and & becomes &. If what follows your & happens to be the name of a reference, you'll see a completely different character: © becomes ©.
So write them as references: Cat & Dog shows Cat & Dog, and 3 < 5 shows 3 < 5.
<main> <h2>With thanks to</h2> <p>Cat & Dog pet food in Heywood, for every bag of biscuits.</p></main>An id is unique
An id is the name of one element. If the same id appears twice, the HTML is invalid, and when in doubt the browser picks the first one.
You notice it with a jump link: a link to #adopt jumps to the element with id="adopt", on the same page. Handy for a long page with a table of contents at the top.
If two chapters are both called adopt, the link always jumps to the first.
<main> <p><a href="#adopt">How do I adopt?</a></p> <section id="animals"> <h2>Looking for a home</h2> <p>Rex, Bella and Misty.</p> </section> <section id="adopt"> <h2>How to adopt</h2> <p>Fill in the form and come and see us.</p> </section></main>Try it yourself
Turn How do I adopt? into a link that jumps to the How to adopt chapter. To do that, give that chapter the id adopt.
<main> <p>How do I adopt?</p> <section> <h2>Looking for a home</h2> <p>Rex, Bella and Misty.</p> </section> <section> <h2>How to adopt</h2> <p>Fill in the form and come and see us.</p> </section></main>body { margin: 0; font-family: system-ui, sans-serif; background: #fffaf3; color: #3d2b1f;}header { padding: 20px 16px 14px; background: #f6c88f;}header h1 { margin: 4px 0 0;}.slogan { margin: 4px 0 10px;}nav ul { display: flex; flex-wrap: wrap; gap: 4px 18px; margin: 0; padding: 0; list-style: none;}nav a { color: #7a3e12; font-weight: bold;}main { padding: 8px 16px 24px;}table { border-collapse: collapse;}th,td { padding: 6px 12px; border: 1px solid #e3c9a8; text-align: left;}th { background: #fbe3c4;}.card { margin: 12px 0; padding: 12px 16px; border-radius: 16px; background: #ffffff;}.card h3 { margin: 4px 0;}.box { padding: 4px 16px; border-radius: 16px; background: #fde9d4;}.breed { color: #b5541c;}.closed { color: #b42318; font-weight: bold;}footer { padding: 12px 16px; background: #f6c88f;}Ready for more?
The full lesson has 5 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson