Introduction to <header>tag

The <header>tag in HTML is a semantic element that defines a container for introductory content or a set of navigational links. It's commonly used to represent the header of a webpage or a section, containing elements like headings, logos, navigation menus, and other introductory content. The <header>element helps improve the structure and readability of a webpage by clearly indicating the beginning of a page or a section.

Key Features of the <header>Tag

Semantic Meaning: The <header>tag provides semantic meaning to your HTML document. It indicates that the content enclosed within it is introductory or represents the start of a webpage or a specific section.

Multiple Uses: You can use multiple <header>tags on a single webpage. For example, a main <header>at the top of the page and separate <header>elements for different sections or articles within the page.

Content Flexibility: The <header>element can contain various types of content, including:
  • Headings (<h1>to <h6>): To introduce the page or section.
  • Logos or Branding: To visually represent the site or company.
  • Navigation Elements (<nav>): To provide a navigational menu or links to other parts of the site.
  • Search Forms: To include search functionality.

Accessibility: Using the <header>tag improves accessibility for screen readers and assistive technologies. It allows these tools to better understand the structure and purpose of the content, enhancing user experience for individuals with disabilities.

SEO Benefits: The <header>tag can positively impact SEO (Search Engine Optimization) by making the structure of the webpage clearer to search engines. It helps search engines understand which content is introductory or navigational, potentially improving the page's ranking.

Best Practices for Using the <header>Tag
  • Use It Wisely: While you can use multiple <header>elements on a page, avoid overusing them. Only use the <header>tag for content that serves as an introduction or provides navigational context.
  • Combine with Other Semantic Tags: The <header>element works well with other semantic tags like <nav>, <main>, <article>, and <footer>. Using these tags together creates a well-structured and semantically rich HTML document.
  • Avoid Redundant Content: Do not use the <header>tag to wrap content that is not introductory or navigational, such as large blocks of text or unrelated media elements.

Copy to clipboard
<!-- Header Example -->
<header class="custom-header">
  <h1>Site Logo</h1>
      <nav class="main-nav">
        <ul>
          <li><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Services</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
   </nav>
</header>
Copy to clipboard
/* CSS for Header */
    .custom-header {
        background-color: #f8f8f8;
        padding: 20px;
        text-align: center;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }
    
    .main-nav ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
    }
    
    .main-nav li {
        display: inline;
        margin: 0 15px;
    }
    
    .main-nav a {
        text-decoration: none;
        color: #333;
        font-weight: bold;
    }
    
    .main-nav a:hover {
        color: #5cb85c;
    }

Global Attributes

AttributesDescription
accesskeySpecifies a shortcut key to activate or focus an element.
autocapitalizeControls whether and how text input is automatically capitalized.
classSpecifies one or more class names for the element, allowing CSS styling and JavaScript manipulation.
contenteditableSpecifies whether the content of an element is editable or not.
contextmenuSpecifies a context menu for the element.
dirDefines the text direction. Possible values are "ltr" (left-to-right), "rtl" (right-to-left), and "auto".
draggableSpecifies whether an element is draggable. Possible values are "true" or "false".
enterkeyhintSpecifies the action label or icon to be shown for the enter key on virtual keyboards.
hiddenIndicates that the element is not yet, or is no longer, relevant. The browser does not display elements that have the `hidden` attribute set.
idSpecifies a unique identifier for the element.
inputmodeProvides a hint to browsers for which virtual keyboard configuration to use when editing this element or its descendants.
isAllows you to specify the type of custom element.
langSpecifies the language of the element's content.
nonceA cryptographic nonce ("number used once") that can be used by Content Security Policy (CSP) to determine whether or not a given fetch will be allowed to proceed.
partSpecifies the element’s part in the shadow DOM.
slotAssigns a slot in a shadow DOM shadow tree.
spellcheckIndicates whether the element is subject to spell checking.
styleProvides inline CSS styling for the element.
tabindexSpecifies the tab order of the element.
titleAdds extra information about the element, displayed as a tooltip when hovering over the element.
translateSpecifies whether the content of the element should be translated. Possible values are "yes" or "no".
HTML AttributesGlobal AttributesEvent Attributes
To view the full list
To view the full list
To view the full list
elementsChrome BrowsersMicrosoft Edge BrowserFirefox BrowsersSafari BrowserOpera Browser
<header>5.09.04.05.011.1