Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest self-service freelancing marketplace for people like you.

HTML <picture> id Attribute

An id on a <picture> tag assigns an identifier to the picture.

The identifier must be unique across the page.

Example

#

An id attribute on a <picture>.

Vincent Van Gogh
<picture id="picture-vangogh">
  <source media="(max-width: 520px)" srcset="/img/html/vangogh-sm.jpg">
  <source media="(max-width: 768px)" srcset="/img/html/vangogh.jpg">
  <img src="/img/html/vangogh-lg.jpg" alt="Vincent Van Gogh">
</picture>

Using id

The id attribute assigns an identifier to the <picture> element.

The id allows JavaScript to easily access the <picture> element.

It is also used to point to a specific id selector in a style sheet.

Tip:  id is a global attribute that can be applied to any HTML element.


Syntax

<picture id="identifier" />

Values

#

Value Description
identifier A unique alphanumeric string. The id value must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens (-), underscores (_), colons (:), and periods (.).

More Examples

A <picture> with a unique id.
Clicking the button displays how many sources are included in the picture element.

Vincent Van Gogh

<picture id="mypicture">
  <source media="(max-width: 520px)" srcset="/img/html/vangogh-sm.jpg">
  <source media="(max-width: 768px)" srcset="/img/html/vangogh.jpg">
  <img src="/img/html/vangogh-lg.jpg" alt="Vincent Van Gogh">
</picture>

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

<script>
  let show = () => {
    let element = document.getElementById("mypicture");
    let sourceCount = element.getElementsByTagName("source").length;
    alert("Source count = " + sourceCount);
  }
</script>

Code explanation

The id attribute assigns a unique identifier for the <picture>.

Clicking the button calls JavaScript which locates the <picture> through the id.

It then counts all the <source> element inside the <picture> and displays it in an alert box.


Browser support

Here is when id support started for each browser:

Chrome
38.0 Oct 2014
Firefox
38.0 May 2015
IE/Edge
13.0 Nov 2015
Opera
25.0 Oct 2014
Safari
9.1 Mar 2016

You may also like

 Back to <picture>
Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest self-service freelancing marketplace for people like you.

Guides