What is the difference between the hidden attribute (HTML5) and the display:none rule (CSS)?

CssHtmlVisibility

Css Problem Overview


HTML5 has a new global attribute, hidden, which can be used to hide content.

<article hidden>
   <h2>Article #1</h2>
   <p>Lorem ipsum ...</p>
</article>

CSS has the display:none rule, which can also be used to hide content.

article { display:none; }

Visually, they are identical. What is the difference semantically? Computationally?

What guidelines should I consider on when to use one or the other?

TIA.

EDIT: Based on @newtron's responses (below), I did more searching. The hidden attribute was hotly contested last year and (apparently) barely made it into the HTML5 spec. Some argued it was redundant and had no purpose. From what I can tell, the final evaluation is this: If I'm targeting only web browsers, there is no difference. (One page even asserted that web browsers used display:none to implement the hidden attribute.) But if I'm considering accessibility (e.g., perhaps I expect my content to be read by screen-readers), then there is a difference. The CSS rule display:none might hide my content from web browsers, but a corresponding aria rule (e.g., aria-hidden="false") might try to read it. Thus, I now agree that @newtron's answer is correct, though perhaps (arguably) not as clear as I might like. Thanks @newtron for your help.

Css Solutions


Solution 1 - Css

The key difference seems to be that hidden elements are always hidden regardless of the presentation:

> The hidden attribute must not be used to hide content that could legitimately be shown in another presentation. For example, it is incorrect to use hidden to hide panels in a tabbed dialog, because the tabbed interface is merely a kind of overflow presentation — one could equally well just show all the form controls in one big page with a scrollbar. It is similarly incorrect to use this attribute to hide content just from one presentation — if something is marked hidden, it is hidden from all presentations, including, for instance, screen readers.

<http://dev.w3.org/html5/spec/Overview.html#the-hidden-attribute>

Since CSS can target different media/presentation types, display: none will be dependent on a given presentation. E.g. some elements might have display: none when viewed in a desktop browser, but not a mobile browser. Or, be hidden visually but still available to a screen-reader.

Solution 2 - Css

Simple rule:

Are you hiding something because it's not yet semantically part of the page content, like a series of potential error messages that have not been triggered yet? Use hidden.

Are you hiding something that is part of the page content, such as toggling a paragraph into a collapsed state to avoid clutter? Use display:none.

hidden is about semantics (whether something is currently part of the page content) and display: none is about presentation of the page content.

Unfortunately, hidden will NOT override any display CSS, even though it would make intuitive sense that something that is not part of the page should never be displayed. If you want hidden to be respected, add this css rule: [hidden] { display: none !important }

Examples:

  1. Use hidden for a "thank you" message that should not exist as part of the page until a form has been filled in.

  2. Use hidden for a series of potential error messages that could be shown to the user depending on their actions on the page. These errors are not semantically part of the page content until an error has occurred.

  3. Use display: none for navigation that is only shown when a user hovers or clicks a menu button.

  4. Use display: none for tabbed panes, where the only reason for the tabbed panes is that it would be too overwhelming to show the user all of the panes simultaneously. (Perhaps if a user had a wide enough screen, all panes would be shown. Therefore the panes were always part of the content of the page, and so CSS presentation logic is the appropriate choice).

  5. Use display: none to collapse a paragraph or section inside a document. This indicates the paragraph is still part of the page content, but we've hidden it for convenience to the user.

Note: Accessibility devices would benefit from knowing the difference between navigation or content that is present but not currently displayed, vs content that is not currently considered to be part of the page and that should therefore never be described to the user.

Solution 3 - Css

> What is the difference between the hidden attribute (HTML5) and the > display:none rule (CSS)?

MDN confirms that:

> Changing the value of the CSS display property on an element with the > hidden attribute overrides the behavior.

And we can show this straightforwardly:

.hidden {
  display:none;
}

.not-hidden {
  display: block
}

<p hidden>Paragraph 1: This content is not relevant to this page right now, so should not be seen. Nothing to see here. Nada.</p>
<p class="hidden">Paragraph 2: This content is not relevant to this page right now, so should not be seen. Nothing to see here. Nada.</p>
<p hidden class="not-hidden">Paragraph 3: This content is not relevant to this page right now, so should not be seen. Nothing to see here. Nada.</p>
<p class="hidden not-hidden">Paragraph 4: This content is not relevant to this page right now, so should not be seen. Nothing to see here. Nada.</p>

This reveals that there is zero presentational difference between:

  • a) <p hidden>
  • b) <p class="hidden"> where .hidden {display: none;}

So... hidden and class="hidden" are interchangeable, right?

Wrong.


Adding Accessibility:

There's more to consider than visual presentation. We should also all be catering to screen-readers to maximise accessibility (right?), so the second option immediately above should, more properly, look like this:

  • b) <p class="hidden" aria-hidden="true"> where .hidden {display: none;}

Why is using a class="hidden" and aria-hidden="true" better than using hidden?

Because we know that CSS can be over-ridden using either the cascade or specificity and we know that aria-hidden speaks to accessibility user-agents like screen-readers.

By contrast, the HTML5 hidden attribute is much sketchier. It doesn't say explicitly what it does or doesn't do - and, worse, it appears to suggest it does things it actually doesn't.

See: https://meowni.ca/hidden.is.a.lie.html


Final Note:

Whatever combination of HTML, CSS and ARIA you go with, note that:

> We already have 6 methods for hiding content with different > purposes/intentions. > 1. display: none; > 2. visibility: hidden; (don't show, but still hold the space) > 3. opacity: 0; > 4. width: 0; height: 0; > 5. aria-hidden="true" > 6. and in the case of form data, input type="hidden" > > Source: https://davidwalsh.name/html5-hidden#comment-501242

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjames.garrissView Question on Stackoverflow
Solution 1 - CssnewtronView Answer on Stackoverflow
Solution 2 - CssAndrew ParksView Answer on Stackoverflow
Solution 3 - CssRounin - Standing with UkraineView Answer on Stackoverflow