An id on a <picture> tag assigns an identifier to the picture.
The identifier must be unique across the page.
An id attribute on a <picture>.
<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>
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.
<picture id="identifier" />
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 (.). |
A <picture> with a unique id.
Clicking the button displays how many sources are included in the picture element.
<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>
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.
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 |
Back to <picture>