{module 2}
Your first page
How the web works, and your tools20 min read
What you will be able to do
- Write a page by hand with headings and paragraphs, each element opened and closed.
- Change a page one step at a time, saving and reloading after each change.
- Say what the browser forgives in your HTML, and find the tag that broke a page when it does not.
Why it matters
Mama Asha's Kitchen is a food stall in Mombasa, Kenya. Asha Bakari, the owner, asked her nephew Juma for a page with the day's menu. He typed it in an evening. When he opened it, every dish and the opening hours were huge and bold, as if the whole page were one title.
Juma changed five things at once: he moved lines, retyped two dishes and deleted a sentence. The page came right, and he did not know why. The next week it broke again in the same way, and he started from nothing.
The cause was one missing character in one tag. This lesson shows you how a page is built from elements, and how to change it in a way that tells you which change did what.
The idea
HTML is plain text with instructions
In lesson 2 you saw that the browser hides <h1> and </h1> and shows the words between them as a heading. That is all HTML is: the text of your page, with short instructions around each piece that say what the piece is.
An element has three parts
Here is one line from Asha's menu:
<p>Pilau with kachumbari, $4</p><p>is the opening tag. A tag is a name between angle brackets.pstands for paragraph.Pilau with kachumbari, $4is the content: the words the visitor reads.</p>is the closing tag. It is the same name with a slash before it, and it says where the paragraph ends.
Together, the opening tag, the content and the closing tag make one element. The tags themselves never appear on the page.
Three elements to start with
h1is the main heading of the page: its title. Use one.h2is a heading for one part of the page, such as "Today's menu". It is smaller thanh1.pis a paragraph: ordinary text.
The browser gives each one a look of its own. Headings are large and bold. Each heading and paragraph starts on a new line, with some space around it. Module 2 adds lists, links and pictures.
Line breaks in your file do not count
The browser decides where lines break. A line break or a run of spaces in your file becomes a single space on the page. Here is a paragraph typed on three lines:
<p>Pilau $4
Chapati $3
Mandazi $1</p>Pilau $4 Chapati $3 Mandazi $1One line, single spaces. If you want separate lines, use separate elements.
What the browser forgives
The browser never refuses to show a page. When your HTML has a mistake, it guesses what you meant and shows something. Some guesses are always right:
- A missing
</p>. A new<p>or heading tells the browser the last paragraph has ended, so it closes it for you. - Capital letters in tags.
<P>is read as<p>. - No skeleton. A full page also has a few lines at the top that describe the page itself. The browser adds what is missing. Your pages work without them for now, and module 2 adds them in its first lesson.
Here are three paragraphs with no closing tags at all:
<h2>Today's menu</h2>
<p>Pilau with kachumbari, $4
<p>Chapati and bean stew, $3
<p>Mandazi, three for $1The browser counts three paragraphs and shows each one correctly:
3
Pilau with kachumbari, $4
Chapati and bean stew, $3
Mandazi, three for $1Other guesses are wrong, and then the page looks wrong from a certain point onwards. You will see two in "Common mistakes". Forgiveness is not a reason to be careless. Close every element you open, and the browser never has to guess.
Worked example
You will build Asha's menu one step at a time, and check it after each step.
In VS Code, open your free-track folder. Click New File... in the Explorer, name the file menu.html and press Enter. Type only the first line:
<h1>Mama Asha's Kitchen</h1>Save with Ctrl+S (Cmd+S on a Mac). Open the file in your browser, as in lesson 2. You see one large, bold line:
Mama Asha's KitchenIt works, so add the next piece. Keep the browser open. After each step below, save, switch to the browser and reload with F5 (Cmd+R on a Mac).
- Add the paragraph about the stall under the heading. Save, reload, check.
- Add the
h2for the menu and the three dishes, one paragraph each. Save, reload, check. - Add the
h2for the opening hours and its paragraph. Save, reload, check.
When you have finished, the file is this:
<h1>Mama Asha's Kitchen</h1>
<p>Home cooking from our stall in the Old Town, Mombasa.</p>
<h2>Today's menu</h2>
<p>Pilau with kachumbari, $4</p>
<p>Chapati and bean stew, $3</p>
<p>Mandazi, three for $1</p>
<h2>Opening hours</h2>
<p>Monday to Saturday, 7:00 to 15:00.</p>And this is the text the browser shows:
Mama Asha's Kitchen
Home cooking from our stall in the Old Town, Mombasa.
Today's menu
Pilau with kachumbari, $4
Chapati and bean stew, $3
Mandazi, three for $1
Opening hours
Monday to Saturday, 7:00 to 15:00.On screen, the first line is the largest. "Today's menu" and "Opening hours" are bold and a little smaller. The rest is ordinary text. Count the elements and you get one h1, two h2 and five p:
1
2
5Each line of the file is one element, with its opening tag at the start and its closing tag at the end. When you read a page you wrote, check that every line looks like that.
Common mistakes
A closing tag without its slash
<h1>Mama Asha's Kitchen<h1>
<p>Home cooking from our stall in the Old Town, Mombasa.</p>
<p>Pilau with kachumbari, $4</p>This is Juma's mistake. The end of the first line should be </h1>. The page shows everything as a heading. Here is how many h1 elements the browser made, then the text inside them:
2
Mama Asha's Kitchen
Home cooking from our stall in the Old Town, Mombasa.
Pilau with kachumbari, $4The file has one heading, and the browser made two. It read the second <h1> as the start of a new heading. That heading never closes, so both paragraphs ended up inside it. When a page looks wrong from a certain line onwards, look at the tag just before that line.
The letter l in place of the number 1
<hl>Mama Asha's Kitchen</hl>
<p>Home cooking from our stall in the Old Town, Mombasa.</p>0
Mama Asha's Kitchen
Home cooking from our stall in the Old Town, Mombasa.The browser found no h1 at all. hl is not a name it knows, so it shows the words as ordinary text, the same size as the paragraph. In many fonts the letter l and the number 1 look almost the same. If a heading is not a heading, read its tag one character at a time.
Think it through
Mei, a learner, has a working copy of Asha's menu. Asha wants a new dish, "Samaki wa kupaka (fish in coconut sauce), $6", added after the chapati, and the hours changed to end at 16:00.
Understand. Two changes: one new paragraph in the menu, and new text in an existing paragraph. Everything else must stay as it is.
Break down. Change one: the new dish. Change two: the hours. Each can be done and checked alone.
Plan. Make the first change, save, reload, check. Then the second, save, reload, check. Never both at once.
Build. She copies the chapati line, pastes it below, and changes the words and the price. She saves and reloads.
Test. The new dish appears in the right place, the same size as the others. Then she changes 15:00 to 16:00, saves and reloads. The hours change and nothing else moves. As an odd case, she deletes the / from the new line's closing tag on purpose. The opening hours suddenly join the dish. She knows exactly which change did it, puts the slash back, and the page is right again.
Reflect. Copying a line that works, then changing only the words, means the tags are already correct. One change between reloads means a broken page always points to one line.
At work
A good question also says what you changed. When a page breaks after you worked on it, the most useful fact you can give is which change broke it. You can only know that if you changed one thing at a time.
Compare two messages Juma could have sent to a friend who knows HTML:
The menu page is broken again. Can you look?
The menu page worked at 18:00. I made three changes, reloading after each. After the first two, it still looked right. After the third, adding a heading for the drinks, everything below that heading became big and bold. Here is the line I added.
The second message tells the friend where to look, so the answer takes a minute. It also shows the friend that Juma tested before he asked.
Your tasks
Task 1: Build the menu
Make menu.html as in the worked example, one step at a time, reloading after each step.
You are done when your page shows the same text as the worked example, with one large heading and two smaller ones.
Task 2: Your own stall
Make a new file for a small business you know, or one you invent: its name as the h1, one paragraph about it, an h2 with three things it sells and their prices in dollars, and an h2 with its hours.
You are done when every element in your file has an opening and a closing tag, and the page shows one h1 and two h2.
Task 3: Break it on purpose
In a copy of your page, make each of these mistakes, one at a time. Before each reload, write down what you expect to see. Remove the slash from the closing tag of your first h2. Then, after repairing it, change the 1 in your h1 tags to the letter l.
You are done when you have written a prediction and the real result for each mistake, and repaired both.
Task 4: Find the change
Ask someone to make one small change to a copy of your page while you look away, such as deleting one character from one tag. Find it by reading the page in the browser and then the file.
You are done when you have found the changed line, and can say which line of the page first looked wrong and why.
Recap
- An element is an opening tag, its content and a closing tag with a slash:
<p>...</p>. h1is the title of the page,h2a heading for one part, andpa paragraph.- Line breaks and extra spaces in your file become one space. Use separate elements for separate lines.
- The browser forgives a missing
</p>and a missing skeleton. It cannot guess a missing slash in a heading or a misspelled tag. - Change one thing, save, reload, check. Then a broken page points to one change.
Go deeper
- MDN: Basic HTML syntax explains elements, opening and closing tags, and the parts of a page this lesson left for module 2.
- MDN: Headings and paragraphs shows the six levels of heading and when to use each.
- MDN: The p element is the reference page for the paragraph, including when its closing tag may be left out.