HTML <header> data-* Attribute

The data-* attribute on a <header> tag attaches additional data to the header. To create a custom attribute, replace the * with a lowercase string, such as data-id, data-status, or data-location.



A custom data-year-published attribute on a <header>.
The attribute value is not visible, but it is readable by JavaScript.

Header section of a page...
<header data-year-published="2020">
  Header section of a page...

Using data-*

The data-* attribute allows you to add custom attributes to a <header> element.

The * part is replaced with a lowercase string, such as data-id, data-cost, or data-location.

An <header> element can have any number of data-* attributes, each with their own name.

These attributes usually store additional data about the header (e.g. id, options, variations, etc.).

Using data-* attributes reduces the need for Ajax requests to the server.

Note: The data-* attribute does not change the appearance of the header tag in any way.


<header data-*="value">

Note: The * can be any string, such as data-iddata-costdata-supplier,  etc.



Value Description
value A string value. Can be numeric, alphanumeric, JSON, etc.

More Examples

A custom data-year-published attribute on a <header> element.
Clicking the button will display the year published.

Header section of a page...

<header id="myheader" data-published-year="2020">
  Header section of a page...

<button onclick="show();">Show data</button>

  let show = () => {
    let element = document.getElementById("myheader");
    alert("Year published = " + element.getAttribute('data-published-year'));

Code explanation

The <header> tag below contains the data-year-published attribute.

The data-year-published attribute specifies the page publication year.

Clicks are handled by the onclick event.

Onclick invokes a JavaScript function that extracts and displays the page publication year.

Note: Notice how the year will displays immediately without server call.

Browser support

Here is when data-* support started for each browser:

6.0 Sep 2010
4.0 Mar 2011
9.0 Mar 2011
11.1 Mar 2011
5.0 Jun 2010

You may also like

 Back to <header>