HTML <figure> data-* Attribute

The data-* attribute on a <figure> tag attaches additional data to the figure. 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 <figure>.
The attribute value is not visible, but it is readable by JavaScript.

<figure data-title="San Giorgio">
  <img src="/img/html/sangiorgio.jpg">
</figure>

Using data-*

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

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

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

These attributes usually store additional data about the figure (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 figure tag in any way.


Syntax

<figure 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-artist attribute on a <figure> element.
Clicking the button will display the artist value.


<figure id="myfigure" data-artist="Claude Monet">
  <img src="/img/html/sangiorgio.jpg">
</figure>

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

<script>
  let show = () => {
    let element = document.getElementById("myfigure");
    alert("Artist = " + element.getAttribute('data-artist'));
  }
</script>

Code explanation

The <figure> tag has a custom data-artist attribute.

The data-artist atttribute holds the figure artist.

Clicks are handled by the onclick event.

Onclick invokes a JavaScript function that extracts and displays the figure artist in an alert box.

Note: Notice how the 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
9.0 Mar 2011
Opera
11.1 Mar 2011
Safari
5.0 Jun 2010

You may also like

 Back to <figure>
Guides