The HTML <header> tag is a key structural element used to define the header section of a webpage or an individual section within it. Typically, the <header> element contains introductory content such as logos, headings, navigation links, or other important information related to the section it belongs to. The <header> tag is versatile and can be used multiple times on a page for different sections like articles, blogs, or main navigation areas.
Syntax of the <header> Tag
<header>
<!-- Header content goes here -->
</header>
The <header> tag does not require any special attributes, but it is commonly used with elements like <h1>, <nav>, and <img> for headings, navigation, and logo placement, respectively.
Attributes of the <header> Tag
While the <header> tag itself does not support any unique attributes, it can utilize global attributes such as:
- class: Used to apply CSS styles to the header.
- id: Used to uniquely identify the header section, which is helpful for styling and JavaScript functionality.
- style: Allows inline CSS to style the header element directly.
- lang: Specifies the language of the content within the header.
- accesskey: Assigns a shortcut key to access the header section.
These global attributes enhance the functionality and styling of the header, allowing developers to create a more interactive and accessible user experience.
Examples of HTML <header> Tag
Example 1: Simple Website Header
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Website</title>
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</header>
<main>
<p>This is the main content of the website.</p>
</main>
</body>
</html>
In this example, the <header> tag contains the main title and navigation menu for the website, making it a useful introductory section.
Example 2: Section-Specific Header
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog Post</title>
</head>
<body>
<article>
<header>
<h2>The Importance of Web Accessibility</h2>
<p>Posted on August 15, 2024 by John Doe</p>
</header>
<p>Ensuring that your website is accessible to all users is critical in modern web development...</p>
</article>
</body>
</html>
In this example, the <header> tag is used within an <article> to provide the title and metadata for a blog post, making the content more organized and easily navigable.
FAQs About HTML <header>Tag
Q1: What is the purpose of the HTML <header> tag?
A: The <header> tag is used to define the introductory content of a webpage or section, typically containing logos, headings, navigation, or other metadata.
Q2: Can I use the <header> tag multiple times on a webpage?
A: Yes, you can use the <header> tag multiple times within different sections of a webpage, such as for individual articles, sections, or the overall site header.
Q3: Does the <header> tag improve SEO?
A: Yes, using the <header> tag helps search engines understand the structure of your page, improving SEO by making the content easier to crawl and index.
Q4: Is it mandatory to use the <header> tag for every webpage?
A: While it's not mandatory, using the <header> tag is a best practice as it improves content structure and accessibility.
Q5: What is the difference between <header> and <head> tags?
A: The <header> tag defines the header section visible to users on the webpage, while the <head> tag contains metadata about the document, such as the title, links to stylesheets, and scripts, and is not displayed to users.