Skip to content

05. head and meta Elements

The <head> element contains metadata about the HTML document—information that describes the document but isn’t displayed on the page. Within the head, <meta> elements provide various types of metadata that help browsers, search engines, and other services understand and process your document correctly. Understanding how to use these elements is crucial for creating well-optimized, accessible web pages.

The <head> element is a container for metadata and other non-visible information about the document. It must be placed between the <html> opening tag and the <body> opening tag.

<!DOCTYPE html>
<html lang="en">
<head>
<!-- Metadata goes here -->
</head>
<body>
<!-- Visible content -->
</body>
</html>

The <head> element typically contains:

  • <title>: Document title
  • <meta>: Various metadata
  • <link>: External resources (CSS, icons)
  • <style>: Embedded CSS
  • <script>: JavaScript (though often placed before </body>)
  • <base>: Base URL for relative links

The <meta> element provides metadata about the HTML document. Meta elements are self-closing and don’t have content between opening and closing tags.

<meta name="description" content="Page description">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

The charset meta tag specifies the character encoding for the document. It should be the first element in the <head>:

<head>
<meta charset="UTF-8">
<!-- Other meta tags -->
</head>

Why UTF-8?

  • Supports all characters from all languages
  • Standard encoding for modern web
  • Prevents character display issues
  • Required for proper rendering of special characters and emojis

Critical for responsive design on mobile devices:

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

What it does:

  • width=device-width: Sets page width to match device width
  • initial-scale=1.0: Sets initial zoom level to 100%

Without this tag: Mobile browsers may render pages at desktop width, requiring users to zoom in.

Provides a summary of the page content for search engines:

<meta name="description" content="Learn HTML, CSS, and JavaScript with comprehensive tutorials and examples.">

Best practices:

  • Keep it under 160 characters
  • Write compelling, descriptive text
  • Include relevant keywords naturally
  • Make it unique for each page

The keywords meta tag is largely ignored by modern search engines:

<!-- Not recommended for SEO -->
<meta name="keywords" content="HTML, CSS, JavaScript">

Modern SEO relies on content quality rather than meta keywords.

Specifies the author of the document:

<meta name="author" content="John Doe">

Controls how search engines index and follow links:

<!-- Allow indexing and following links -->
<meta name="robots" content="index, follow">
<!-- Prevent indexing -->
<meta name="robots" content="noindex">
<!-- Prevent following links -->
<meta name="robots" content="nofollow">
<!-- Prevent both -->
<meta name="robots" content="noindex, nofollow">

Open Graph tags enable rich previews when sharing links on social media:

<!-- Basic Open Graph -->
<meta property="og:title" content="Page Title">
<meta property="og:description" content="Page description">
<meta property="og:image" content="https://example.com/image.jpg">
<meta property="og:url" content="https://example.com/page">
<meta property="og:type" content="website">
<!-- Twitter Card (alternative/additional) -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Page Title">
<meta name="twitter:description" content="Page description">
<meta name="twitter:image" content="https://example.com/image.jpg">

These meta tags can simulate HTTP headers:

<!-- Redirect after 5 seconds -->
<meta http-equiv="refresh" content="5; url=https://example.com">
<!-- Refresh page every 30 seconds -->
<meta http-equiv="refresh" content="30">

Note: Use JavaScript for redirects in modern development; this is mainly for legacy support.

<meta http-equiv="Content-Security-Policy" content="default-src 'self'">

Note: CSP is better implemented via HTTP headers, but meta tags can be used as a fallback.

While not a meta tag, the <title> element is essential metadata:

<head>
<title>My Web Page - Home</title>
</head>

Best practices:

  • Keep it under 60 characters
  • Include site name and page description
  • Make it unique for each page
  • Use descriptive, keyword-rich text

Here’s a comprehensive example of a well-structured <head> section:

<!DOCTYPE html>
<html lang="en">
<head>
<!-- Character encoding (must be first) -->
<meta charset="UTF-8">
<!-- Viewport for responsive design -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Page description -->
<meta name="description" content="Learn web development with comprehensive HTML, CSS, and JavaScript tutorials.">
<!-- Author -->
<meta name="author" content="Your Name">
<!-- Robots -->
<meta name="robots" content="index, follow">
<!-- Open Graph for social sharing -->
<meta property="og:title" content="Web Development Tutorials">
<meta property="og:description" content="Learn web development with comprehensive tutorials.">
<meta property="og:image" content="https://example.com/og-image.jpg">
<meta property="og:url" content="https://example.com">
<meta property="og:type" content="website">
<!-- Twitter Card -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Web Development Tutorials">
<meta name="twitter:description" content="Learn web development with comprehensive tutorials.">
<!-- Page title -->
<title>Web Development Tutorials | Learn HTML, CSS, JavaScript</title>
<!-- Stylesheet -->
<link rel="stylesheet" href="styles.css">
<!-- Favicon -->
<link rel="icon" type="image/x-icon" href="/favicon.ico">
</head>
<body>
<!-- Content -->
</body>
</html>

Problem: Forgetting the charset meta tag.

Impact: Special characters may not display correctly.

Solution: Always include <meta charset="UTF-8"> as the first element in <head>.

Problem: Not including the viewport meta tag.

Impact: Pages won’t be mobile-responsive.

Solution: Always include the viewport meta tag for responsive design.

Problem: Using the same title on every page.

Impact: Poor SEO and user experience.

Solution: Create unique, descriptive titles for each page.

  1. Charset first: Place <meta charset="UTF-8"> as the first element in <head>
  2. Always include viewport: Essential for mobile responsiveness
  3. Unique descriptions: Write unique meta descriptions for each page
  4. Descriptive titles: Use clear, keyword-rich titles
  5. Open Graph tags: Include them for better social media sharing
  6. Keep it organized: Group related meta tags together
  7. Validate: Use validators to check your meta tags