HTML and CSS · Chapter 1 of 12

Your first web page

By the end of this chapter you can say what a web page is, write one from an empty file (the doctype, the <html> element with its language, the <head> with the charset and the title, and the <body>), mark up text as headings and paragraphs in the right outline, see your page in the Preview, and look at the tree of elements the browser builds from your file, which is often not quite what you wrote. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: HTML: the structure
  • 3 tasks
  • HTML and CSS
  • Free chapter

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.

Why you need this

It is March 2025. Sayali has spent a year signing up twelve of Pune's own venues for Parda, her new ticket company: five single-screen cinemas like Kala Talkies in Sadashiv Peth, which her grandfather ran, three small stages, a music hall, two clubs and an open-air lawn in Aundh. What Parda does not have is a website. People find out what is on from paper posters at the gates, or they phone Datta, who runs the box office: about 150 calls a day, and most of them ask the same three things. What is on? Where is it? When does the box office open?

Sayali wants Parda's first website live on Friday 4 April. Ira, the designer, has drawn every page, and she builds the site with you, by hand, in HTML and CSS: the home page, a page for each venue, a show page, a contact form, and a layout that works on a phone. You need nothing but a browser to start; this lab begins at the very first line.

Ira's first try was to type the home page's text into a file called index.html and open it in a browser. Every line is there, in order. This is what the browser showed, in the Preview:

Parda Tickets for Pune's own cinemas and stages Twelve venues, one website Parda sells tickets for twelve of Pune's own venues: ...

One long line. The browser cannot tell which words are Parda's name, which are a heading and which are a sentence, and it ignores the line breaks in a file: to a browser, any run of spaces and new lines is one space. To make a page, the text needs tags that say what each part of it is.

The lesson

A web page is a file

A web page is a plain text file, usually with a name that ends in .html. A browser fetches it from a web server (or opens it from your disk), reads it from the first character to the last, and draws what it describes. There is nothing to install and nothing to compile: the browser is the program, and HTML is the language it reads. The file for a site's front page is called index.html by custom, because that is the file a web server sends when someone asks for the site's address with no file name after it.

HTML stands for HyperText Markup Language. Markup means you keep the text and mark what each part of it is.

Tags and elements

Here is a piece of a venue page for Moti Mahal Talkies:

<h2>Moti Mahal Talkies</h2>
<p>Camp, Pune. A single screen since 1946, with 460 seats.</p>
  • <h2> is an opening tag: a name between angle brackets. </h2>, with a slash, is its closing tag. The two tags and everything between them make one element, here a heading.
  • <p> is a paragraph. The browser shows each element its own way: a heading big and bold, on its own line; a paragraph on its own line, with some space above and below.
  • An opening tag can carry attributes, a name, an equals sign and a value in quotes: <html lang="en"> says the page is in English.
  • Elements go inside each other, and close in the reverse order they opened: a link inside a paragraph is <p>... <a href="...">...</a></p>.
  • A few elements are empty: they hold nothing, so they have no closing tag. <meta> is one; you will meet <img> and <br> later.

The lab's terminal can show you what a browser makes of a file. lab tree reads the file with the same rules a browser uses and prints the tree of elements it builds, one element a line, with the elements inside it indented under it:

$ lab tree moti-mahal.html
(no <!DOCTYPE html> first: the browser shows this page in quirks mode)
<html>   (not in the file: added by the browser)
  <head>   (not in the file: added by the browser)
  <body>   (not in the file: added by the browser)
    <h2> "Moti Mahal Talkies"
    <p> "Camp, Pune. A single screen since 1946, with 460 seats."

The file had two elements; the browser built five. It added an <html>, a <head> and a <body> of its own, because every page has them, and it used quirks mode. Both are what the skeleton is for.

The skeleton every page has

Every page you write in this lab starts the same way. Here is Nilambari Cinema's page, whole:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Nilambari Cinema, Kothrud: Parda</title>
  </head>
  <body>
    <h1>Nilambari Cinema</h1>
    <p>Kothrud, Pune. A single screen since 1968, with 320 seats.</p>
  </body>
</html>
  • <!DOCTYPE html> is not an element: it tells the browser to use today's rules, the HTML standard. A page without it is shown in quirks mode, which copies the bugs of browsers from the 1990s so that very old pages still look as they did. It comes first, before any tag (a comment before it is fine).
  • <html lang="en"> holds the whole page. lang tells a screen reader which voice to read the page in, and tells a browser which language to offer to translate from.
  • <head> holds facts about the page, and none of it is shown on the page. <meta charset="utf-8"> says how the file's bytes turn into letters. Without it, a browser that is not told the encoding some other way guesses an old one. Here is Kala Talkies' price line in a page with no meta charset, as Chromium read it in the lab:

`` Front stalls from ₹120, balcony ₹240. ``

