首页 > 其他分享 >display: none与visibility: hidden的区别

display: none与visibility: hidden的区别

时间:2023-09-27 15:46:07浏览次数:33  
标签:none 盒子 元素 visibility hidden display

display: none与visibility: hidden的区别

引言:

在前端面试中,一般比较侧重JavaScript方面的考察,CSS布局方面考察的内容会相对少一些,其中display: none与visibility: hidden的区别是较常见的考点,这两个css配置都可以从视觉上隐藏DOM元素,那这两者的使用上有什么区别呢?

display: none

首先我们来看W3C中对display: none的描述:

'none'

​ The element and its descendants generate no boxes or text sequences.

​ Similarly, if a text node is defined to behave as display: none, it generates no text sequences.

Elements with either of these values do not have inner or outer display types, because they don’t generate any boxes at all.

NOTE: As these values cause affected elements to not generate a box, anonymous box generation rules will ignore the elided elements entirely, as if they did not exist in the box tree.

简单翻译一下:

元素及其后代不生成盒子或文本序列。

同样,如果文本节点被定义为display: none,它就不会生成文本序列。

具有上述任一值(指none和contents,W3C的文档中这两者放在同一小节)的元素都没有内部或外部显示类型,因为它们根本不会生成任何盒子。

注意:由于这些值会导致受影响的元素不生成盒子,因此匿名盒子生成规则会完全忽略被省略的元素,就好像它们不存在于盒子树中一样。

根据上面这段描述可以看出,给元素设置display: none后,该元素及其后代都不会生成盒子和文本序列,也就是在渲染树上不会有这个元素对应的节点。

visibility: hidden

首先我们看visibility属性本身的描述:

The visibility property specifies whether the box is rendered. Invisible boxes still affect layout.

意思是:

可见性属性指定是否渲染盒子。不可见的盒子仍会影响布局。

接着继续看visibility: hidden的描述:

hidden

Any boxes generated by the element are invisible. Descendants of the element can, however, be visible if they have visibility: visible.

意思是:

该元素生成的任何盒子都是不可见的。然而,该元素的后代如果设置为visibility: visible,则就是可见的。

根据上面这段描述,可以看出,给元素设置为visibility: hidden后,该元素也会生成盒子,所以仍会影响布局,只是不可见(没有被绘制),并且后代元素可以控制自己是否可见;也就是说后代是可能被显示到页面上的,因此渲染树上会有其对应的节点,只是这个元素本身是不可见的,类似透明。

我们可以看到W3C中还有下面一段描述:

Invisible boxes are not rendered (as if they were fully transparent), cannot be interacted with (and behave as if they had pointer-events: none), are removed from navigation (similar to display: none), and are also not rendered to speech (except when speak is always [CSS-SPEECH-1]). However, as with display: contents, their semantic role as a container is not affected, to ensure that any visible descendants are properly interpreted.

翻译一下意思是:

隐形盒子不会被呈现(就像完全透明一样)、无法与之交互(行为类似于设置了pointer-events: none)、被从导航中移除(类似于display: none),也不会呈现为语音(除非speak被设置为always [CSS-SPEECH-1])。不过,与display: contents一样,它们作为容器的语义作用不会受到影响,以确保任何可见后代都能得到正确的解释。

我看了一下,大概意思差不多理解,但是其中“被从导航中移除”这个不太理解,原本以为是设置锚点不能跳转,但是尝试了一下,发现是可以的,所以又查阅了一下MDN的说法:MDN-visibility

hidden

The element box is invisible (not drawn), but still affects layout as normal. Descendants of the element will be visible if they have visibility set to visible. The element cannot receive focus (such as when navigating through tab indexes).

大致意思就是:

不能被聚焦(比如通过tabindex这个属性)。

tabindex这个属性可以使HTML元素获得焦点,比如:

image

我们可以通过tab键使设置了tabindex的元素获得焦点,也可以直接点击这个元素使它获得焦点;那么也就是说如果元素设置了visibility: hidden,即使设置了tabindex属性,也无法获取焦点。

另外后面这段内容,我觉得也可以帮助理解,

Using a visibility value of hidden on an element will remove it from the accessibility tree. This will cause the element and all its descendant elements to no longer be announced by screen reading technology.

它的意思是,设置了hidden的元素将从可访问树上被移除,其中的内容无法被无障碍阅读设备读取,总体来说,就是将这个被设置了visibility: hidden的元素当做完全不存在,直接跳过。

两者对比

从上述的定义中,可以看出,两者在渲染时,主要有两个区别:

  • 第一,是对后代元素是否可见的可控性,visibility: hidden无法完全控制后代的可见性
  • 第二,是是否参与布局计算,从定义中完全可以看出,元素即使设置了visibility: hidden,依旧会生成盒子,会参与布局的计算

那么在使用上要怎么选择呢?

首先我们可以考虑,是否由元素完全控制其后代的可见性,如果答案是否,那么display: none就可以排除了。

