HTML <head> Section: Elements and Best Practices

HTML <head> Section: Elements and Best Practices

The head is the control room of every web page. It tells browsers how to read the page, search engines how to index it and social apps how to preview it.

⏱ 20 min 🧩 8 elements 🎯 Beginner → Intermediate ✅ Checklist + quiz
01

What is the head?

The <head> element holds metadata: data about the page, not content shown inside it. Only the <title> becomes visible, as the browser tab label.

🌐
Browsers

Read the charset, viewport, styles and scripts to render the page properly.

🔍
Search engines

Use the title, description, canonical URL and robots rules to index and display it.

📱
Social apps

Use Open Graph tags to build the preview card for shared links.

RememberA page has exactly one head. It sits between <html> and <body> and must come first.
02

Head explorer

Click any line of this head to see what it does.

<head> </head>
03

Allowed elements

Only these elements are valid in the head. Anything else, such as <div> or <p>, ends the head early.

ElementPurposeKey attributesStatusCount
<title>Document title—Required1
<meta>Metadatacharset name content propertyRequiredMany
<link>External resourcesrel href as sizesCommonMany
<style>Internal CSSmediaOptionalMany
<script>JavaScriptsrc defer async typeOptionalMany
<base>Base URL for relative linkshref targetOptionalMax 1
<noscript>No-JavaScript fallback—OptionalMany
<template>Inert reusable markupidOptionalMany
04

Elements in detail

Charset <meta charset>

Declares how text is decoded. Use UTF-8 and make it the first element, within the first 1024 bytes of the file.

charset.html
<meta charset="UTF-8">

Viewport <meta name="viewport">

Makes the page responsive on phones. Without it, mobile browsers render at desktop width and zoom out.

viewport.html
<meta name="viewport" content="width=device-width, initial-scale=1.0">
AccessibilityNever use user-scalable=no or maximum-scale=1. They stop users from zooming.

Title <title>

Names the tab, bookmark, history entry and search result. Make it unique, about 50–60 characters, with the key words first.

title.html
<!-- Pattern: Page topic | Site name -->
<title>HTML Head Section: Elements and Best Practices | HTML Lessons</title>

Description <meta name="description">

The summary search engines often show under your title. Write it for people, about 150–160 characters, different for every page.

description.html
<meta name="description"
      content="Learn every HTML head element with examples and best practices.">

Links <link>

The rel attribute defines the relationship: stylesheet, icon, canonical page, or a performance hint.

link.html
<link rel="stylesheet" href="css/style.css">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="canonical" href="https://example.com/html-head">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preload" href="fonts/main.woff2" as="font" type="font/woff2" crossorigin>

Style <style>

Embeds CSS in the page. Fine for demos and critical CSS; for real projects prefer an external file so it can be cached.

style.html
<style>
  body { font-family: system-ui, sans-serif; margin: 0; }
</style>

Script <script>

A normal script pauses HTML parsing. In the head, add defer or async.

Normal

Pauses parsing while it downloads and runs. Slowest.

defer

Downloads in parallel, runs after parsing, keeps order. Best default.

async

Runs as soon as ready, in any order. Good for analytics.

script.html
<script src="js/app.js" defer></script>
<script src="js/analytics.js" async></script>
<script type="module" src="js/main.js"></script>

Base <base>

Sets the base URL for every relative link. It affects the whole page, so use it with care and place it before any URL.

base.html
<base href="https://example.com/lessons/" target="_blank">

Social and extra meta tags

social.html
<meta property="og:title" content="HTML Head Section">
<meta property="og:description" content="Elements and best practices.">
<meta property="og:image" content="https://example.com/cover.jpg">
<meta name="twitter:card" content="summary_large_image">
<meta name="theme-color" content="#E44D26">
<meta name="robots" content="noindex, nofollow" <!-- only when needed -->
Good to knowThe old <meta name="keywords"> tag is ignored by major search engines. Leave it out.
05

Best practices

✓ Do
  • Set lang on <html>
  • Put charset first
  • Include the viewport meta
  • Unique title and description per page
  • Use defer for scripts
  • Link CSS in the head
  • Provide SVG and PNG icons
  • Add a canonical link when needed
✕ Don’t
  • Put visible tags in the head
  • Use two <title> or <base>
  • Block rendering with big scripts
  • Disable zoom
  • Copy one description everywhere
  • Load dozens of third-party scripts
  • Rely on the keywords meta tag
  • Redirect with meta refresh

Spot the mistake

mistake.html
<head>
  <meta charset="UTF-8">
  <div>Welcome!</div>   <!-- ✕ closes the head right here -->
  <title>My Page</title>  <!-- now treated as body content -->
</head>
06

Recommended order

Order affects correctness and speed. Use this sequence by default.

  1. <meta charset>First, so everything after it is decoded correctly.
  2. <meta name="viewport">Layout settings before rendering starts.
  3. <title>Identifies the document early.
  4. <base> (if used)Must come before any element with a relative URL.
  5. Description, robots and social metaMetadata for search engines and platforms.
  6. preconnect / preloadStart critical network work as early as possible.
  7. Stylesheets and <style>CSS before scripts so styles are not delayed.
  8. <script defer>Scripts last; deferred so they never block rendering.
  9. Icons and canonical linkLow priority, safe at the end.
07

Starter template

Copy this into every new page and replace the placeholders.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <!-- Encoding & viewport -->
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <!-- Title & SEO -->
  <title>Page Title | Site Name</title>
  <meta name="description" content="A clear 150–160 character summary.">
  <link rel="canonical" href="https://example.com/page">

  <!-- Social -->
  <meta property="og:title" content="Page Title">
  <meta property="og:description" content="Short description.">
  <meta property="og:image" content="https://example.com/cover.jpg">

  <!-- Appearance -->
  <meta name="theme-color" content="#E44D26">
  <link rel="icon" href="/icon.svg" type="image/svg+xml">
  <link rel="apple-touch-icon" href="/apple-touch-icon.png">

  <!-- Styles & scripts -->
  <link rel="stylesheet" href="css/style.css">
  <script src="js/app.js" defer></script>
</head>
<body>
  <!-- Visible content -->
</body>
</html>
08

Checklist & quiz

Pre-publish checklist0%

Quick quiz

Lesson summary

  • The <head> contains metadata, not visible content.
  • Valid children: title, meta, link, style, script, base, noscript, template.
  • Start with charset and viewport, then title and description.
  • Load CSS before scripts and use defer for JavaScript.
  • Add social and canonical tags to control sharing and indexing.
Built with HTML · Lesson: The <head> Section
Inar Learn
Inar Learnhttps://inarlearn.com
Inar Learn is an innovative online learning platform offering high-quality courses, tutorials, and resources to help learners gain practical skills and grow their knowledge.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here