为什么position:static 会忽略z-index?

发布于 2024-12-21 09:56:18 字数 566 浏览 1 评论 0原文

请参阅此评论 来自 jquery-ui

// Ignore z-index if position is set to a value where z-index is ignored by the browser
// This makes behavior of this function consistent across browsers
// WebKit always returns auto if the element is positioned

我看到如果元素是 jquery 的 zIndex() 返回 0 位置:静态

position:static 不支持 z-index 吗? (它在 Chrome 中适用于我,尚未测试跨浏览器)

See this comment from jquery-ui

// Ignore z-index if position is set to a value where z-index is ignored by the browser
// This makes behavior of this function consistent across browsers
// WebKit always returns auto if the element is positioned

I see that jquery's zIndex() returns 0 if the element is position: static.

Isn't z-index supported on position:static? (It works for me in Chrome, haven't tested cross-browser)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨死的猪 2024-12-28 09:56:18

因为 position: static 的意思是“忽略来自 lefttopz-index 等的所有定位指令。 ”。

'z-index'
Value:      auto | <integer> | inherit
Initial:    auto
Applies to:     positioned elements

http://www.w3.org/TR/CSS21/visuren.html#z-index

如果元素的“position”属性具有“static”以外的值,则称该元素已定位。

http://www.w3.org/TR/CSS21/visuren.html#positioned-element

Because position: static means "Ignore all the positioning instructions from left, top, z-index, etc.".

'z-index'
Value:      auto | <integer> | inherit
Initial:    auto
Applies to:     positioned elements

http://www.w3.org/TR/CSS21/visuren.html#z-index

An element is said to be positioned if its 'position' property has a value other than 'static'.

http://www.w3.org/TR/CSS21/visuren.html#positioned-element

超可爱的懒熊 2024-12-28 09:56:18

对于 flex-items(flex-container 的直接子元素、具有 display: flexdisplay: inline-flex 的元素,不会忽略 z-index ) 或 grid-items(grid-container 的直接子元素,具有 display: griddisplay: inline-grid 的元素)。

规格引用

W3C Flexbox 规格

Flex 项目 的绘制与内联块完全相同CSS21,除了 order - 使用修改后的文档顺序代替原始文档顺序,以及 z-indexauto 创建堆叠上下文即使 position静态


来自 W3C CSS 网格布局规范

网格项在定位到相交网格区域,甚至位于非相交时由于负边距或定位而导致的区域。 网格项的绘制顺序与内联完全相同阻止 CSS21,除了 订单修改文档顺序 用于代替原始文档顺序,以及 z-index< /a> auto 创建堆叠上下文即使 positionstatic (行为完全就像 < a href="https://www.w3.org/TR/css3-positioning/#propdef-position" rel="noreferrer">位置相对)。因此 z-index属性可以轻松地用于控制网格项的 z 轴顺序。

Flexbox 示例

因此,假设我们的布局具有重叠(.flex-item-two.flex-item-one 重叠,例如使用负边距):

.flex {
  display: flex;
  align-items: center;
}

.flex-item-one {
  width: 100px;
  height: 100px;
  background-color: red;
  margin-right: -50px;
}

.flex-item-two {
  width: 200px;
  height: 200px;
  background-color: green;
}
<div class="flex">
  <div class="flex-item flex-item-one">One</div>
  <div class="flex-item flex-item-two">Two</div>
</div>

如果 flex-item-one 索引大于 .flex-item-two 的索引,则 .flex-item-one 会与 重叠>.flex-item-two

.flex {
  display: flex;
  align-items: center;
}

.flex-item-one {
  width: 100px;
  height: 100px;
  background-color: red;
  margin-right: -50px;
  z-index: 1;
}

.flex-item-two {
  width: 200px;
  height: 200px;
  background-color: green;
}
<div class="flex">
  <div class="flex-item flex-item-one">One</div>
  <div class="flex-item flex-item-two">Two</div>
</div>

CSS 网格示例

#grid {
  display: inline-grid;
  width: 250px;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr
}

