The <header>
tag in HTML is used to define a header section of a webpage or a section of the document. It typically contains introductory content or navigational links. The <header>
element is a semantic HTML5 element, meaning it provides meaning to the structure of the document, making it more accessible and understandable for both browsers and developers.
Purpose of the <header>
tag:
- To represent the top section of a page or a section of the content, which often includes things like:
- A logo
- Site title or heading
- Navigation links (menus)
- Search bar
- Contact information
- The
<header>
tag can be used in various places within a document, not just at the top of the page. You can have a header within a<section>
,<article>
, or<aside>
, for example.
Basic Syntax:
<header>
<!-- Content such as navigation, logo, title, etc. -->
</header>
Example:
Here’s an example of using the <header>
tag to create a simple page header:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My 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="#services">Services</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</header>
<main>
<!-- Main content of the page -->
</main>
</body>
</html>
Explanation of Example:
- The
<header>
tag contains a heading (<h1>
) and a navigation menu (<nav>
). - The
<nav>
element contains a list of links, which would typically serve as a navigation menu for the website.
Common Use Cases:
- Website Header:
- It often includes the website title, logo, and primary navigation links.
- Article/Section Header:
- Each
<article>
or<section>
element can have its own<header>
that includes headings and potentially introductory content.
- Each
- Introductory Content:
- A
<header>
might contain introductory content or a brief description about the content of the page or section.
- A
Can the <header>
tag be used more than once?
Yes, the <header>
tag can be used multiple times in a document. For example:
- You can use a
<header>
tag for the main page header. - Each
<section>
,<article>
, or<aside>
can also have their own<header>
tag.
This allows you to structure the document in a semantic and meaningful way.
Example with Multiple <header>
tags:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Blog</title>
</head>
<body>
<header>
<h1>My Blog</h1>
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
</ul>
</nav>
</header>
<section>
<header>
<h2>Latest Blog Post</h2>
<p>Posted on January 16, 2025</p>
</header>
<p>Welcome to my latest post...</p>
</section>
<section>
<header>
<h2>Older Post</h2>
<p>Posted on January 10, 2025</p>
</header>
<p>Here is another post...</p>
</section>
</body>
</html>
Key Points:
- Semantic HTML: The
<header>
tag makes your document more semantic and accessible. It tells the browser and screen readers that the enclosed content is the header section of the page or section. - Improved SEO and Accessibility: Using semantic elements like
<header>
can improve search engine optimization (SEO) because it helps search engines understand the structure and context of your content. It also aids in accessibility for users relying on screen readers.
Conclusion:
The <header>
tag in HTML is a semantic element used to define a header for a document or section. It can contain introductory content, logos, titles, and navigation links. The use of the <header>
tag helps improve the structure of a web page, making it more accessible and understandable for both developers and users.