The Structure of an HTML Document

First lesson of the HTML Fundamentals course, free to read: about 15 minutes, 3 exercises and 3 quiz questions. This is the catalogue's base lesson; in the student space, it is rewritten for your profile.

Objectives

  • Write the minimal skeleton of a valid HTML page
  • Distinguish the roles of <head> and <body>
  • Understand the notions of tag, element and attribute

Skeleton of an HTML documentTags, elements and attributes

HTML (HyperText Markup Language) is the language that describes the content and meaning of a web page: this is a heading, this is a paragraph, this is a link. Appearance (colors, layout) comes later with CSS, and interactions with JavaScript.

Tags, elements and attributes

An HTML element is generally written with an opening tag, content and a closing tag:

<!-- opening tag, content, closing tag -->
<p>Hello everyone</p>

<!-- an attribute refines the element: name="value" -->
<a href="contact.html">Contact us</a>

Some elements are empty: they have neither content nor a closing tag, for example <meta>, <img>, <br> or <input>.

Elements nest like Russian dolls: you always close first what you opened last.

<p>A word <strong>important</strong> here.</p>   <!-- correct -->
<p>A word <strong>important</p></strong>        <!-- incorrect -->

The minimal skeleton

Here is the basis of every modern page:

<!DOCTYPE html>                 <!-- the browser's standards mode -->
<html lang="fr">                <!-- main language of the page -->
  <head>
    <meta charset="UTF-8">      <!-- encoding: accents display correctly -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>My first site</title>  <!-- text of the tab and of search engines -->
  </head>
  <body>
    <h1>Welcome</h1>
    <p>This is my first page.</p>
  </body>
</html>

Line by line:

  • <!DOCTYPE html> is not a tag but a declaration: it tells the browser to use standards mode.
  • <html lang="fr"> is the root of the document. The lang attribute helps screen readers pick the right pronunciation and search engines classify the page.
  • <head> groups the metadata, invisible in the page: encoding, mobile display setting (viewport), tab title, and later the links to CSS stylesheets.
  • <body> contains everything the user sees.

The document tree

The browser turns this text into a tree (the DOM): html has two children, head and body, which themselves have children. Keeping this picture in mind will help you a lot with CSS and JavaScript.

Good habits

  • Indent your code: each level of nesting is shifted by two spaces.
  • Write tags and attributes in lowercase and put quotes around values.
  • Add comments with <!-- ... -->; they are not displayed.
  • Save the file as UTF-8 with the .html extension, then open it in your browser.

HTML is forgiving: an error does not crash the page, the browser guesses. That is handy, but it hides errors. Get into the habit of writing clean code from the start.

Exercises

Solutions and tests stay on the server. In the student space, your code is run and checked, and the tutor gives hints before the solution.

  1. CodeDifficulty 1HTML page in the browser2 hints

    Complete the skeleton

    Complete the page: add the lang="fr" attribute to <html>, a <meta charset="UTF-8"> tag and a <title> containing Ma page.

    <!DOCTYPE html>
    <html>
      <head>
        <!-- ajoutez l'encodage et le titre -->
      </head>
      <body>
        <p>Bonjour</p>
      </body>
    </html>
  2. CodeDifficulty 2HTML page in the browser2 hints

    A complete page

    Write a complete page from scratch: doctype, <html lang="fr">, <head> with UTF-8 charset, meta viewport and the title Portfolio, then a <body> containing an <h1> “Bonjour, je suis Alex” and an introductory paragraph.

    <!-- écrivez toute la page ici -->
  3. Multiple choiceDifficulty 11 hint

    Where does the tab title go?

    In which element should the <title> tag be placed?

    • In <body>
    • In <head>
    • Right after <!DOCTYPE html>, outside <html>

End-of-lesson quiz

  1. What is for?

    • Displaying the title
    • Activating the browser's standards mode
    • Importing CSS
  2. Which of these elements is an empty element (no closing tag)?

    • <p>
    • <title>
    • <meta>
  3. Why add lang=“fr” to ?

    • To translate the page automatically
    • To help screen readers and search engines know the language
    • It is mandatory for the page to display

The rest of the course

  1. Headings, Paragraphs and Lists15 min
  2. Links and Images15 min
  3. Structuring a Page with Semantic HTML15 min
  4. Data Tables15 min
  5. Forms20 min

HTML Fundamentals · Beginner · 3 h 5 min