Web Foundations Lesson 1 of 7

HTML basics: tags and elements

25 min, 4 exercises

Make the title

Make Pizza night the big title. On line 1, change both p to h1, then press Run.

A whole pizza with pepperoni, mushrooms, olives and basilA whole pizza with pepperoni, mushrooms, olives and basil

The words in angle brackets are tags. They tell the browser what each piece is.

Learn

Opening and closing tags

Every web page you have ever scrolled past is, underneath, a text file written in HTML. HTML does not make things pretty and it does not make things move. Its one job is to say what each piece of content is: this is a heading, this is a paragraph, this is a list.

Look at your first line. Most tags come in pairs, an opening tag and a closing tag with a slash: <h1> opens the heading and </h1> closes it.

The opening tag, the content and the closing tag together make an element.

Learn

Headings, paragraphs and lists

Some useful elements:

  • <h1> to <h6>: headings, from biggest to smallest
  • <p>: a paragraph
  • <ul> with <li> inside: a bulleted list, one <li> per item
  • <strong>: words that matter

Browsers show <strong> in bold, and screen readers can say it with stress. <b> also makes text bold, but it says nothing about meaning, so use <strong> when the words are important.

Elements can live inside other elements. That is called nesting, and it is how a page gets its structure: each <li> here sits inside the <ul>.

index.html to run
<h1>Pizza night</h1>
<p>Bring <strong>one</strong> topping each.</p>
<ul>
  <li>Cheese</li>
  <li>Mushrooms</li>
  <li>Peppers</li>
</ul>
<h2>When</h2>
<p>Saturday at 7.</p>

Learn

Closing tags in order

The rule: close things in the reverse order you opened them, like stacking boxes. <p><strong>Hi</strong></p> is fine. <p><strong>Hi</p></strong> is a mess, and browsers will quietly guess what you meant (often wrongly).

Indenting nested elements is optional for the browser but very much not optional for your sanity. Future you, debugging at 1am, will thank you.

Try it in the editor: change the text, break a closing tag on purpose and see what happens. That is how this sticks.

Look it upWeb cheatsheet

Practice

Add a heading and a paragraph

Add an <h1> heading with the exact text My Playlist, and below it a <p> paragraph with any text you like.

exercise_1.html to run
<!-- Write your h1 and p below -->
Songs I can't stop playing

Practice

Make a list of three songs

The list below has only one song. Make it a <ul> with exactly three <li> items (any song names you like).

exercise_2.html to run
<h2>Top songs</h2>
<ul>
  <li>Song one</li>
</ul>

Break/fixFix the bug

The paragraph comes out huge

A stack of pancakes with butter and syrupA stack of pancakes with butter and syrup

The recipe's first line should be the only big heading, with a normal paragraph under it. Instead the paragraph comes out huge and bold. Find out why and fix it.

exercise_3.html to run
<h1>Pancakes<h1>
<p>Serves 4. Takes 20 minutes.</p>
<ul>
  <li>2 eggs</li>
  <li>100 g flour</li>
</ul>

Your turn

Write a game night page

A game controller, playing cards and a dieA game controller, playing cards and a die

Write a page for a game night from nothing. It needs:

  • one h1 that says Game night
  • a paragraph that has the word Friday inside a strong
  • an h2 that says Games, followed by a list of three games
  • an h2 that says Snacks, followed by a list of two snacks

On your own. Hints open after your first check.

exercise_4.html to run
index.html to run
<p>Pizza night</p>
<ul>
  <li>Saturday at 7</li>
  <li>Bring a friend</li>
</ul>

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.