css索引_CSS中的Z索引:它是什么以及它做什么

css索引

什么是Z索引? (What is a Z Index?)

Z Index (z-index) is a CSS property that defines the order of overlapping HTML elements. Elements with a higher index will be placed on top of elements with a lower index.

Z索引( z-index )是一个CSS属性,用于定义重叠HTML元素的顺序。 索引较高的元素将放置在索引较低的元素之上。

Note: Z index only works on positioned elements (position:absolute, position:relative, or position:fixed).

注意 :Z索引仅适用于定位的元素( position:absoluteposition:relativeposition:fixed )。

可能的值 (Possible Values)
/* Default value if not specified */
z-index: auto;

/* Integer values */
z-index: 1;
z-index: 100;
z-index: 9999;
z-index: -1;

/* Global values */
z-index: inherit;
z-index: initial;
z-index: unset;

如何使用Z索引 (How to use the Z Index )

In this example, you can see three boxes displayed on top of each other in different orders using z-index.

在此示例中,您可以使用z-index看到三个框,它们以不同的顺序彼此重叠显示。

HTML

HTML

<div class="container">
  <div class="box" id="blue"></div>
  <div class="box" id="red"></div>
  <div class="box" id="green"></div>
</div>

CSS

CSS

#blue {
  background-color: blue;
}

#red {
  background-color: red;
}

#green {
  background-color: green;
}

Since z-index wasn’t defined, it will have a default value of auto. This is a result:

由于未定义z-index ,因此它将具有默认值auto 。 结果是:

Try to change the order to Green, Blue, Red in CSS using z-index.

尝试使用z-index在CSS中将顺序更改为绿色,蓝色,红色。

#blue {
  background-color: blue;
  z-index: 2;
}

#red {
  background-color: red;
  z-index: 1;
}

#green {
  background-color: green;
  z-index: 3;
}

Your result will be:

您的结果将是:

Use Z Index if you need to put a background element below a container. You can easily place the background under every element by giving it a negative Z Index like below:

如果需要在容器下面放置背景元素,请使用Z索引。 您可以通过为负负Z索引轻松地将背景放置在每个元素下,如下所示:

#background {
  z-index: -1;
}

翻译自: https://www.freecodecamp.org/news/z-index-in-css-what-it-is-and-what-it-does/

css索引

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值