A hidden attribute on an <img> tag hides the image.
Although the image is not visible, its position on the page is maintained.
A hidden attribute on an <img>.
The image is not visible.
A hidden image:
<p>A hidden image:</p>
<img src="/img/html/parliament.jpg" hidden>
The hidden attribute hides the <img> element.
You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid.
A hidden <img> is not visible, but maintains its position on the page.
<img hidden>
<img hidden="hidden">
Value | Description |
---|---|
hidden | Use value 'hidden' or no value at all. |
Clicking the button toggles A hidden attribute on the <img> element.
<img id="myimg" src="/img/html/parliament.jpg">
<br /><br />
<button onclick="toggle(this);">Hide image</button>
<script>
let toggle = button => {
let element = document.getElementById("myimg");
let hidden = element.getAttribute("hidden");
if (hidden) {
element.removeAttribute("hidden");
img.innerText = "Hide image";
} else {
element.setAttribute("hidden", "hidden");
img.innerText = "Show image";
}
}
</script>
Initially, the <img> element has no hidden attribute and can be seen.
Repeatedly clicking the button calls JavaScript that toggles the hidden attribute from the <img> element.
The hidden attribute hides the image.
Here is when hidden support started for each browser:
Chrome
|
1.0 | Sep 2008 |
Firefox
|
1.0 | Sep 2002 |
IE/Edge
|
1.0 | Aug 1995 |
Opera
|
1.0 | Jan 2006 |
Safari
|
1.0 | Jan 2003 |
Back to <img>