Sub topic 4
Writing a Good Title
This chapter assumes you know basic HTML syntax and the structure of a document (doctype, html, head, body). It covers the title element: what it does, how to write it, and the mistakes that come up most often.
What the title does
The title element sets the name of the page. It lives in the head and holds plain text only. Browsers, search engines, and other tools read it in several places:
- The browser tab and window title bar.
- Bookmarks and the history list.
- The headline of a search result.
- The name a screen reader announces when a page loads.
- The default text when someone shares or saves the link.
Every document needs exactly one title. A page without one is invalid HTML, and browsers fall back to showing the URL or file name, which tells the user nothing.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Trail Running Shoes - Example Store</title>
</head>
<body>
<h1>Trail Running Shoes</h1>
</body>
</html>The title is not the h1
The title and the h1 often look alike, but they do different jobs. The title appears outside the page, in tabs and search results, where the reader has no other context. The h1 appears inside the page, where the surrounding content already gives context. So the title usually carries the site name and the h1 usually does not.
| Row | `title` | `h1` |
|---|---|---|
| Location | In the `head` | In the `body` |
| Visible on the page | No | Yes |
| Seen without context | Yes (tabs, results) | No |
| Count per page | Exactly one | One is the usual convention |
| Typical content | Page topic plus site name | Page topic |
Rules for a good title
- Make it unique. Two pages with the same title are hard to tell apart in a tab bar, a history list, or search results.
- Put the specific part first. Tabs shrink when many are open, and the end of a long title gets cut off. Lead with the page topic, then add the site name.
- Keep it short. Search engines cut long titles off in results. There is no fixed limit, since truncation depends on pixel width. Around 60 characters is a common working target.
- Describe the page. "Home" or "Untitled" says nothing. "Trail Running Shoes" says what the user will find.
- Write for people. Repeating keywords ("shoes, running shoes, cheap shoes") reads badly and does not help.
- Use plain text. Tags inside
titleare not parsed as markup. Characters such as&should be written as&.
A common pattern is Page topic - Site name:
<title>Waterproof Trail Shoes - Example Store</title>
<title>Shipping and Returns - Example Store</title>
<title>Contact Us - Example Store</title>Titles in pages that change
Single-page apps change the visible content without loading a new document. If the title stays the same, the tab, the history entries, and the screen reader announcement all stay stale. Update it on each navigation:
<script>
function showPage(name, titleText) {
document.title = titleText + " - Example Store";
// ...swap in the content for `name` here
}
showPage("shoes", "Trail Running Shoes");
</script>Setting document.title updates the tab immediately. Search engines may not see a title set late by JavaScript, so for pages that need to be indexed, send the correct title in the initial HTML.
Common errors
- Missing title. The tab shows a file name or URL.
- The same title on every page. Usually a template where the
titleis hard-coded in a shared layout. - Title outside the head. A
titleplaced in thebodyis invalid and may be ignored. - Tags in the title.
<title><b>Shoes</b></title>shows the literal text<b>Shoes</b>. - Site name first on every page. All tabs then start with the same words and look identical when shortened.
Checking your title
Open the page and read the tab text. Then open developer tools, select the Elements panel, and expand head to confirm there is one title. In the Console, you can read the live value:
console.log(document.title);Exercise
A shop uses the title "Example Store - Home" on every page, including product pages. Name two problems with this and rewrite the title for a page about waterproof trail shoes.
Show answer
Problems: the title is not unique, so tabs, history, and search results look identical, and it starts with the site name, which is what gets shown when the title is shortened. A better title is "Waterproof Trail Shoes - Example Store", with the page topic first and the site name last.
Exercise
Why might a single-page app need to set document.title when the user moves to a new view?
Show answer
The document does not reload, so the title would otherwise stay the same. The tab, history entries, and screen reader announcement would then describe the wrong page.