A hidden attribute on a <picture> tag hides the picture.
Although the picture is not visible, its position on the page is maintained.
A hidden attribute on a <picture> tag.
The picture is not visible.
A hidden picture element:
<p>A hidden picture element:</p>
<picture hidden>
<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 hidden attribute hides the <picture> element.
You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid.
A hidden <picture> element is not visible, but it maintains its position on the page.
Removing the hidden attribute makes it re-appear.
<picture hidden>
<picture hidden="hidden">
Value | Description |
---|---|
hidden | Use 'hidden' or hidden='hidden'. Both are valid. |
Clicking the button toggles A hidden attribute on 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="toggle(this)">Hide picture</button>
<script>
let toggle = button => {
let element = document.getElementById("mypicture");
let hidden = element.getAttribute("hidden");
if (hidden) {
element.removeAttribute("hidden");
button.innerText = "Hide picture";
} else {
element.setAttribute("hidden", "hidden");
button.innerText = "Show picture";
}
}
</script>
Initially, the <picture> is visible.
Clicking the button calls JavaScript which toggles the hidden attribute.
With the hidden attribute the picture is invisible.
Here is when hidden 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 |