z-index 的最小值和最大值?
我的 HTML 页面中有一个 div。 我根据某些条件显示此 div,但该 div 显示在我将鼠标光标指向的 HTML 元素后面。
我已经尝试了从 0 - 999999 的所有 z-index 值。谁能告诉我为什么会发生这种情况?
CSS的Z-INDEX属性是否有最小值或最大值?
.divClass {
position: absolute;
left: 25px;
top: 25px;
width: 320px;
height: 300px;
z-index: 1000;
}
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td>
<asp:HyperLink ID="lnkProgram" runat="server"></asp:HyperLink>
</td>
</tr>
<tr>
<td>
<div class="divClass">
Some Data
</div>
</td>
</tr>
</table>
我使用 jQuery 通过
显示和隐藏 .divClass
onclick 的 div。
I have a div in my HTML page. I am showing this div based on some condition, but the div is displaying behind the HTML element where I pointed the mouse cursor.
I have tried all values for z-index from 0 - 999999. Can anyone tell me why this is happening?
Is there any minimum or maximum value of Z-INDEX property of CSS?
.divClass {
position: absolute;
left: 25px;
top: 25px;
width: 320px;
height: 300px;
z-index: 1000;
}
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td>
<asp:HyperLink ID="lnkProgram" runat="server"></asp:HyperLink>
</td>
</tr>
<tr>
<td>
<div class="divClass">
Some Data
</div>
</td>
</tr>
</table>
I am showing and hiding the div with .divClass
onclick via the <asp:hyperlink>
using jQuery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
http://www.w3.org/TR/CSS21/visuren.html #z-索引
http://www.w3.org/TR/CSS21/syndata.html#数字
所以基本上 CSS 标准中的 z-index 值没有限制,但我猜大多数浏览器在实践中将其限制为带符号的 32 位值(−2147483648 到 +2147483647)(64 有点偏离顶部,而且它如今使用小于 32 位的任何东西都没有意义)
http://www.w3.org/TR/CSS21/visuren.html#z-index
http://www.w3.org/TR/CSS21/syndata.html#numbers
So basically there are no limitations for z-index value in the CSS standard, but I guess most browsers limit it to signed 32-bit values (−2147483648 to +2147483647) in practice (64 would be a little off the top, and it doesn't make sense to use anything less than 32 bits these days)
我的测试显示
z-index: 2147483647
是最大值,在 OS X 的 FF 3.0.1 上测试。我发现了一个整数溢出错误:如果您输入
z-index: 2147483648
(即 2147483647 + 1),该元素就会排在所有其他元素后面。 至少浏览器不会崩溃。我们要吸取的教训是,您应该注意不要为
z-index
属性输入太大的值,因为它们会环绕。My tests show that
z-index: 2147483647
is the maximum value, tested on FF 3.0.1 for OS X.I discovered a integer overflow bug: if you type
z-index: 2147483648
(which is 2147483647 + 1) the element just goes behind all other elements. At least the browser doesn't crash.And the lesson to learn is that you should beware of entering too large values for the
z-index
property because they wrap around.根据经验,我认为正确的最大 z-index 是 2147483647。
Out of experience, I think the correct maximum
z-index
is 2147483647.它是 32 位整数的最大值:2147483647
另请参阅文档:https ://www.w3.org/TR/CSS22/visuren.html#z-index
(允许负数)
It's the maximum value of a 32 bits integer: 2147483647
Also see the docs: https://www.w3.org/TR/CSS22/visuren.html#z-index
(Negative numbers are allowed)
结论 最大 z-index 值为 2,147,483,647,超过此值将转换为 2,147,483,647
BrowserStack 中测试的所有值。
Conclusion Maximum z-index value is 2,147,483,647 and more than this convert to 2,147,483,647
All Values tested in BrowserStack.
这取决于浏览器(尽管所有浏览器的最新版本的最大值应为 2147483638),以及超出最大值时浏览器的反应。
It depends on the browser (although the latest version of all browsers should max out at 2147483638), as does the browser's reaction when the maximum is exceeded.
Z-Index 仅适用于应用了
position:relative;
或position:absolute;
的元素。 如果这不是问题,我们需要查看示例页面以获取更多帮助。编辑:好医生已经给出了最完整的解释,但快速版本是最小值为 0,因为它不能是负数,而最大值 - 好吧,对于大多数设计来说,你永远不需要超过 10。
Z-Index only works for elements that have
position: relative;
orposition: absolute;
applied to them. If that's not the problem we'll need to see an example page to be more helpful.EDIT: The good doctor has already put the fullest explanation but the quick version is that the minimum is 0 because it can't be a negative number and the maximum - well, you'll never really need to go above 10 for most designs.
我发现,如果 z-index 不起作用,通常是因为它的父级/兄弟级没有指定的 z-index。
因此,如果您有:
项目#3,甚至#4,可能会争夺#2的点击/悬停空间,但如果您将#1设置为z-index 0,则z-index的兄弟姐妹现在将它们放入独立的堆栈中位于同一个堆栈中并且将正确地进行 z 索引。
这有一个有用且相当人性化的描述:http://foohack.com/2007/10/top- 5-css-错误/
I have found that often if z-index isn't working its because its parent/siblings don't have a specified z-index.
So if you have:
item #3, or even #4, may be contesting #2 for the click/hover space, though if you set #1 to z-index 0, the siblings who's z-index put them in independant stacks now are in the same stack and will z-index properly.
This has a helpful and fairly humanized description: http://foohack.com/2007/10/top-5-css-mistakes/
上面的一位用户说:“好吧,对于大多数设计来说,你永远不需要超过 10。”
根据您的项目,您可能只需要 z 索引 0-1 或 z 索引 0-10000。 您经常需要玩更高的数字......特别是如果您使用灯箱查看器(9999 似乎是标准,如果您想超越他们的 z-index,您需要超过这个数字!)
A user above says "well, you'll never really need to go above 10 for most designs."
Depending on your project, you may only need z-indexes 0-1, or z-indexes 0-10000. You'll often need to play in the higher digits...especially if you are working with lightbox viewers (9999 seems to be the standard and if you want to top their z-index, you'll need to exceed that!)
有两种超级简单的方法可以使用
calc()
编写z-index
的最大值和最小值。最大值
要写入最大值,请执行以下操作:
最小值
要写入最小值,请执行以下操作:
calc()
浏览器兼容性(
值支持)表There are two super easy ways to write maximal and minimal value for
z-index
usingcalc()
.Maximal Value
To write maximal value, do this:
Minimal Value
To write minimal value, do this:
calc()
Browser compatibility (<number>
value support) table更现代和正确的方法是使用它:
所有主要浏览器都支持此功能: https:// caniuse.com/mdn-css_types_calc-constant_infinity
A more modern and proper approach would be to use this:
This is supported in all major browsers: https://caniuse.com/mdn-css_types_calc-constant_infinity
虽然 INT_MAX 可能是最安全的选择,但 WebKit 显然在内部使用了双精度数,因此允许非常大的数字(达到一定的精度)。
LLONG_MAX
例如工作正常(至少在 64 位 Chromium 和 WebkitGTK 中),但将四舍五入为 9223372036854776000。(尽管您应该仔细考虑您是否真的、真的需要这个许多 z 索引...)。
While
INT_MAX
is probably the safest bet, WebKit apparently uses doubles internally and thus allows very large numbers (to a certain precision).LLONG_MAX
e.g. works fine (at least in 64-Bit Chromium and WebkitGTK), but will be rounded to 9223372036854776000.(Although you should consider carefully whether you really, really need this many z indices…).
不要使用最大/最小值,只需尝试:
1. 组织 HTML 标签顺序
z-index
组织堆叠顺序2. 创建新的堆栈上下文以隔离 z-index
z-index: 0
隔离堆叠上下文屏幕你不需要 z-index
z-索引是危险的:
z-index: 1
;,更多标签将不得不使用z-index: 2;
,z-index: 3 ;
...z 索引:最大 || 分钟;
,繁荣!使用 z 索引:max || min; 特别危险。 如果一个标签获得了 z-index: max ,它将很难被其他标签覆盖,整个项目将逐渐失控。
Do NOT use max/min value, simply try:
1. Organize HTML tag order
z-index
to organize stacking order2. Create new The stacking context to isolate z-index
z-index: max || min
to override the other tagz-index: 0
to isolate The stacking contextYou don't need z-index
z-index is dangerous:
z-index: 1
;, more tags will have to usez-index: 2;
,z-index: 3;
...z-index: max || min;
, Boom!.Use
z-index: max || min;
is especially dangerous. If one tag gets z-index: max , it will be hard to override by others, the whole project will out of control gradually.