Chrome 上“display:none”元素的 offsetWidth 或 offsetHeight 为 0。
我想知道这是 W3C 定义的标准行为,还是只是浏览器的一种方言?或者它实际上是所有现代浏览器都支持的?
我在网上搜索,发现几乎没有文字解决我的问题。
最佳答案
是的,这是标准行为。
如果display
为none
,则不会创建布局框:
Please note that a display of 'none' does not create an invisible box; it creates no box at all. CSS includes mechanisms that enable an element to generate boxes in the formatting structure that affect formatting but are not visible themselves. Please consult the section on visibility for details.
如果没有盒子,则返回零:
The offsetWidth attribute must return the result of running these steps:
- If the element does not have any associated CSS layout box return zero and terminate this algorithm.
- Return the border edge width of the first CSS layout box associated with the element, ignoring any transforms that apply to the element and its ancestors.
https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetwidth
关于javascript - "display:none"元素的宽度或高度为零是标准吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56405517/