#A {
  grid-column: 1 / span 2;
  grid-row: 2;
  align-self: end;
  background-color: #4f81bd;
}

#B {
  grid-column: 1;
  grid-row: 1;
  z-index: 10;
  background-color: #8064a2;
}

#C {
  grid-column: 2;
  grid-row: 1;
  align-self: start;
  margin-left: -20px;
  background-color: #f79646;
}

#D {
  grid-column: 2;
  grid-row: 2;
  justify-self: end;
  align-self: start;
  background-color: #9bbb59;
}

#E {
  grid-column: 1 / span 2;
  grid-row: 1 / span 2;
  z-index: 5;
  justify-self: center;
  align-self: center;
  background-color: #c0504d;
}

#grid > * {
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 40px;
  font-size: 32px;
}

#C, #D {
  padding: 10px 20px;
}
<div id="grid">
  <div id="A">A</div>
  <div id="B">B</div>
  <div id="C">C</div>
  <div id="D">D</div>
  <div id="E">E</div>
</div>

z-index is not ignored for flex-items (immediate children of flex-container, element with display: flex or display: inline-flex) or grid-items (immediate children of grid-container, element with display: grid or display: inline-grid).

Specs quotation

From W3C Flexbox specs:

Flex items paint exactly the same as inline blocks CSS21, except that order-modified document order is used in place of raw document order, and z-index values other than auto create a stacking context even if position is static.

From W3C CSS Grid Layout specs:

Grid items can overlap when they are positioned into intersecting grid areas, or even when positioned in non-intersecting areas because of negative margins or positioning. The painting order of grid items is exactly the same as inline blocks CSS21, except that order-modified document order is used in place of raw document order, and z-index values other than auto create a stacking context even if position is static (behaving exactly as if position were relative). Thus the z-index property can easily be used to control the z-axis order of grid items.

Flexbox example

So assume we have this layout with overlapping (.flex-item-two overlaps .flex-item-one using e.g. negative margins):

.flex {
  display: flex;
  align-items: center;
}

.flex-item-one {
  width: 100px;
  height: 100px;
  background-color: red;
  margin-right: -50px;
}

.flex-item-two {
  width: 200px;
  height: 200px;
  background-color: green;
}
<div class="flex">
  <div class="flex-item flex-item-one">One</div>
  <div class="flex-item flex-item-two">Two</div>
</div>

If flex-item-one index is bigger than .flex-item-two's, .flex-item-one then overlaps .flex-item-two.

.flex {
  display: flex;
  align-items: center;
}

.flex-item-one {
  width: 100px;
  height: 100px;
  background-color: red;
  margin-right: -50px;
  z-index: 1;
}

.flex-item-two {
  width: 200px;
  height: 200px;
  background-color: green;
}
<div class="flex">
  <div class="flex-item flex-item-one">One</div>
  <div class="flex-item flex-item-two">Two</div>
</div>

CSS Grid example

#grid {
  display: inline-grid;
  width: 250px;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr
}

#A {
  grid-column: 1 / span 2;
  grid-row: 2;
  align-self: end;
  background-color: #4f81bd;
}

#B {
  grid-column: 1;
  grid-row: 1;
  z-index: 10;
  background-color: #8064a2;
}

#C {
  grid-column: 2;
  grid-row: 1;
  align-self: start;
  margin-left: -20px;
  background-color: #f79646;
}

#D {
  grid-column: 2;
  grid-row: 2;
  justify-self: end;
  align-self: start;
  background-color: #9bbb59;
}

#E {
  grid-column: 1 / span 2;
  grid-row: 1 / span 2;
  z-index: 5;
  justify-self: center;
  align-self: center;
  background-color: #c0504d;
}

#grid > * {
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 40px;
  font-size: 32px;
}

#C, #D {
  padding: 10px 20px;
}
<div id="grid">
  <div id="A">A</div>
  <div id="B">B</div>
  <div id="C">C</div>
  <div id="D">D</div>
  <div id="E">E</div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文