A hidden attribute on a <label> tag hides the label.
Although the label is not visible, its position on the page is maintained.
A hidden attribute on a <label>.
The second label is not visible.
A hidden label:
<p>A hidden label:</p>
<label hidden>First name</label>
The hidden attribute hides the <label> element.
You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid.
A hidden <label> element is not visible, but it maintains its position on the page.
Removing the hidden attribute makes it re-appear.
<label hidden>
<label hidden="hidden">
Value | Description |
---|---|
hidden | Use 'hidden' or hidden='hidden'. Both are valid. |
Clicking the button toggles A hidden attribute on the <label>.
<form>
<label id="mylabel" for="firstname">First name</label>
<input type="text" id="firstname" name="firstname">
</form>
<br />
<button onclick="toggle(this)">Hide label</button>
<script>
let toggle = button => {
let element = document.getElementById("mylabel");
let hidden = element.getAttribute("hidden");
if (hidden) {
element.removeAttribute("hidden");
button.innerText = "Hide label";
} else {
element.setAttribute("hidden", "hidden");
button.innerText = "Show label";
}
}
</script>
Initially, the <label> tag has no hidden attribute and is visible.
Clicking the button calls JavaScript that toggles the hidden attribute.
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 <label>