{module 3}
The skeleton of an HTML page
Your first page in HTML20 min read
What you will be able to do
- Write the skeleton of an HTML page from memory: the doctype,
html,headandbody. - Explain what each line of the
headis for: the character set, the viewport line and the title. - Fix a page whose tab shows the wrong name, or whose content has disappeared.
Why it matters
Sarajevo Coffee Roasters roasts and sells coffee beans in Sarajevo, Bosnia and Herzegovina. Its owner, Emir Hodžić, wrote the shop's first page himself, with a heading and three paragraphs, the way you did in module 1. On his laptop it looked right.
Then customers told him two things. When they saved the page, it was saved as "index.html", which told them nothing. And on a phone, the text was so small that they had to zoom in to read the price.
Nothing in his heading or his paragraphs was wrong. What was missing were a few lines at the top of the file that every page needs. This lesson adds them, one at a time, and says what each one does.
The idea
What the browser adds for you
In module 1 you wrote a page with only a heading and a paragraph. Here is a page like that:
<h1>Sarajevo Coffee Roasters</h1>
<p>We roast coffee beans every morning.</p>Open it and look in the Elements panel of the developer tools. You see three elements you did not write: html, head and body. Here is how many of each the browser made:
1
1
1The browser needs these three elements, so when they are missing, it adds them. But it cannot guess what you would have put inside them. The page has no name, so the tab shows the file name. The browser does not know which language the page is in. On a phone it does not know how wide to make the page. The lines you are about to learn tell it.
Attributes: extra information inside a tag
Some of these lines use an attribute. An attribute is extra information about an element, written inside its opening tag. It has a name, an equals sign, and a value in double quotes:
<html lang="en">Here the element is html, the attribute's name is lang, and its value is en.
The skeleton, line by line
Every page in this course starts with the same lines. They are called the skeleton, because the rest of the page hangs on them.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Sarajevo Coffee Roasters</title>
</head>
<body><!doctype html> is always the first line. It is not an element. It tells the browser to follow the current rules of HTML, and not the older rules that some very old pages were written for.
html holds the whole page. Every other element goes inside it. Its lang attribute says which language the page is written in: en for English, bs for Bosnian, fr for French. A screen reader, the software that reads a page aloud for a person who cannot see it, uses lang to choose how to pronounce the words.
head holds information about the page. Nothing inside it is shown in the page itself.
<meta charset="utf-8" /> says how the letters in the file are stored. UTF-8 covers the letters of almost every language, so "Baščaršija" shows as it should. It is the only value HTML allows. A meta element has no content and no closing tag. The slash before its > shows that nothing is missing. It is optional, and this course always writes it.
<meta name="viewport" ... /> is the viewport line. The viewport is the part of the window where the page is drawn. Without this line, a phone pretends to be a wide screen, draws the page at that width, then shrinks the whole picture to fit. The text becomes tiny. width=device-width tells the phone to draw the page as wide as its own screen, and initial-scale=1 tells it to start at the normal zoom. This is what was wrong on Emir's page.
title is the name of the page. The browser shows it on the tab, and uses it when a visitor saves the page. Search engines show it as the heading of a result. This is the other thing Emir's page was missing.
body holds everything a visitor sees: the headings, the paragraphs, and all you will add in this module. After the content come </body> and </html>, which close the two elements.
Indenting shows what is inside what
head and body are inside html, so they start two spaces further right. The lines inside head start two spaces further again. The browser ignores these spaces. They show you what is inside what.
Worked example
Open your course folder in Visual Studio Code, the way you did in module 1. Make a new folder inside it called sarajevo-coffee, and in that folder a new file called index.html. Type the whole page below. Type it rather than paste it, so that you learn the skeleton.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Sarajevo Coffee Roasters</title>
</head>
<body>
<h1>Sarajevo Coffee Roasters</h1>
<p>We roast coffee beans every morning in Baščaršija, the old market of Sarajevo.</p>
<p>A bag of 250 grams costs $9.</p>
<p>Open Monday to Saturday, 8:00 to 16:00.</p>
</body>
</html>Save the file with Ctrl+S (Cmd+S on a Mac). Then open it in your browser: double-click the file in your file manager, or press Ctrl+O in the browser (Cmd+O on a Mac) and choose the file. This is the text the browser shows:
Sarajevo Coffee Roasters
We roast coffee beans every morning in Baščaršija, the old market of Sarajevo.
A bag of 250 grams costs $9.
Open Monday to Saturday, 8:00 to 16:00.Nothing from the head is in the page. Now look at the tab. It shows the title:
Sarajevo Coffee RoastersThe language and the viewport line are not shown anywhere on the screen, but the browser has read them. This is what it found in the two attributes:
en
width=device-width, initial-scale=1Open the Elements panel (right-click the page and choose Inspect). This time, the html, head and body elements are the ones you wrote, and the head holds your three lines.
Common mistakes
A mistake in the skeleton rarely produces an error message. You notice it by looking at the tab and at what is missing from the page.
A title that is never closed
<title>Sarajevo Coffee Roasters
</head>The closing </title> is missing. Open this page and it is empty: no heading, no price. Look at the tab instead. This is the title the browser made:
Sarajevo Coffee Roasters </head> <body> <h1>Sarajevo Coffee Roasters</h1> <p>A bag of 250 grams costs $9.</p> </body> </html>The browser treats everything after <title> as plain text, until it finds </title>. It never finds one, so the rest of the file became the title, tags and all. Here is how many headings and paragraphs are left in the page:
0
0When a whole page disappears, look at the tab. Then check that </title> is there.
The page's name written in the head
head sounds like "the top of the page", so it is easy to put the main heading there:
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<h1>Sarajevo Coffee Roasters</h1>
</head>The page looks right, because the browser moves the heading. Here is how many h1 elements are in the head, then in the body:
0
1But the page has no title, so the tab shows the file name, as on Emir's first page. The head is not the top of the page: it is information about the page. Put the h1 in the body, and the name of the page in a title.
Think it through
Nadia is building a second page for the coffee shop, for visitors who read Bosnian. It has the same three paragraphs, translated. She wants to know which lines of the skeleton must change.
Understand. In: the English page. Out: a Bosnian page that the browser, a screen reader and a search engine all treat as Bosnian.
Break down. She goes through the skeleton one line at a time and asks of each: does this depend on the language?
Plan. The doctype, head and body do not change. The character set stays utf-8, because it covers Bosnian letters. The viewport line is about the screen, not the language. lang must change, and so must the title, which a visitor reads.
Build. She copies index.html to a new file, bs.html, changes lang="en" to lang="bs", translates the title to "Sarajevo pržionica kafe", and translates the paragraphs.
Test. She opens the page and reads the tab. Then the odd case: she opens both pages in two tabs, side by side. Each tab has its own name, so a visitor can tell them apart.
Reflect. Most of the skeleton is the same on every page. Only two lines describe this page in particular: lang and title. Those are the two to check each time she copies a page.
At work
Clients often ask for a change when they mean a problem. Emir wrote: "Please make the writing on the page bigger." A developer who did exactly that would make the text bigger on every screen, and the page would still be hard to use on a phone. A careful developer asks about the problem behind the request first:
Before I change anything, two questions. Where did you see the writing too small: on a phone, on a laptop, or both? And what were you or your customer trying to read at the time?
Emir answers: "On my phone. I was showing a customer the price." Now the cause is clear. The page has no viewport line, so the phone shrinks it. One line in the head fixes it. The question took one minute, and it found the real need: customers must be able to read the price on a phone.
Your tasks
Task 1: Type the skeleton
Build the page from the worked example in sarajevo-coffee/index.html. Change the price and the opening hours to ones you invent. Save and open it in your browser.
You are done when the tab shows "Sarajevo Coffee Roasters", the page shows your price and hours, and the Elements panel shows your three lines inside head.
Task 2: Lose the page and find it again
Delete </title> from your file. Before you reload, write down what you expect to see. Save, reload, and read the tab. Then put </title> back.
You are done when you have seen your own tags in the tab, and the repaired page looks as it did before.
Task 3: Look at the page as a phone would
Open the developer tools and turn on the device toolbar, as in module 1, so that the page is drawn at the width of a phone. Look at the size of the text. Then delete the viewport line, save, and reload. Look again. Put the line back.
You are done when you can say in one sentence what changed without the viewport line, and the line is back in your file.
Task 4: A skeleton in another language
Write a new page, my-language.html, in a language you know that is not English. Look up the code for that language on the MDN page about lang in "Go deeper". Write the skeleton from memory first, then compare it with the worked example.
You are done when the tab shows a title in your language, the lang attribute has the right code, and every line of the skeleton is in the right place.
Recap
- Every page starts with
<!doctype html>, thenhtmlwith alangattribute, aheadand abody. - The
headholds information about the page: the character set, the viewport line and thetitle. None of it is shown in the page. - Without the viewport line, a phone shrinks the page and the text becomes too small to read.
- The
titleis the name on the tab. If the tab or the page looks wrong, check theheadfirst.
Go deeper
- MDN: Basic HTML syntax walks through elements, attributes and the skeleton of a page.
- MDN: What's in the head? explains the title, the character set and the other lines that can go in the
head. - MDN: The viewport meta tag shows what a phone does with and without the viewport line.
- MDN: The lang attribute explains language codes and who uses them.