HTML <head> Tag

The <head> tag in HTML serves as a container for metadata. Metadata is essentially "data about data" and provides information about the HTML document itself. It's not directly visible to users on the rendered webpage but is crucial for browsers, search engines, and other web services to understand and interpret the document correctly. This includes things like the document title, character set, links to external resources (like CSS and JavaScript files), and meta descriptions.

HTML Head: Defining Document Metadata

Syntax

<head>
  <!-- Metadata elements go here -->
</head>

Attributes

The <head> tag itself does not accept any specific attributes. It's primary function is to serve as a container for other metadata elements.

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My Webpage</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <h1>Hello, World!</h1>
</body>
</html>

More Examples

1. Adding a Meta Description:

Meta descriptions provide a brief summary of the page's content, often used by search engines.

<head>
  <meta name="description" content="This is a brief description of my webpage. It should be informative and concise.">
</head>

2. Setting Keywords:

Keywords help search engines understand the topic of the page. While less impactful than before, they are still helpful.

<head>
  <meta name="keywords" content="web development, html, tutorial, example">
</head>

3. Specifying the Author:

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

4. Linking External CSS Stylesheets

<head>
    <link rel="stylesheet" href="styles.css">
</head>

5. Linking to an Icon for the Page

<head>
    <link rel="icon" href="/favicon.ico" type="image/x-icon">
</head>

6. Setting different encodings:

<head>
  <meta charset="UTF-16">
</head>

7. Adding Open Graph meta tags for Social Sharing

<head>
    <meta property="og:title" content="My Webpage Title">
    <meta property="og:description" content="A brief description for social sharing.">
    <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">
</head>

8. Using Multiple meta tags

 <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="This is a meta description for the article">
        <meta name="keywords" content="html, head, tag, metadata">
        <meta name="author" content="CodeLucky.com">
        <title>The HTML Head Tag</title>
        <link rel="stylesheet" href="style.css">
    </head>

Browser Support

The <head> tag is a fundamental part of HTML and is supported by all major browsers:

  • Chrome
  • Edge
  • Firefox
  • Safari
  • Opera

Notes and Tips

  • Placement: The <head> tag should always be placed within the <html> tag and before the <body> tag.
  • Essential: It's essential for providing necessary information about the document.
  • Metadata: The <head> tag is the standard container for <title>, <meta>, <link>, <style> and <script> elements. It should not contain visible content for the user.
  • Character Set: Always specify the character encoding with <meta charset="UTF-8"> to ensure proper text display.
  • Viewport: Include <meta name="viewport" content="width=device-width, initial-scale=1.0"> to ensure your site is responsive on various devices.
  • Order Matters: While not always critical, placing the title before other meta tags is a generally good practice. The <link> tags for CSS should be placed before <script> tags for JavaScript to prevent content from flashing before the styles are applied.
  • SEO: Use meta descriptions and keywords wisely to improve search engine optimization (SEO), but remember, quality content is still the most crucial factor for SEO.
  • Open Graph: Use Open Graph meta tags to make your website share well on social media.

By properly utilizing the <head> tag and its contained elements, you can create well-structured, search engine-friendly, and user-accessible web pages.