UTF-8 writes ₹ as three bytes, and the old encoding read them as three letters. (The Preview always reads your files as UTF-8, so it would not show this; a real server might.)

  • <title> is the page's name: the browser shows it on the tab, in bookmarks and in search results. It is not a heading on the page, and the Preview does not show it.
  • <body> holds everything the page shows.
  • One more line goes in the head of every page in this lab: <link rel="stylesheet" href="parda-base.css">. It links a file of styles, Ira's, that gives Parda's pages their look; chapter 6 teaches how it works. Like the title, it is not shown on the page.

The spaces at the start of the lines are for people: the browser ignores them. Indent each element two spaces more than the one it is in, and the file shows its own tree. lab tree nilambari.html now prints exactly what the file says, with nothing added:

$ lab tree nilambari.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title> "Nilambari Cinema, Kothrud: Parda"
  <body>
    <h1> "Nilambari Cinema"
    <p> "Kothrud, Pune. A single screen since 1968, with 320 seats."

Headings and paragraphs

HTML has six levels of heading, <h1> to <h6>, and the number is the heading's place in the outline of the page, like the chapters and sections of a book. Rangdeep Studio Theatre's page has its name, two parts, and two smaller parts inside the first. lab outline prints a page's headings the way a screen reader lists them, which is how many blind visitors move around a page:

$ lab outline rangdeep.html
h1 "Rangdeep Studio Theatre"
  h2 "Seats and prices"
    h3 "Stalls"
    h3 "Back"
  h2 "Getting there"

Three rules keep an outline useful:

  • One <h1> a page: its name.
  • Never skip a level: a part inside an <h2> is an <h3>. Take out the h2 and make the two seat classes h4s, and the outline has a hole:

`` $ lab outline rangdeep-skip.html h1 "Rangdeep Studio Theatre" h4 "Stalls" (after an h1: levels are skipped) h4 "Back" h2 "Getting there" ``

  • Choose a level for its place, never for its look. Browsers do show each level in a different size. In the lab's Chromium, with no styles of your own, they are 32, 24, 18.72, 16, 13.28 and 10.72 pixels, all bold, and a paragraph is 16:

`` $ lab style sizes.html "h1,h2,h3,h4,h5,h6,p" font-size ... <h6> "Level 6" font-size: 10.72px <p> "A paragraph" font-size: 16px ``

An <h6> is smaller than a paragraph, but it is still a heading: a screen reader still announces it as one, in the list of headings. Size is CSS's job, from chapter 6.

A paragraph, <p>, is one block of text, however many lines it takes in the file. Spaces and new lines inside it are squeezed to one space, so this:

<p>Chitra Mandir,      Pimpri.
   392 seats,
   since 1979.</p>

shows in the Preview as one line, Chitra Mandir, Pimpri. 392 seats, since 1979. Long paragraphs are wrapped across lines in the file only to keep the file easy to read.

The browser never says no

Write a page with a mistake in it and the browser shows no error. It repairs the file by rules that are written down in the HTML standard, the same in every browser, and shows what it built. Here a heading has lost its closing tag:

<h1>Moti Mahal Talkies
<p>Camp, Pune. A single screen since 1946, with 460 seats.</p>
<h2>Seats and prices</h2>
<p>Front ₹130, middle ₹200, balcony ₹260.</p>

In the Preview the paragraph under the name is as big and as bold as the name itself. lab tree shows why:

$ lab tree moti-mahal-broken.html
...
  <body>
    <h1>   (no </h1> of its own: the browser closed it)
      "Moti Mahal Talkies"
      <p> "Camp, Pune. A single screen since 1946, with 460 seats."
    <h2> "Seats and prices"
    <p> "Front ₹130, middle ₹200, balcony ₹260."

The browser kept the <h1> open and put the paragraph inside it, until the next heading started and the rules said to close it there. A validator, a program that checks a file against the standard, names the cause:

$ lab validate moti-mahal-broken.html
moti-mahal-broken.html line 8: Unclosed element '<h1>'
moti-mahal-broken.html line 9: <p> element is not permitted as content under <h1>
...

Read the first line and fix that; the lines after it often come from the first. One more rule is worth knowing early: a few elements hold only text, and <title> is one of them. After <title>, the browser reads everything as the title's text until it meets </title>, tags and all.

What the browser built, and how to see it

Every browser shows you both sides of a page:

  • View source (Ctrl+U, or Cmd+Option+U on a Mac) shows the file as it arrived, character for character.
  • The inspector (right-click the page, then Inspect) shows the tree the browser built, the DOM, after its repairs. When a page looks wrong, the tree tells you why; the source only tells you what you typed.

In this lab, cat index.html prints the file, lab tree index.html prints the tree, lab outline the headings and lab validate what a validator finds. lab help lists them all.

The Preview and the Check

The Preview beside the editor shows index.html from your saved files; save, and it shows the page again. A link to another of the chapter's pages opens that page in the Preview, and its header names the page you are on. A site is built in two layers, the structure in HTML and the look in CSS, and the first five chapters get the structure right. The look comes from Ira's parda-base.css, linked in each page's head: with it, your headings turn maroon on Parda's cream. It styles plain elements only, so the page looks right only when the structure is right. Chapter 6 teaches how a stylesheet works.

