← Structure and style a page

Structure and style a page: recap

Answer each question in your head or on paper first, then check it.

Text and style

1. Tags label text

Try firstA student presses Enter in the middle of a paragraph in their code, but the page does not change. Why, and how do they start a new line?

Check your answer

The browser ignores line breaks in the code. To start a new paragraph, end the first with </p> and start the next with <p>.

The video and the picture show the same steps:

  1. A tag is a label. <h1> labels the main heading.
  2. <p> opens a paragraph and </p> closes it.
  3. Two lines of text in the code show as one line on the page: the browser ignores line breaks in the code.
  4. A new paragraph needs its own <p>.

In the picture, Boxes is on: each box is a tag, and the paragraphs are separate boxes.

The page this code makes, with Boxes on
What this code makes (with Boxes on: each box is a tag)
Tags label text, and line breaks in the code do not show · Sound on for the voiceover
Open the lesson →

2. Tags inside tags

Try firstA student writes <strong>Friday <em>night</strong></em>. What is wrong, and what will the browser say?

Check your answer

The tags cross: <em> was opened last so </em> must come first. The browser says nothing; it makes a best guess and the page may look wrong.

The video and the picture show the same steps:

  1. <strong> marks important words and the browser makes them bold.
  2. <em> goes inside it and marks a stressed word, slanted.
  3. The tag opened last closes first: </em>, then </strong>, then </p>.
  4. If a tag is missing or crosses, the browser shows no error. It makes its best guess.

In the picture, Boxes is on: <em> is a box inside the <strong> box, inside the <p> box. A comment, <!-- a note -->, is ignored by the browser.

The page this code makes, with Boxes on
What this code makes (with Boxes on: each box is a tag)
A tag can go inside another tag, and the last one opened closes first · Sound on for the voiceover
Open the lesson →

3. Rules and selectors

Try firstA rule h2 { color: purple; } is added to a page with four h2 headings. How many turn purple, and is any <h2> changed into something else?

Check your answer

All four turn purple, because the selector names the tag. None changes into something else: the tag is still h2, and the rule only changes how it looks.

The video and the picture show the same steps:

  1. CSS rules go in a <style> block, at the top of the code for now.
  2. A rule starts with a selector: p picks every paragraph, and nothing else.
  3. Inside the braces: a property, a colon, a value, a semicolon, as in color: teal;.
  4. Both paragraphs turn teal. The heading is not a p, so it stays as it was.
  5. A second rule can style the heading. The HTML never changed.

Rules change how parts look. The tags still say what each part is.

The page this code makes
What this code makes
A rule names a tag, then sets how every one of them looks · Sound on for the voiceover
Open the lesson →

Lists, links and pictures

4. Lists

Try firstA student types 1. 2. 3. at the start of each item in an <ol>. What does the page show, and what should they do?

Check your answer

Each item shows two numbers, the one the browser added and the one typed. They should delete the typed numbers: the <ol> counts for them.

The video and the picture show the same steps:

  1. <ul> opens a box for a list.
  2. Each <li> is one item, inside the list.
  3. </ul> closes the list.
  4. <ol> makes a numbered list. The browser does the counting, so you never type the numbers.
  5. An item is a box too, so a whole list can go inside an <li>.

Every <li> must be inside a <ul> or an <ol>. In the picture, Boxes is on: you can see each item inside its list.

The page this code makes, with Boxes on
What this code makes (with Boxes on: each box is a tag)
A list is a box of items, and an item can hold another list · Sound on for the voiceover
Open the lesson →

6. Pictures

Try firstA student's picture does not show, only a few words. What do they check first, and what are the words?

Check your answer

They check that the file name in src matches the file exactly. The words are the alt text, which shows when the picture cannot load.

The video and the picture show the same steps:

  1. <img> shows a picture and has no closing tag.
  2. src names the picture file, and the name must match exactly.
  3. alt says what the picture shows. With a wrong src, people see the alt words instead.
