← Back to HTML & CSS, page 3

html-css-0052 · Easy fresher

Which metadata tags belong in the head of a page, and what does each one do?

The head contains metadata for browsers, search engines and sharing apps. Key tags are charset, viewport, a unique title, a description, a canonical link and Open Graph tags. The lang attribute on html declares the page language.

Full answer

The head holds information about the page. Visitors do not see it on the page, but browsers, search engines and chat apps read it.

  • meta charset="utf-8" tells the browser how to decode the text. Put it first, within the first 1024 bytes of the file.
  • title is the name shown in the tab, in bookmarks and in search results. Give every page its own title, and put the most specific words first.
  • meta name="description" is a short summary. Search engines may show it under the title. About 150 to 160 characters is a common guide, because longer text is often cut off. It is not a ranking factor, but a good one earns more clicks.
  • meta name="viewport" makes layout use the real device width on phones.
  • link rel="canonical" names the preferred address when the same content is reachable at several URLs, such as with and without tracking parameters.
  • Open Graph tags (og:title, og:description, og:image) control the preview card when the link is shared in a chat or social app.
  • meta name="robots" content="noindex" asks search engines not to list the page, for example for a thank-you page.
  • lang on the html element states the page language. It helps screen readers pick a voice and helps translation tools.

In this page the description is under 160 characters, the canonical address is an absolute URL, and the language is declared. Social and search previews depend on the crawlers that read these tags, so test a real link in the sharing tools you care about.

Use one unique title and description per page. Repeating the same ones across many pages makes search results look the same and wastes the space.

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>CSS Flexbox Practice Questions | Schoolabe</title>
  <meta name="description" content="Practise flexbox with ten interview-style questions, short answers and runnable examples for freshers and experienced developers.">
  <link rel="canonical" href="https://schoolabe.com/interview-prep/html-css/flexbox">
  <meta property="og:title" content="CSS Flexbox Practice Questions">
  <meta property="og:description" content="Ten flexbox questions with runnable examples.">
  <meta property="og:image" content="https://schoolabe.com/images/flexbox-card.png">
  <meta property="og:url" content="https://schoolabe.com/interview-prep/html-css/flexbox">
  <link rel="icon" href="/favicon.ico">
</head>
<body>
  <h1>Flexbox practice</h1>
</body>
</html>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • charset utf-8 goes first so the browser decodes the rest correctly.
  • Each page needs its own title and description.
  • canonical names the preferred URL when content is reachable at several addresses.
  • Open Graph tags control the preview card when a link is shared.
  • lang on the html element helps screen readers and translation tools.

Common mistakes

  • Leaving out the description and letting the search engine pick random text from the page.
  • Using the same title on every page, such as the site name only.
  • Using a relative URL in og:image or canonical. Crawlers need the full absolute address.

Follow-ups

  • What does the canonical link tell a search engine?
  • Why should the charset declaration come first in the head?

Tips

  • Write the tags for one real page in your head, with a title under about 60 characters and a description near 150. It is a common live task.
  • Mention noindex and canonical when asked about duplicate or private pages. They show you know what the head is for.