What is the <head> Element?

The <head> element contains metadata — information about the webpage that is not displayed directly to users but is essential for browsers, search engines, and external services.

Key Functions:

  • Defines the page title (shown in browser tab and search results)
  • Contains meta tags for SEO, responsiveness, and social sharing
  • Links external resources like CSS, fonts, and favicons
  • Loads JavaScript files and defines page-level scripts
  • Sets character encoding and language

Important: Everything in <head> is invisible to users but critical for how your page works and appears in search engines and social media.

Common Tags Inside <head>

Tag Purpose Example
<title> Page title (browser tab & search results) <title>My Website</title>
<meta charset> Character encoding (UTF-8 for international text) <meta charset="UTF-8">
<meta viewport> Mobile responsiveness settings <meta name="viewport" content="width=device-width, initial-scale=1">
<meta description> Page description for search engines <meta name="description" content="Learn HTML">
<link> Links external resources (CSS, fonts, icons) <link rel="stylesheet" href="style.css">
<style> Internal CSS styling <style>body { margin: 0; }</style>
<script> JavaScript code or external JS files <script src="app.js"></script>

The <title> Tag

The title is one of the most important elements for SEO and user experience.

<title>HTML Tutorial - Learn Web Development</title>

Best Practices:

  • Keep it 50-60 characters - Longer titles get cut off in search results
  • Put important keywords first - "HTML Tutorial" not "Learn About HTML Tutorial"
  • Be descriptive - Tell users exactly what the page is about
  • Make it unique - Every page should have a different title
  • Include brand name - "Product Name | Company Name"

Essential Meta Tags

1. Character Encoding (Required)

<meta charset="UTF-8">

Always use UTF-8 to support international characters (é, ñ, 中, etc.)

2. Viewport Meta Tag (Required for Mobile)

<meta name="viewport" content="width=device-width, initial-scale=1.0">

Makes your site responsive on mobile devices. Without this, mobile browsers show desktop version zoomed out.

3. Description Meta Tag (Important for SEO)

<meta name="description" content="Learn HTML with our comprehensive guide. Perfect for beginners with examples and exercises.">

Shown in search results below your page title. Keep it 150-160 characters.

4. Keywords Meta Tag (Legacy)

<meta name="keywords" content="HTML, tutorial, web development, beginners">

Note: Most search engines ignore this now, but it doesn't hurt to include it.

5. Author Meta Tag

<meta name="author" content="Aman Raj">

Specifies who created the page.

Social Media Meta Tags

Control how your page looks when shared on social platforms:

Open Graph (Facebook, LinkedIn)

<meta property="og:title" content="HTML Tutorial">
<meta property="og:description" content="Learn HTML from scratch">
<meta property="og:image" content="https://example.com/preview.jpg">
<meta property="og:url" content="https://example.com/html-tutorial">
<meta property="og:type" content="website">

Twitter Cards

<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="HTML Tutorial">
<meta name="twitter:description" content="Learn HTML from scratch">
<meta name="twitter:image" content="https://example.com/preview.jpg">

Favicon

The small icon shown in the browser tab:

<link rel="icon" type="image/png" href="favicon.png">
<link rel="apple-touch-icon" href="apple-icon.png">

Tip: Use a 32x32px PNG or ICO file for best compatibility.

Complete Example

<!DOCTYPE html>
<html lang="en">
<head>
  <!-- Essential Meta Tags -->
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <!-- SEO Meta Tags -->
  <title>HTML Tutorial - Learn Web Development | WebDev Academy</title>
  <meta name="description" content="Learn HTML with our comprehensive guide. Perfect for beginners with examples and exercises.">
  <meta name="keywords" content="HTML, tutorial, web development, beginners">
  <meta name="author" content="Aman Raj">
  
  <!-- Social Media Meta Tags -->
  <meta property="og:title" content="HTML Tutorial - Learn Web Development">
  <meta property="og:description" content="Learn HTML from scratch with practical examples">
  <meta property="og:image" content="https://example.com/preview.jpg">
  <meta property="og:url" content="https://example.com/html-tutorial">
  
  <!-- Favicon -->
  <link rel="icon" type="image/png" href="favicon.png">
  
  <!-- External CSS -->
  <link rel="stylesheet" href="style.css">
  
  <!-- Internal CSS -->
  <style>
    body { 
      font-family: Arial, sans-serif; 
      background-color: #f5f5f5; 
      margin: 0;
      padding: 20px;
    }
  </style>
  
  <!-- JavaScript -->
  <script>
    console.log("Page loaded successfully!");
  </script>
</head>
<body>
  <h1>HTML Head Demo</h1>
  <p>Check the browser tab title, and inspect the page metadata.</p>
</body>
</html>

What Each Part Does:

  • <meta charset> → Enables international characters
  • <meta viewport> → Makes page mobile-responsive
  • <title> → Sets browser tab title and search result title
  • <meta description> → Improves SEO and search result snippet
  • <meta property="og:*"> → Controls social media preview
  • <link rel="icon"> → Adds favicon to browser tab
  • <link rel="stylesheet"> → Loads external CSS
  • <style> → Defines internal CSS
  • <script> → Adds JavaScript functionality

Performance Meta Tags

Preloading Resources

<link rel="preload" href="font.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="hero-image.jpg" as="image">

Tells the browser to load important resources early.

DNS Prefetch

<link rel="dns-prefetch" href="https://fonts.googleapis.com">

Speeds up loading of external resources.

Quick Quiz

Q1. Which tag defines the title shown in the browser tab?
Q2. Which meta tag is essential for mobile responsiveness?
Q3. What is the ideal length for a meta description?
Q4. Which meta tag controls how pages appear when shared on Facebook?

Practice Tasks

  1. Create a complete HTML head section with:
    • Title: My Portfolio | Web Developer
    • Meta description about your skills (150 characters)
    • Meta author with your name
    • Viewport meta tag for mobile
    • Open Graph tags for social sharing
  2. Add a favicon: Create or download a 32x32px icon and link it in the head
  3. Performance optimization: Add preload tags for a CSS file and a web font
  4. Test SEO: Use a browser extension or online tool to check your meta tags

Lesson Summary

Key Takeaways:

  • The <head> element contains invisible but crucial page metadata
  • Essential tags: title, charset, viewport, description
  • SEO impact: Title and description appear in search results
  • Social sharing: Open Graph and Twitter Cards control preview appearance
  • Performance: Preload and DNS prefetch improve page speed
  • Best practice: Every page needs unique, descriptive metadata

Proper head configuration improves SEO, user experience, and social media presence. Never skip the metadata!