A data-* attribute on a <main> tag attaches additional data to the main element.
To create a custom attribute, replace * with a lowercase string, such as data-id
, data-status
, or data-location
.
A custom data-title
attribute on a <main> element.
The attribute value is not visible, but it is readable by JavaScript.
<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>
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.
<main data-*="value">
Note: The * can be any string, such as data-id
, data-cost
, data-supplier
, etc.
Value | Description |
---|---|
value | A string value. Can be numeric, alphanumeric, JSON, etc. |
A custom data-subject
attribute on a <main> element.
Clicking the button will display the subject value.
<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>
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.
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 |
Back to <main>