Loading video player…

Read Syntax Like the Parser

In this video, you read Rosa’s home page the way the browser’s parser reads it. The parser follows HTML’s own rules, not the way your file looks:

  • A slash closes nothing. <div class="plot-map" /> is only a start tag. The div stays open, and the paragraph, both headings and both events after it land inside, because a div, unlike a paragraph, may hold headings. It closes only when the parser reaches </main>. A div has no look of its own, so the page looks the same. Hover the div in the Elements panel, and its box covers everything down to the seed swap, so a style for that box would reach all of it. Write <div class="plot-map"></div> instead.
  • Only void elements stand alone. A void element can’t hold content, so it has no end tag. The common ones are <meta>, <link>, <img>, <br>, <hr> and <input>. A slash on one does nothing either, and the Elements panel shows <meta charset="utf-8" /> as <meta charset="utf-8">. Leave the slash off, so you won’t expect it to close a div. Give every other element its end tag, so the tree is the one you meant. HTML lets you omit a few end tags, like a paragraph’s (<p>First<p>Second builds two paragraphs), but this course always writes them.
  • Whitespace shows as one space. The tree keeps your text’s line breaks and indentation, but by default the browser displays each run of spaces, tabs and line breaks as one space, and none at the start or end of the text. So the work day paragraph, with its last sentence on its own indented line, shows its two lines joined by one space.
  • A < in text starts a tag when a letter follows it. Rosa’s address in angle brackets vanished from the page, because the parser read it as an element. Write the character reference &lt; for <. A > on its own starts nothing, but &gt; keeps the pair matching. An ampersand starts every reference, so you write one in your text as &amp;.

Here’s the <main> of index.html at the end of the video:

Language: HTML Filename: index.html
<main>
  <h1>Elm Street Community Garden</h1>
  <p>Volunteers grow vegetables and herbs here every season.</p>
  <div class="plot-map"></div>
  <p>Plots are 3 by 4 metres.</p>
  <h2 id="work-day">Next Work Day</h2>
  <p class="event">
    Saturday, November 28, from 9 a.m. to noon.
      Bring <em>gloves</em>.
  </p>
  <h2 id="seed-swap">Spring Seed Swap</h2>
  <p class="event family-friendly">
    Sunday, March 21, from 2 p.m. to 4 p.m. Bring seeds to share.
  </p>
  <p>Questions? Email Rosa &lt;[email protected]&gt;.</p>
</main>

Next comes Exercise: Write the Work Day Notice, where you write Rosa a one-page notice for Saturday’s work day. You write and run your code in the browser, and automated tests tell you what passes and what to fix.

00:00 Your garden’s home page has a complete skeleton now. In this video, you’ll read some syntax the way the browser’s parser reads it, which isn’t always how it looks.

00:10 One day, Rosa wants a map of the plots on the home page. For now, she asks for an empty space to hold it after the welcome paragraph, then a line on plot sizes.

00:21 No HTML element means “plot map”, so a div does the job. A div is a box with no meaning of its own, for content that no other element describes.

00:32 The box stays empty, so you might close it in the same tag, with a slash before the closing bracket. That’s how JSX and XML write an empty element. Save, and the page looks fine.

00:46 But a page can look fine while its tree is wrong. Your turn: pause the video and predict. In the Elements panel, is the plot-size paragraph inside the div, or after it?

01:03 It’s inside. Open the div in the Elements panel, and it holds the paragraph, both headings, and both events.

01:12 In HTML, that slash means nothing on a div. The parser reads a start tag, so the div opens. Everything after it lands inside, since a div, unlike a paragraph, may hold headings.

01:26 The div closes only when the parser reaches the end tag of main, its parent. A div has no look of its own, so the page looked the same. But hover over the div in the Elements panel, and its box on the page covers everything down to the seed swap.

01:42 Style that box later, and all of it changes.

01:46 The fix is an end tag, right after the start tag. Save, and the div is empty, with the paragraph after it.

01:55 Only void elements stand alone, like the two meta lines in your skeleton. They can’t hold content, so they have no end tag. A slash does nothing on them either.

02:06 Add one to the charset line, and the Elements panel shows the meta element with no slash, as if you’d never typed it. So leave it off, and you won’t expect it to close a div.

02:17 Here’s the rule, with the common void elements. In this course, you give every other element its end tag, so the tree is the one you meant.

02:27 Text has rules, too. Put the work day’s last sentence on its own indented line. The tree keeps the line break and spaces, but the page joins the lines with one space.

02:38 By default, each run of whitespace displays as one space, and none at the text’s start or end.

02:46 Rosa also wants her email address at the bottom, in angle brackets, the way a mail app shows it. Type it in as it is, and the address vanishes from the page.

02:57 A less-than sign followed by a letter starts a tag. So the parser read the address as an element, and tags never show. To show a less-than sign as text, write a character reference: an ampersand, a short name, and a semicolon.

03:13 A greater-than sign on its own starts nothing, but its reference keeps the pair matching. Save, and the address shows, brackets and all. An ampersand starts every reference, so one in your text can be misread.

03:28 The chip’s last line writes it safely.

03:32 The parser reads syntax by HTML’s rules, not by how the file looks. A slash closes nothing, so only void elements stand alone. Runs of whitespace show as one space.

03:44 And you write a less-than sign in text as a reference. Next comes Exercise: Write the Work Day Notice. You’ll write Rosa a one-page notice for Saturday’s work day.

03:56 You write and run your code right in the browser, and automated tests tell you what passes and what to fix.

Become a Member to join the conversation.