其次我们还要考虑一件事,就是通常来说,如果某个元素页面上不需要展示,我们直接可以不写,但既然我们写了,那么这个隐藏元素就可能出现在页面上,也就是说显示/隐藏的状态会发生切换。

这个时候我们就要考虑回流重排的问题,因为元素在设置display: none时不参与布局的计算,在状态切换为显示时,又会参与布局,这就会使渲染树的节点发生改变,导致触发浏览器回流并重新生成渲染树;频繁的切换状态就会导致频繁地触发回流重排,影响页面渲染性能,所以在有状态切换的场景,尤其是频繁切换,更推荐优先使用visibility: hidden

并且使用visibility: hidden还可以设置一些过渡的显示效果(transition)。

还要注意一个问题,就是元素设置visibility: hidden后,就无法与之交互,如果遇到一些特殊的业务需求,比如需要与不可见元素发生交互,或者能够被无障碍阅读设备读取,这个时候就不能这样用了,这个时候可以考虑使用opacity: 0,将元素的透明度设置为0,这样除了变成完全透明,其他方面与普通元素没什么不同。

display: none这么多缺点,是不是就要抛弃不用呢? 那倒也不是这么绝对,比如有种情况,在页面加载的时候,根据某些值来判断是否显示某个元素,并且后续基本很少切换状态,那简单使用display: none也是没有问题的。

标签:none,盒子,元素,visibility,hidden,display
From: https://www.cnblogs.com/beckyyyy/p/17732843.html

相关文章

  • ERROR: Could not find a version that satisfies the requirement selunium (from ve
    错误信息ERROR:Couldnotfindaversionthatsatisfiestherequirementselenium(fromversions:none)ERROR:Nomatchingdistributionfoundforselenium解决方案方法1:增大超时时间pip--default-timeout=100installselenium方法2:修改安装源为清华安装源pipi......
  • Python dataclass 如何让传入的 field 为 None 时自动转化为默认值?
    一般dataclass的字段可以设置default或default_factory生成默认值,当传入参数时,默认值不会生效。但是,有些情况下受限于外部调用环境,某些参数缺失时,以None的形式存在,而非创建dataclass实例时不传入参数。这种情况下,可能会希望传入的None被识别到并转化为默认值。fromd......
  • 解决:[email protected] requires a peer of node-sass@^4.0.0 but none is installed.
    参考:https://blog.csdn.net/hancoder/article/details/113821646去https://github.com/sass/npnode-sass或者https://github.com/sass/node-sass/releases都可以看到node和node-sass对应的版本信息npmconfigsetregistryhttp://registry.npm.taobao.org/npminstall......
  • c# winfrom窗体设置无边框后修改窗体大小 FormBorderStyle设置none后修改窗体大小
    //窗体缩放constintGuying_HTLEFT=10;constintGuying_HTRIGHT=11;constintGuying_HTTOP=12;constintGuying_HTTOPLEFT=13;constintGuying_HTTOPRIGHT=14;constintGuying_HTBOTTOM=15;co......
  • 17 display 和 浮动
    display可以使得行内元素和块元素相互转换,但不常用,用float!<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><style>/*inline:block:i......
  • python报错:pyglet.canvas.xlib.NoSuchDisplayException: Cannot connect to "None"
    运行python代码报错:       问题发现:问题其实十分的狗血,这个代码是在服务器上运行的,运行之前其实并没有看具体的代码情况,gitclone下载下来就直接运行了,原来这个代码需要进行图片绘制,说直白些就是需要显示屏,于是解决方法也十分简单,就是换个带桌面的电脑或者使用......
  • EMPIRE: LUPINONE
    Download:https://download.vulnhub.com/empire/01-Empire-Lupin-One.zipDescriptionDifficulty:MediumThisboxwascreatedtobemedium,butitcanbehardifyougetlost.CTFlikebox.Youhavetoenumerateasmuchasyoucan.ForhintsdiscordServer(h......
  • Python中的​​display​​​函数 from IPython.display import display
    Python中的display函数通常与JupyterNotebook或其他交互式开发环境一起使用,用于显示各种类型的数据,包括文本、图像、音频、视频等。这个函数通常是由IPython.display模块提供的,主要用于创建富媒体输出,以便在笔记本中直观地呈现数据。以下是有关display函数的一些重要信息:导入模块:......
  • display和浮动
    div:块级元素span:行内元素span  display:block        display:inline-block  既是行内元素,也是块级元素inline可以把div转为行内 浮动,可以左右浮动两个向左,两个向右 clear:清除浮动 因为两侧都不允许有浮动元素,所以layer4另起一行: ......
  • python selenium报错ValueError: Timeout value connect was <...>, but it must be an
    最近学习爬虫,安装selenium,很简单地执行代码,但是一直报错。importtimeimportopenpyxlfromseleniumimportwebdriverfromselenium.webdriver.common.keysimportKeysfromselenium.webdriver.common.byimportByfromselenium.webdriver.chrome.serviceimportService......