The page this code makes
What this code makes
src names the picture file, and alt describes it · Sound on for the voiceover
Open the lesson →

7. Pick some with a class

Try firstA rule .sale { color: red; } does nothing. Name two things that could be wrong.

Check your answer

No element has class="sale", or the name is spelt differently in the HTML. Another possibility is class=".sale" with the dot in the HTML.

The video and the picture show the same steps:

  1. class="note" marks an element with a name you choose.
  2. .note { } in the CSS styles only the elements with that class.
  3. Any number of elements can share one class, and the rule is written once.

In the picture, the two marked paragraphs are crimson and the others are not.

The page this code makes
What this code makes
A class marks the elements you choose, and a dot rule styles only those · Sound on for the voiceover
Open the lesson →

8. Fix the newsletter

Try firstA student adds a heading to the head of the page, and nothing shows. Why?

Check your answer

Only what is inside the body shows on the page. The head holds the title and the rules.

Open the lesson →

Parts of a page

9. Tables

Try firstA table has 3 columns and 4 rows of data under the heading row. How many <td> cells does it need, and why?

Check your answer

12 cells: every one of the 4 rows needs a cell for each of the 3 columns (4 × 3).

The video and the picture show the same steps:

  1. <table> opens the whole table.
  2. <tr> is one row. In the first row, each <th> is a heading cell.
  3. The next row holds ordinary cells: <td>.
  4. </table> closes the table.
  5. Columns appear by themselves, so every row needs the same number of cells.

In the picture, Boxes is on: the table holds two rows, and each row holds cells.

The page this code makes, with Boxes on
What this code makes (with Boxes on: each box is a tag)
A table is rows, and each row is cells · Sound on for the voiceover
Open the lesson →

10. Name the parts of a page

Try firstA student wraps a menu in <nav> and nothing changes on screen. Did it work? Why?

Check your answer

Yes. nav looks like no tag at all, but it tells browsers and screen readers that the part is the main menu, so people can jump to it.

The video and the picture show the same steps (Boxes is on, so each named part shows as a box):

  1. <header> is the top of the page: the title, and often the menu (<nav>).
  2. <main> holds the page's main content. <section> is one part of it, with its own heading.
  3. <footer> is the bottom: contact details and small print.
  4. They look like plain text. Their names tell browsers and screen readers what each part is.
The page this code makes, with Boxes on
What this code makes (with Boxes on: each box is a tag)
Section tags name the parts of a page · Sound on for the voiceover
Open the lesson →

12. Forms

Try firstA sign-up form has a label Email and a box, but clicking Email does nothing. Give the likely cause and the fix.

Check your answer

The label's for does not match the box's id (or is missing). Make them the same, for example for="email" and id="email".

The video and the picture show the same steps:

  1. A <form> holds the boxes people type in.
  2. A <p> puts a label and its box on their own line.
  3. The label's for matches the box's id, so clicking the label finds the box.
  4. A <button> is for sending the answers...
  5. ...but nothing is connected to it. Code behind a form does the work.

A <select> with <option> items makes a drop-down list.

The page this code makes, with Boxes on
What this code makes (with Boxes on: each box is a tag)
A label links to its box by id, and a form does nothing alone · Sound on for the voiceover
Open the lesson →

Final challenge: a club page

13. Challenge: a club page

Try firstName three parts of your page and the tag, attribute or rule behind each.

Check your answer

For example: the menu is a nav list of links, and each href matches a section's id; the gold winners are rows with class winner and a .winner rule; the sign-up labels work because each for matches a box's id.

The picture shows the finished page. Build it in this order, one milestone at a time:

  1. Header and menu: a <header> with a <nav> list of links to #ids, coloured by tag rules.
  2. Sections: a list, a picture and a results table, with the winners marked by a class.
  3. Sign-up and footer: a <form> with labels linked to their boxes, and a <footer>.

Check the page after every change.

The page this code makes
What this code makes
Open the lesson →