Lesson 1 of 5 · 5 min

Your first HTML document

Elements, tags and attributes, the boilerplate every page needs, and what each line of it does.

On this page
  1. The boilerplate
  2. Comments
  3. Whitespace
  4. Developer tools
  5. Try it

HTML is made of elements. Most elements have an opening tag, content, and a closing tag:

<p>This is a paragraph.</p>

Some elements are empty and have no closing tag:

<img src="photo.jpg" alt="A cup of coffee on a desk">
<br>

Attributes add information to an element. They go in the opening tag as name="value". Above, src and alt are attributes of img.

Elements nest inside each other, and must close in the reverse order they opened:

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

The boilerplate

Every page starts with the same structure:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>My first page</title>
    <meta name="description" content="A short summary for search results.">
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <h1>Hello, world</h1>
    <p>This is my first web page.</p>
  </body>
</html>

Line by line:

  • <!doctype html> tells the browser to use modern standards mode.
  • <html lang="en"> is the root element. lang helps screen readers pronounce words correctly and lets the browser offer translation.
  • <head> holds information about the page. None of it is shown in the page itself.
  • <meta charset="utf-8"> lets the page display any character, from é to ₹ to emoji.
  • The viewport meta tag makes the page fit phone screens instead of showing a shrunken desktop page.
  • <title> is the text in the browser tab and the headline in search results.
  • <meta name="description"> is often used as the snippet under that headline.
  • <body> holds everything visible.

Comments

<!-- This note is for developers. Browsers ignore it. -->

Comments are visible to anyone who views the page source, so never put passwords or private notes in them.

Whitespace

Browsers collapse runs of spaces and line breaks into a single space. Indent your HTML freely to make it readable; it won't change the page.

Developer tools

Right-click any element on any website and choose Inspect. The Elements panel shows the live HTML, and you can edit it to experiment. It's one of the fastest ways to learn how other sites are built.

Try it

Create index.html with the boilerplate above. Change the title and heading to your name, add two paragraphs about yourself, and open the file in your browser. Then view the page source (Ctrl+U or Cmd+Option+U) to see exactly what the browser received.

More from HTML Crash Course