05. head and meta Elements
Introduction
Section titled “Introduction”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
Section titled “The head Element”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.
Basic Structure
Section titled “Basic Structure”<!DOCTYPE html><html lang="en"><head> <!-- Metadata goes here --></head><body> <!-- Visible content --></body></html>What Goes in head
Section titled “What Goes in head”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
Section titled “The meta Element”The <meta> element provides metadata about the HTML document. Meta elements are self-closing and don’t have content between opening and closing tags.
Basic Syntax
Section titled “Basic Syntax”<meta name="description" content="Page description"><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">Essential Meta Tags
Section titled “Essential Meta Tags”Character Encoding
Section titled “Character Encoding”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
Viewport Meta Tag
Section titled “Viewport Meta Tag”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 widthinitial-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.
Page Description
Section titled “Page Description”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
Keywords (Deprecated)
Section titled “Keywords (Deprecated)”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.
Author Information
Section titled “Author Information”Specifies the author of the document:
<meta name="author" content="John Doe">Robots Meta Tag
Section titled “Robots Meta Tag”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 Meta Tags
Section titled “Open Graph Meta Tags”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">HTTP-Equiv Meta Tags
Section titled “HTTP-Equiv Meta Tags”These meta tags can simulate HTTP headers:
Refresh/Redirect
Section titled “Refresh/Redirect”<!-- 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.
Content Security Policy
Section titled “Content Security Policy”<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.
The title Element
Section titled “The title Element”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
Complete head Example
Section titled “Complete head Example”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>Common Mistakes
Section titled “Common Mistakes”Missing charset
Section titled “Missing charset”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>.
Missing viewport
Section titled “Missing viewport”Problem: Not including the viewport meta tag.
Impact: Pages won’t be mobile-responsive.
Solution: Always include the viewport meta tag for responsive design.
Generic titles
Section titled “Generic titles”Problem: Using the same title on every page.
Impact: Poor SEO and user experience.
Solution: Create unique, descriptive titles for each page.
Best Practices
Section titled “Best Practices”- Charset first: Place
<meta charset="UTF-8">as the first element in<head> - Always include viewport: Essential for mobile responsiveness
- Unique descriptions: Write unique meta descriptions for each page
- Descriptive titles: Use clear, keyword-rich titles
- Open Graph tags: Include them for better social media sharing
- Keep it organized: Group related meta tags together
- Validate: Use validators to check your meta tags