HTML <main> data-* Attribute

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

Example

#

A custom data-title attribute on a <main> element.
The attribute value is not visible, but it is readable by JavaScript.

Paul Cézanne was a French artist and Post-Impressionist painter whose work laid the foundations of the transition from the 19th-century conception of artistic endeavor to a new and radically different world of art in the 20th century.
<main data-title="Who is Paul Cézanne?">
  Paul Cézanne was a French artist and Post-Impressionist
  painter whose work laid the foundations of the transition 
  from the 19th-century conception of artistic endeavor 
  to a new and radically different world of art in the 
  20th century.
</main>

Using data-*

The data-* attribute adds custom information to a <main> element.

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

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

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

Note: The data-* attribute is not visible and does not change the appearance of the main.


Syntax

<main data-*="value">

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


Values

#

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

More Examples

A custom data-subject attribute on a <main> element.
Clicking the button will display the subject value.

Paul Cézanne was a French artist and Post-Impressionist painter whose work laid the foundations of the transition from the 19th-century conception of artistic endeavor to a new and radically different world of art in the 20th century.

<main id="mymain" data-subject="Who is Paul Cézanne?">
  Paul Cézanne was a French artist and Post-Impressionist
  painter whose work laid the foundations of the transition 
  from the 19th-century conception of artistic endeavor 
  to a new and radically different world of art in the 
  20th century.
</main>

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

<script>
  let show = () => {
    let element = document.getElementById("mymain");
    alert("Title = " + element.getAttribute('data-subject'));
  }
</script>

Code explanation

The <main> tag has a custom data-subject attribute.

The data-subject attribute stores the <main> content subject.

Clicks are handled by the onclick event.

Onclick invokes a JavaScript function that extracts and displays the <main> content subject.

Note: Notice how the main title displays immediately without server call.


Browser support

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

Chrome
6.0 Sep 2010
Firefox
4.0 Mar 2011
IE/Edge
12.0 Jul 2015
Opera
11.1 Mar 2011
Safari
5.0 Jun 2010

You may also like

 Back to <main>
Guides