Lesson 9 of 52HTML
The Head and SEO
Fill the head with title, description, viewport, favicon and link-preview tags, then add structured data, canonical, hreflang and robots rules.
Everything you have built so far lives in the body, the part people see. The head is the part they mostly do not see, yet it decides a surprising amount: the words in the browser tab, the blue link and grey snippet in search results, the card that appears when someone shares your page in a chat, and whether the page looks right on a phone at all. It is also where you connect your CSS and JavaScript. Ten good lines in the head are some of the highest value lines on any site.
A complete head
Here is a solid starting head for almost any page. Every line is explained below.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sourdough for Beginners | Crumb Club</title>
<meta name="description" content="A step by step sourdough guide: feeding a starter, shaping, and baking your first loaf at home.">
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<link rel="stylesheet" href="/styles.css">
<script src="/app.js" defer></script>
</head>
<body>
...
</body>
</html>Setup. charset tells the browser how to turn bytes into letters. viewport makes phones show the page at its real width.
The basics: charset, lang and viewport
meta charset="utf-8" tells the browser which character encoding the file uses, the rule for turning bytes into letters. UTF-8 covers every language and emoji, it is the standard, and it should be the first thing in the head so the browser does not have to restart reading. Without it you may see é where an é should be.
The lang attribute on html declares the page's language, for example en, de, or pt-BR for Brazilian Portuguese. Screen readers use it to pick the right pronunciation, browsers use it to offer translation, and CSS uses it for things like hyphenation. If one quote in the page is in French, mark just that part: <span lang="fr">.
The viewport meta tag matters on phones. Without it, mobile browsers pretend the screen is about 980 pixels wide, draw the page at that size and shrink it down, so everything looks tiny. width=device-width, initial-scale=1 says "use the real screen width". Every responsive site needs this line, and you will rely on it in the responsive design lesson. Do not add user-scalable=no or maximum-scale=1: blocking zoom hurts people who need larger text.
Title and description
The title is the single most important line in the head. It appears in the browser tab, in bookmarks, in history, as the clickable headline in search results, and is the first thing a screen reader announces when the page loads. Good titles are:
- Unique for each page. Ten tabs all saying "Home" are no help.
- Specific first. Put the page topic first and the site name last: "Sourdough for Beginners | Crumb Club".
- Short. Search engines cut off long titles. Around 50 to 60 characters usually fits.
The meta description is a one or two sentence summary. Search engines often show it as the snippet under the title, though they may write their own from the page text if they think it fits the search better. It does not directly change your ranking, but a clear description makes people more likely to click. Aim for roughly 150 to 160 characters.
Preview
This preview reads your title and description and draws a mock search result (a small script does the drawing; you will learn scripts in the JavaScript & TypeScript course). Rewrite the title for a page about bagels and watch the result update. Then make the description very long and see the character count climb past 160, where a real search engine would start cutting it off.
Favicons
A favicon is the small icon in the browser tab and bookmarks. Modern browsers accept an SVG, which stays sharp at every size:
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<link rel="icon" href="/favicon.ico" sizes="32x32">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">The .ico line is a fallback for older browsers and tools. The apple-touch-icon (180 by 180 pixels is the usual size) is used when someone adds your site to an iPhone home screen.
Link previews with Open Graph
Paste a link into a chat app or social network and a card often appears with an image, a title and a short text. Most of these apps read Open Graph tags, a format Facebook introduced that is now widely used. They are meta tags with a property attribute instead of name:
<meta property="og:title" content="Sourdough for Beginners">
<meta property="og:description" content="Feed a starter, shape, and bake your first loaf.">
<meta property="og:image" content="https://crumbclub.example/og/sourdough.jpg">
<meta property="og:url" content="https://crumbclub.example/guides/sourdough">
<meta property="og:type" content="article">Use a full address (starting with https://) for og:image and og:url, because the app reading them is not on your page. An image of 1200 by 630 pixels works well across most apps. Some platforms have their own extra tags (X reads twitter:card, for example) but fall back to Open Graph for the rest.
Preview
This mimics a chat app's link card. Change the og:title content and watch the card follow. Delete the og:description line entirely and see "(missing)": real apps would either leave it blank or guess from the page text, which is rarely what you want.
Match each head tag to what it controls.
Linking CSS and JavaScript
CSS goes in the head with a link element so the browser has the styles before it draws anything. If CSS arrived late, people would first see unstyled text and then watch the page jump.
Scripts are trickier. When the browser parses HTML and meets a plain script, it stops, downloads and runs the script, and only then continues reading the page. A script in the head therefore runs before the body exists, and it delays everything below it.
Preview
The script finds the paragraph because it comes after it in the source. Cut the p line and paste it below the closing script tag: now the script runs first and finds nothing. That ordering problem is exactly what defer solves.
With defer on an external script, the browser downloads it in parallel while it keeps reading the HTML, then runs it after the whole document has been parsed, in the order the scripts appear. That gives you the best of both: the script can live in the head, start downloading early, and still see the full page.
<script src="/app.js" defer></script>There is also async, which runs the script as soon as it downloads, in any order. It suits independent scripts like analytics. Note that defer and async only apply to scripts with a src, and scripts with type="module" are deferred automatically.
script src='app.js' in the head, and app.js does document.querySelector('#menu'). It returns null. What is the cleanest fix?How search engines read a page
SEO (search engine optimisation) is the work of helping search engines find, understand and rank your pages. A search engine sends a program called a crawler to fetch pages and follow links. It then indexes each page: it reads the text, the title, the headings, the link text and the alt text, and files the page under the topics it covers. When someone searches, the engine ranks the indexed pages it thinks answer best.
Put the life of a page in a search engine in order.
- Tap the steps below in the right order.
Most of what helps is what you have already learned. Clear, useful text written for people. One descriptive h1 and a logical heading order. Real links with descriptive text, so crawlers can follow them. Alt text on images. Fast pages that work on phones. Semantic HTML is good SEO. Tricks like hidden keywords tend to get pages demoted, not promoted.
When the same page is reachable at several addresses (with and without tracking parameters, say), a canonical link in the head names the main one: <link rel="canonical" href="https://crumbclub.example/guides/sourdough">. Search engines then treat the copies as one page.
Two files help crawlers too: robots.txt at the root of the site tells crawlers which paths to skip, and a sitemap.xml lists the pages you want found. Google Search Console and Bing Webmaster Tools let you submit a sitemap and see how your pages are doing.
Structured data with JSON-LD
The title and description tell a search engine what a page is called. Structured data tells it what the page is: an article with an author and a publish date, a product with a price and stock level, a page that sits at a certain place in the site's hierarchy. Search engines use it for rich results: breadcrumbs instead of a raw URL, star ratings, prices and availability under a product link. AI search tools also read it to understand what the page is about.
The format search engines recommend is JSON-LD: a JSON object using the schema.org vocabulary, inside a script tag with a special type. The browser does not run it. It is data, and it can sit in the head or the body.
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "Sourdough for Beginners",
"image": ["https://crumbclub.example/og/sourdough.jpg"],
"datePublished": "2026-09-14T08:00:00+01:00",
"dateModified": "2026-10-02T09:30:00+01:00",
"author": [{ "@type": "Person", "name": "Priya Shah", "url": "https://crumbclub.example/team/priya" }]
}
</script>The three types you will meet most at work:
Article(orBlogPosting,NewsArticle) for content pages:headline,image,datePublished,dateModified,author.Productfor shop pages:name,image, and anoffersobject withprice,priceCurrencyandavailability(for examplehttps://schema.org/InStock), plusaggregateRatingif the page shows reviews.BreadcrumbListfor the trail of parent pages: anitemListElementarray ofListItemobjects, each with aposition, anameand anitemURL.
You can put several objects on one page, for example a Product and a BreadcrumbList. The preview below reads the JSON-LD from its own page, the way a crawler does, and draws a mock result:
Preview
Delete the aggregateRating line and the rating text disappears. Remove a comma after one of the objects and the whole block fails to parse: one syntax error and a crawler ignores everything in it. That is the most common way structured data breaks in production, usually because someone built the JSON by gluing strings together instead of serialising an object.
Three rules keep structured data useful:
- Mark up only what is visible on the page. Ratings, prices or FAQs that the user cannot see break search engine guidelines, and the penalty can be losing rich results across the whole site.
- Generate it from the same data as the page. If the price comes from your database, the JSON-LD price must come from the same field, or they drift apart and Google flags a mismatch.
- Validate it. Google's Rich Results Test shows which rich results a URL qualifies for and what is missing, and the Schema Markup Validator (validator.schema.org) checks general schema.org correctness. Search Console then reports errors across all your pages. Search engines retire rich result types from time to time (Google has cut back FAQ and HowTo results, for example), so check Google's Search Gallery documentation for what is currently supported before building one.
If you work in Next.js, Metadata and Images shows how to render this tag from a page component.
Canonical links: the pitfalls
A canonical is a hint, not a command: search engines weigh it with redirects, internal links and sitemaps, and they can ignore it. They tend to ignore it when it contradicts everything else. The mistakes that show up in real audits:
- Pointing every page at the home page, or every paginated page (
?page=2,?page=3) at page 1. Those pages have different content, so the hint is wrong, and items only listed on later pages may never be indexed. Each paginated page should be its own canonical. - Two different canonical tags on one page, often one from a CMS plugin and one from the template. Google may ignore both.
- A canonical that points at a redirect, a 404 or a noindexed page. Point it at the final, indexable URL.
- Relative URLs and mismatched variants. Use an absolute URL and pick one form of host, protocol and trailing slash (
https://crumbclub.example/guides/sourdough, not sometimeshttp://orwww.or a trailing/). Your internal links and sitemap should use the same form. - Canonical tags added by JavaScript that differ from the one in the server HTML. Crawlers may see either, so put it in the HTML the server sends.
Every indexable page should carry a self-referencing canonical: a canonical that points to its own clean URL. It costs one line and absorbs all the tracking-parameter copies of the page.
hreflang for translated pages
When the same content exists in several languages or for several regions, hreflang links tell search engines which version to show which searcher. A UK searcher should land on the British English page with prices in pounds, not the US one.
<link rel="alternate" hreflang="en-gb" href="https://crumbclub.example/uk/guides/sourdough">
<link rel="alternate" hreflang="en-us" href="https://crumbclub.example/us/guides/sourdough">
<link rel="alternate" hreflang="de" href="https://crumbclub.example/de/guides/sauerteig">
<link rel="alternate" hreflang="x-default" href="https://crumbclub.example/guides/sourdough">The rules that trip teams up:
- The full set goes on every version, including a link to itself. If the German page lists the UK page but the UK page does not list the German one, the pair is ignored. These return links are the most common hreflang error.
- Codes are a language, optionally plus a region:
en,en-gb,pt-br. The language comes from ISO 639-1 and the region from ISO 3166-1, so the UK isgb, anden-ukis invalid. A region alone, likegb, is invalid too. x-defaultnames the fallback for everyone else, often a language picker or the international page.- Each version's canonical points at itself. Pointing every translation's canonical at the English page tells search engines the translations are duplicates, and they drop out of the index. This one mistake can wipe out an entire market's search traffic.
- The
langattribute is still required for screen readers and translation, but search engines do not use it for this.hreflangcan also live in the sitemap or in HTTP headers, which suits large sites better than dozens oflinktags per page.
If you build multilingual sites in Next.js, Internationalization in Next.js covers generating these links.
The robots meta tag versus robots.txt
These two look like they do the same thing. They do not, and confusing them is a classic production incident.
robots.txtcontrols crawling. It tells well-behaved crawlers which paths not to fetch. It does not keep a URL out of the index: if other sites link to a blocked URL, it can still appear in results as a bare link with no snippet.- The robots meta tag controls indexing.
<meta name="robots" content="noindex">tells search engines not to list the page. For files that have no head, such as PDFs, send the same instruction as an HTTP header:X-Robots-Tag: noindex.
Here is the trap: a search engine can only obey noindex if it is allowed to crawl the page and see it. Block a page in robots.txt and add noindex, and the crawler never fetches the page, never sees the tag, and the URL can stay in the index. To remove a page from search, allow crawling, serve noindex, and wait for it to drop out.
User-agent: *
Disallow: /cart
Disallow: /search
Sitemap: https://crumbclub.example/sitemap.xmlOther values you will see in the robots meta tag: nofollow (do not follow links on this page), nosnippet, and max-image-preview:large (allow large image previews). A name="googlebot" meta targets only Google's crawler. robots.txt is also where companies opt out of AI training crawlers by their user-agent names; the names change, so check each vendor's documentation.
Two more things seniors know. robots.txt is public and only a polite request, so it is not security: never list secret paths in it, and protect staging sites with authentication, not just Disallow. And the most expensive SEO bug is usually a noindex (or a Disallow: /) copied from staging into production. Check the live head and robots.txt after every launch and template change.
noindex to it and also adds Disallow: /old-landing to robots.txt. Weeks later it is still in the results. Why?Match each audit finding to its fix.
- head
- The part of an HTML document with information about the page, not its visible content.
- viewport meta tag
- Tells mobile browsers to draw the page at the device's real width.
- meta description
- A short summary of the page that search engines often show under the title.
- Open Graph
- og: meta tags that control the title, text and image of a shared link card.
- defer
- A script attribute that downloads in parallel and runs after the HTML is parsed, in order.
- crawler
- A program that fetches pages and follows links so a search engine can index them.
- SEO
- Search engine optimisation: helping search engines find, understand and rank your pages.
- JSON-LD
- Structured data in the schema.org vocabulary, inside a script type=application/ld+json tag, describing what the page is.
- canonical link
- A link rel=canonical hint naming the main URL of a page, so copies at other addresses are treated as one.
- hreflang
- Alternate links that map each language or regional version of a page to the others, with return links on every version.
- noindex
- A robots meta tag or X-Robots-Tag header value that keeps a page out of search results. The page must stay crawlable for it to work.