The Check opens your saved pages in a real browser in the lab, Chromium, and reads them the way a person does: the title, the headings, the paragraphs, the text. When a task fails, the first line says what it found and what the task asks for:

index.html has no doctype: put <!DOCTYPE html> before <html>, as the first tag in the file

Your turn

Three tasks. Task 1 gives index.html the skeleton, with Ira's lines in the body and the title in the head (careful: none of her text goes in the head). Task 2 marks up her lines as one h1, two h2s with three h3s between them, and a paragraph after each, keeping the link line at the end. Task 3 is an interview question: Datta's box-office.html is blank in the Preview, and fixing one problem shows the next. Run lab tree box-office.html before you change anything, and be ready for Ira's question: a browser never shows an error for broken HTML, so what does it do instead, and how do you see what it built? Watch the Preview as you save, and press Check when a task looks right.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    index.html: the page's skeleton

    index.html holds Ira's text for Parda's home page as plain lines, and the Preview runs them all into one line. Give the file the skeleton every web page has, in this order: the doctype, <!DOCTYPE html>, first (after the task comments is fine); then an <html> element with lang="en" that holds a <head> and then a <body>. The <head> holds a <meta> with charset="utf-8", Ira's stylesheet line, <link rel="stylesheet" href="parda-base.css"> (the first line under the comments: move it into the head), and a <title> that says exactly Parda: tickets for Pune's own cinemas and stages. The <body> holds everything the page shows: all of Ira's lines and the link line at the end.

    The head is not shown on the page: only the meta, the stylesheet line and the title go in it, and nothing of Ira's text. parda-base.css is a file of Ira's styles that gives the page Parda's look; chapter 6 teaches how it works. Letters, spaces and capitals must match exactly.

  2. Task 2

    index.html: headings and paragraphs

    Mark up Ira's lines inside the <body> of index.html so that each one is the element it should be, in this order: an h1 Parda; a p Tickets for Pune's own cinemas and stages; an h2 Twelve venues, one website; a p Parda sells tickets for twelve of Pune's own venues: five single-screen cinemas, three stages, a music hall, two clubs and an open-air lawn.; an h3 Cinemas; a p Kala Talkies, Nilambari Cinema, Moti Mahal Talkies, Chitra Mandir and Shalini Picture House.; an h3 Stages and halls; a p Rangdeep Studio Theatre, Natya Ghar, Kothrud Kala Kendra and Sur Sangam Hall.; an h3 Clubs and lawns; a p The Attic, Kattaa Cafe Stage and Sahyadri Lawns.; an h2 Write to us; a p Questions about a show or a seat? Write to help@parda.example.

    Then keep the link line, <p><a href="box-office.html">Box office hours</a></p>, as it is, the last thing in the body. The page has exactly these six headings: one h1, and every h3 sits under an h2, no level skipped. A paragraph may take several lines in the file; a line break in the file is not a line break on the page. Letters, spaces and full stops must match exactly.

  3. Task 3 · interview question

    Interview: box-office.html, the page that shows nothing

    Datta's box-office.html shows a blank page in the Preview (open it from the link at the end of index.html). Fix the file so that its title is Box office hours: Parda and it shows exactly, in this order: an h1 Box office hours; a p Every Parda box office opens an hour before the first show. Pay at the window with cash or UPI.; an h2 Kala Talkies, Sadashiv Peth; a p Open 11 am to 10 pm, every day.; an h2 Rangdeep Studio Theatre, Erandwane; a p Open 4 pm to 9 pm. Closed on Mondays.; an h2 Sahyadri Lawns, Aundh; a p Open from 5 pm on show days only.; and last a p Tickets bought at a box office can be changed for another show up to an hour before it starts.

    Exactly these four headings: a heading says what a part of the page is about, and is never chosen for how big or small its text looks. Keep the skeleton (the doctype, html lang="en", the head with the meta charset and the title, the body).

    Ira will ask: a browser never shows an error for broken HTML. What does it do instead, and how do you see what it built from your file?

What you will learn

  • A web page: a text file that the browser reads from the top and turns into a page.
  • Tags and elements: <p> and </p> around text make a paragraph; tags hold attributes and other elements.
  • The skeleton: the doctype, <html lang="en">, a <head> with <meta charset="utf-8"> and a <title>, and a <body>.
  • Headings and paragraphs: <h1> to <h6> as the outline of the page, <p> for its paragraphs.
  • What the browser built: it repairs broken HTML without a word; view source, the inspector and lab tree show you the file and the tree.
  • The Preview and the Check: your page beside the editor, and the tests that read it like a person.

More in HTML: the structure

Build "Your first web page" yourself

Parda is launching its first website for Pune's own cinemas and stages. You build it, page by page. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.