Find a course

Module 1: How Web Pages WorkLesson 2 of 1212 min

HTML Document Structure

Understand tags, elements, attributes and nesting, and build the standard skeleton of every HTML page with doctype, head, body and metadata.

Step 1 of 13Learn

Tags, elements and attributes

HTML is made of elements. Most elements have three parts:

<p>This is a paragraph.</p>
  • <p> is the opening tag.
  • </p> is the closing tag, the same name with a slash.
  • The text between them is the content.

Tag names aren't case-sensitive, but the convention is lowercase.

Many elements take attributes, extra information written inside the opening tag as name="value":

<a href="https://example.com">Visit Example</a>
<img src="cat.jpg" alt="A ginger cat asleep on a sofa">

The link's href attribute says where it goes. The image's src says which file to show, and alt describes it for people who can't see it.

Notice that <img> has no closing tag. Some elements have no content at all, so they're void elements: img, br (line break), hr (divider), input and meta are common examples. You may see them written <br /> with a slash in older code; in HTML both forms work.

Two attributes work on any element: id gives an element a unique name (one per page), and class puts it in a group that CSS can style. You'll use both constantly once you start writing CSS.

Step 2 of 13Practice · Spot the mistake

Tap the two items that are attributes in this element: <img src="logo.png" alt="Company logo" width="200">

Step 3 of 13Diagram

Anatomy of an HTML element

Anatomy of an HTML elementParts diagram: <a href="about.html">About us</a>, Opening tag, Attribute name, Attribute value, Content, Closing tag.<ahref="about.html">Aboutus</a>Opening tagAttributenameAttributevalueContentClosing tag

Tap a shape with to see more.

<a href="about.html">About us</a>
Opening tag

<a ...> starts the element.

Attribute name

href adds extra information to the element.

Attribute value

"about.html", always in quotes.

Content

About us, the text people see and click.

Closing tag

</a> has a slash and ends the element.

Step 4 of 13Learn

Nesting: elements inside elements

Elements can contain other elements. This is called nesting, and it gives every page a tree structure:

<p>This is <strong>very</strong> important.</p>

Here strong is a child of p, and p is its parent. The rule is simple but strict: close elements in the reverse order you opened them, like nested boxes. The last one opened must be the first one closed.

Correct:

<p>Read <em>this <strong>now</strong></em>.</p>

Wrong:

<p>Read <em>this <strong>now</em></strong>.</p>

Browsers are forgiving and will try to guess what you meant, but different guesses lead to strange styling bugs that are hard to track down. Indenting child elements, as in the examples in this course, makes the structure easy to see and mistakes easy to spot.

Some elements can only go in certain places. List items (li) belong inside a list (ul or ol). A paragraph can't contain a heading or another paragraph. A validator, a free online tool from the W3C, checks your HTML against these rules and points out errors line by line.

Step 5 of 13Practice · Spot the mistake

Tap the line that breaks the nesting rule.

Step 6 of 13Learn

The skeleton of every page

Every HTML document follows the same basic skeleton:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Fresh Bread Co. | Bakery in Portland</title>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <!-- visible content goes here -->
  </body>
</html>

Line by line:

  • <!DOCTYPE html> tells the browser to use modern standards mode. Without it, browsers fall back to an old "quirks" mode with odd behavior.
  • <html lang="en"> wraps everything and declares the page's language, which helps screen readers pronounce words correctly and helps translation tools.
  • <head> holds information about the page, not shown in the page itself.
  • <meta charset="utf-8"> sets the character encoding so accented letters and symbols display correctly.
  • <meta name="viewport" ...> makes the page display at the right size on phones. You'll see why in the responsive design lesson.
  • <title> sets the text in the browser tab and the clickable headline in search results.
  • <link rel="stylesheet"> connects a CSS file.
  • <body> holds everything visible.

Text between <!-- and --> is a comment: notes for humans that the browser ignores.

Step 7 of 13Practice · Sort into groups

Does each item belong in the head or the body?

Tap an item, then tap its group (or drag it there).

Step 8 of 13Diagram

The skeleton of a page as a tree

Every element has exactly one parent. Developer tools show this same tree in the Elements panel.

The skeleton of a page as a treeHierarchy diagram: html, head, body, meta charset, viewport, title, link to style.css, header with h1, main with paragraphs, footer.htmlheadbodymetacharset,viewporttitlelink tostyle.cssheaderwith h1main withparagraphsfooter

Tap a shape with to see more.

html
head

Information about the page; not shown in the page itself.

body

Everything visitors see.

meta charset, viewport
title

Shown in the browser tab and search results.

link to style.css
header with h1
main with paragraphs
footer

Step 9 of 13Note

Step 10 of 13Practice · Code challenge

Write a function makeElement(tag, text) that returns an HTML string with that tag wrapped around the text. For example, makeElement("p", "Hi") returns "<p>Hi</p>".

Runs in a sandbox in your browser.
  • makeElement("p", "Hi")
  • makeElement("h1", "Welcome")
  • makeElement("em", "now")

Step 11 of 13Tap to reveal

Useful extras in the head

  1. Meta description

    A one- or two-sentence summary of the page, <meta name="description" content="...">. Search engines often show it under your title in results, so it works like a short advertisement for the page.

  2. Favicon

    The small icon in the browser tab, added with <link rel="icon" href="favicon.svg">. It helps visitors find your tab among many.

  3. Language

    The lang attribute on the html element, such as lang="en" or lang="es", lets screen readers choose the right pronunciation and lets browsers offer translation.

  4. Social preview tags

    Optional meta tags that control the title, description and image shown when someone shares your link in a chat or social app.

Step 12 of 13Practice · Choose one

What does <!DOCTYPE html> at the top of a file do?

Step 13 of 13Practice · Fill in the blanks

Fill in the blanks

Elements without content or a closing tag, such as img and br, are called elements. The attribute that gives an image a text description is , and the attribute that sets a link's destination is .