Sub topic 9
display: none
This chapter covers what display: none does to an element, how it affects layout, scripts, forms, and assistive technology, and the mistakes people commonly make with it. It assumes you know the difference between block and inline boxes from the earlier chapters, and that you can write CSS rules and small JavaScript snippets.
What display: none does
display: none tells the browser not to generate any box for the element. The element stays in the HTML and in the DOM, but it takes no part in layout. It has no width, no height, no margin, and no position in the flow. Neighbouring elements are laid out as if it were not there.
The effect covers the whole subtree. A descendant of a display: none element is not rendered either, even if you give that descendant its own display value.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>display: none</title>
<style>
.gone { display: none; }
.gone .child { display: block; } /* has no effect */
p { margin: 0 0 1rem; background: #eef; }
</style>
</head>
<body>
<p>Paragraph one</p>
<div class="gone">
<p class="child">I am inside a hidden parent.</p>
</div>
<p>Paragraph two</p>
</body>
</html>Paragraph two appears directly under paragraph one with a single 1rem gap. The hidden div leaves no empty space. The .child rule sets display: block, but the child's parent generates no box, so the child never renders.
The diagram below shows how the browser treats the elements.
How it fits with the other display values
The other values you have seen decide what kind of box an element makes. none decides whether it makes one at all. It is a different kind of value from block, inline, and inline-block. You can think of it as "no box" instead of "a box of another type".
That has a consequence for how you undo it. When you remove the rule that hides an element, the element returns to whatever display value it had before: its default from the browser stylesheet, or one set by another rule. You do not have to know that value.
Showing the element again
A common mistake is to reverse display: none by setting display: block. That works for a div. It breaks elements whose normal value is something else.
| Element | Normal display | Result of forcing `display: block` |
|---|---|---|
| `div` | `block` | Looks correct |
| `span` | `inline` | Becomes a block and breaks onto its own line |
| `li` | `list-item` | Loses its bullet or number |
| `table` | `table` | Loses table layout behavior |
The safer approach is to toggle a class that applies display: none and remove the class to show the element again. The element then falls back to its normal value.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Toggle</title>
<style>
.is-hidden { display: none; }
</style>
</head>
<body>
<button id="toggle" type="button" aria-expanded="true" aria-controls="panel">
Toggle panel
</button>
<ul id="panel">
<li>First</li>
<li>Second</li>
</ul>
<script>
const button = document.getElementById("toggle");
const panel = document.getElementById("panel");
button.addEventListener("click", () => {
const hidden = panel.classList.toggle("is-hidden");
button.setAttribute("aria-expanded", String(!hidden));
});
</script>
</body>
</html>The ul returns with its bullets because the script never sets a display value. The script also keeps aria-expanded in sync, which tells assistive technology whether the panel is open.
The hidden attribute
HTML has a built-in way to do the same thing: the hidden attribute. The browser's default stylesheet maps it to display: none.
<p hidden>This paragraph is not rendered.</p>There is a trap. The default stylesheet has very low priority, so any author rule that sets display on the same element overrides it.
<style>
.notice { display: flex; }
</style>
<div class="notice" hidden>I am still visible.</div>The .notice rule wins, and the hidden attribute has no effect. A common fix is to restore the intent explicitly:
[hidden] {
display: none !important;
}Use that rule deliberately. It stops other display rules from ever showing an element that was marked hidden.
Effects on scripts and measurements
A display: none element remains in the DOM, so scripts can still find and change it. Layout information is another matter, because no box exists.
element.offsetWidthandelement.offsetHeightreturn0.element.getBoundingClientRect()returns a rectangle of zeros.getComputedStyle(element).displayreturns"none", so you can test for it.
This matters when you need to measure an element before showing it, for example to animate a height. If you read offsetHeight while the element is hidden, you get 0. You must make the element render first, then measure it.
Hiding or showing an element also triggers a reflow, because the layout of the surrounding elements changes. For a single toggle this cost is negligible. If you toggle many elements in a loop, batch the changes. One approach is to change a class on a common ancestor.
Forms, focus, and assistive technology
Because display: none removes the element from the rendering tree, browsers also remove it from the accessibility tree. Screen readers do not announce it, and keyboard users cannot focus anything inside it. That is the correct behavior for a collapsed menu or a closed dialog. It would be a bug for content that users need to reach.
Form controls behave in a way that surprises people:
- A hidden
inputis still part of the form and is still submitted with its value. - If a hidden
inputhas therequiredattribute and is empty, the browser's validation blocks submission. The browser cannot focus the control to show an error, so the user sees nothing happen. Chrome logs a message such as "An invalid form control is not focusable."
If you hide part of a form, disable or remove the controls that no longer apply.
<form>
<label>
<input type="checkbox" id="has-pet"> I have a pet
</label>
<div id="pet-fields" style="display: none">
<input name="pet" id="pet" disabled>
</div>
<button>Send</button>
<script>
const checkbox = document.getElementById("has-pet");
const fields = document.getElementById("pet-fields");
const pet = document.getElementById("pet");
checkbox.addEventListener("change", () => {
fields.style.display = checkbox.checked ? "" : "none";
pet.disabled = !checkbox.checked;
});
</script>
</form>Setting style.display = "" clears the inline value and lets the element return to its normal display. Disabled controls are not submitted and are skipped by validation.
Resources that still load
Hiding an element does not reliably prevent its resources from downloading. Behavior differs between browsers and between element types. An img element generally still loads even when it is hidden, unless it uses loading="lazy". A CSS background image on a hidden element is usually not fetched until the element is shown. Do not use display: none as a way to save bandwidth. If you need to avoid a download, do not put the resource in the page, or load it with script when you need it.
Animation and transitions
For years, display could not be animated. Switching from none to a visible value happens at once, so a fade or slide on the same change had nothing to play on. The usual workarounds were to animate opacity or max-height and set display afterwards.
Newer browsers support discrete animation of display when you opt in:
.panel {
transition: opacity 0.3s, display 0.3s allow-discrete;
}
.panel.is-hidden {
opacity: 0;
display: none;
}With allow-discrete, the browser keeps the element rendered until the transition ends, then applies none. Entering from none also needs the @starting-style rule. Check browser support before relying on this. The older fallback is to animate opacity first, then add the hiding class in a transitionend handler.
Defaults you did not set
Many elements are display: none by default. The browser stylesheet hides head, title, meta, link, style, and script. That is why the content of a style element does not appear on the page. You can reveal it, though it is rarely useful:
style {
display: block;
white-space: pre;
}Responsive use
Media queries often use display: none to show different content at different widths.
.mobile-only { display: none; }
@media (max-width: 600px) {
.mobile-only { display: block; }
.desktop-only { display: none; }
}This works, but both versions of the content stay in the HTML, which adds page weight and can confuse search and screen reader users if the two versions differ. Prefer one responsive layout when you can.
Common mistakes
- Setting
display: blockto undonone, which breaks inline and list items. - Expecting a child with its own
displayvalue to appear inside a hidden parent. - Using the
hiddenattribute on an element that has adisplayrule in your CSS. - Hiding a required form field and leaving it enabled.
- Measuring a hidden element and getting
0. - Using
display: nonefor content that should stay available to screen readers, such as a visually hidden label. A different technique is needed there, and the next chapter compares the options.
Exercise
A page has <li class="item" hidden>Draft</li> and a stylesheet with .item { display: list-item; }. The item still appears on screen. Why, and how would you make hidden work again?
Show answer
The hidden attribute only maps to display: none in the browser's default stylesheet, which has lower priority than author CSS. The .item rule sets display, so it wins and the item stays visible. Fix it with [hidden] { display: none !important; }, or by hiding with a class such as .is-hidden { display: none; } that is placed after .item or has higher specificity.
Exercise
You call panel.offsetHeight on a panel that has display: none and get 0. You want to measure its height for an animation. What should you do?
Show answer
Make the element render before measuring, for example by removing the hiding class, then read offsetHeight. A display: none element has no box, so the browser has no height to report.
Exercise
A signup form submits nothing when the user clicks Send, and the console says an invalid form control is not focusable. One section of the form is hidden with display: none and contains an empty required input. How do you fix it?
Show answer
The browser's validation blocks the submit but cannot focus the hidden input to show the error. Disable the input (or remove it from the form) while it is hidden, and enable it again when you show the section. Disabled controls are skipped by validation and are not submitted.