简体   繁体   English

hidden 属性和 display:none 规则 (CSS) 有什么区别?

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

HTML has an attribute, hidden , which can be used to hide content. HTML 有一个属性hidden ,可以用来隐藏内容。

<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 and return the space that content would have taken back to the document flow. CSS 具有display:none规则,该规则也可用于隐藏内容并将内容本应占用的空间返回到文档流。

article { display:none; }

Visually, they appear identical.从视觉上看,它们看起来是一样的。 Is there any difference semantically?在语义上有什么区别吗? Computationally?在计算上?

What guidelines should I consider on when to use one or the other?关于何时使用其中一个或另一个,我应该考虑哪些准则?

The key difference seems to be that hidden elements are always hidden regardless of the presentation:关键的区别似乎是hidden的元素总是隐藏的,无论呈现方式如何:

The hidden attribute must not be used to hide content that could legitimately be shown in another presentation. hidden 属性不得用于隐藏可以在另一个演示文稿中合法显示的内容。 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.例如,在选项卡式对话框中使用 hidden 来隐藏面板是不正确的,因为选项卡式界面只是一种溢出呈现——同样可以在一个带有滚动条的大页面中显示所有表单控件。 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 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.由于 CSS 可以针对不同的媒体/演示文稿类型,因此display: none将取决于给定的演示文稿。 Eg some elements might have display: none when viewed in a desktop browser, but not a mobile browser.例如,在桌面浏览器中查看时,某些元素可能会display: none ,但在移动浏览器中则不会。 Or, be hidden visually but still available to a screen-reader.或者,在视觉上隐藏但仍然可供屏幕阅读器使用。

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 .使用hidden

Are you hiding something that is part of the page content, such as toggling a paragraph into a collapsed state to avoid clutter?您是否隐藏了属于页面内容的一部分,例如将段落切换到折叠的 state 以避免混乱? Use display:none .使用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. hidden是关于语义(当前是否是页面内容的一部分)和display: none是关于页面内容的呈现。

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.不幸的是, hidden不会覆盖任何display CSS,即使不应该显示不属于页面的内容在直觉上也是如此。 If you want hidden to be respected, add this css rule: [hidden] { display: none !important }如果您希望hidden得到尊重,请添加此 css 规则: [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.使用hidden表示在填写表格之前不应作为页面一部分存在的“谢谢”消息。

  2. Use hidden for a series of potential error messages that could be shown to the user depending on their actions on the page.对一系列潜在的错误消息使用hidden ,这些错误消息可能会根据用户在页面上的操作显示给用户。 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.使用display: none用于仅在用户悬停或单击菜单按钮时显示的导航。

  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.display: none用于选项卡式窗格,其中选项卡式窗格的唯一原因是同时向用户显示所有窗格会过于庞大。 (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). (也许如果用户有足够宽的屏幕,所有窗格都会显示。因此,窗格始终是页面内容的一部分,因此 CSS 表示逻辑是合适的选择)。

  5. Use display: none to collapse a paragraph or section inside a document.使用display: none折叠文档中的段落或部分。 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.注意:可访问性设备将受益于了解导航或存在但当前未显示的内容与当前不被视为页面一部分且因此不应向用户描述的内容之间的区别。

What is the difference between the hidden attribute (HTML5) and the display:none rule (CSS)? hidden属性 (HTML5) 和display:none规则 (CSS) 有什么区别?

MDN confirms that: MDN确认:

Changing the value of the CSS display property on an element with the hidden attribute overrides the behavior.在具有hidden属性的元素上更改 CSS display属性的值会覆盖该行为。

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> a) <p hidden>
  • b) <p class="hidden"> //.hidden {display: none;} b) <p class="hidden"> //.hidden {display: none;}

Adding Accessibility:添加辅助功能:

However ... 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"> //.hidden {display: none;} b) <p class="hidden" aria-hidden="true"> //.hidden {display: none;}

Why is using a class="hidden" and aria-hidden="true" better than using hidden ?为什么使用class="hidden"aria-hidden="true"比使用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.因为我们知道CSS 可以使用级联或特异性覆盖,而且我们知道aria-hidden与屏幕阅读器等可访问性用户代理有关。

By contrast, the HTML5 hidden attribute is much sketchier.相比之下,HTML5 hidden属性要粗略得多。 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参见: https://meowni.ca/hidden.is.a.lie.html


Final Note:最后注:

Whatever combination of HTML, CSS and ARIA you go with, note that:无论 HTML、CSS 和 ARIA 与 go 的组合如何,请注意:

We already have 6 methods for hiding content with different purposes/intentions.我们已经有 6 种方法来隐藏具有不同目的/意图的内容。

  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"在表单数据的情况下, input type="hidden"

Source: https://davidwalsh.name/html5-hidden#comment-501242资料来源: https://davidwalsh.name/html5-hidden#comment-501242

Both of these hide an element but difference is connected with layout with display:none an element will be shown as if there is no anything but when it comes to visibility: hidden it also hides an element but affects layout这两者都隐藏了一个元素,但区别与布局有关,显示:无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM