一、z-index解释

z-index属性决定了一个HTML元素的层叠级别,元素层叠级别是相对于元素在Z轴上(与X轴Y轴相对照)的位置而言。一个更高的z-index值意味着这个元素在叠层顺序中会更靠近顶部。这个层叠顺序沿着垂直的线轴被呈现。

在一个HTML页面中,自然的层叠顺序(也就是元素在Z轴上的顺序)是由很多因素决定的:

  • 具有负值的stacking contexts(层叠环境)元素,按照出现的先后顺序排列(越靠后层级越靠上);
  • 没有被定位,没有浮动的块级元素,按照出现的先后顺序排列;
  • 没有被定位,有浮动的元素,按照出现的先后顺序排列;
  • 内联元素,按照出现的先后顺序排列排列;
  • 被定位的元素,按照出现的先后顺序排列;
  • Z-index 属性,当被正确使用的时候,会改变自然的层叠顺序。

二、顺序规则

如果不对节点设定 position 属性, 位于文档流后面的节点会遮盖前面的节点.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>z-index练习</title>
<style>
div{width: 200px; height: 200px;}
.a{ background-color: red;}
.b{ background-color: green; margin-top: -100px; margin-left: 100px;}
</style>
</head>
<body>
<div class="a">A</div>
<div class="b">B</div>
</body>
</html>

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATsAAAE7CAYAAACi3CbHAAAFxElEQVR4nO3csYorZRiA4YnsFYiWp9s7sBEtrFUOWHkDgqXY2AkewQvwnNLa6lQHRL0CxcYbkO0svYg4Q7K6LEl2FSbJ5H0e+MmSYeGrXvLPn8lqPRoALtxrpx4A4BjEDkgQOyBB7ICEq71XVqsjjgEL41xvcXyyAxLEDkgQOyDh0bH7eVzTXbx355sFYDaPjt034/p0XL8Om/ABLMmjYnczbCL30bjeGderOScCmMGjYvfj9vX9cX08ru9mGwdgHo+K3efj+nb794fb1+ezjAMwjwdjd3t/7jZy18Pm3t3LuSYCmMFq7088bZ+guD19/eXOpSmAH4zrp2GztYUcT1Aszv7HxYZ/DyYmux4emw4qxA5YgoPb2NuDifWONW1lHVQAS3EwdncPJu77YvvqoAJYgr2xu38wcd90UDF9585BBbAEu2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ABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyAhNV6dOohAObmkx2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZDwNw49VLtw9mhHAAAAAElFTkSuQmCC" alt="" width="225" height="225" />

二、定位规则

2.1 如果将 position 设为 static, 位于文档流后面的节点依然会遮盖前面的节点浮动, 所以 position:static 不会影响节点的遮盖关系.

    <style>
div{width: 200px; height: 200px;}
.a{ background-color: red; position: static;}
.b{ background-color: green; margin-top: -100px; margin-left: 100px;}
</style>

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATsAAAE7CAYAAACi3CbHAAAFxElEQVR4nO3csYorZRiA4YnsFYiWp9s7sBEtrFUOWHkDgqXY2AkewQvwnNLa6lQHRL0CxcYbkO0svYg4Q7K6LEl2FSbJ5H0e+MmSYeGrXvLPn8lqPRoALtxrpx4A4BjEDkgQOyBB7ICEq71XVqsjjgEL41xvcXyyAxLEDkgQOyDh0bH7eVzTXbx355sFYDaPjt034/p0XL8Om/ABLMmjYnczbCL30bjeGderOScCmMGjYvfj9vX9cX08ru9mGwdgHo+K3efj+nb794fb1+ezjAMwjwdjd3t/7jZy18Pm3t3LuSYCmMFq7088bZ+guD19/eXOpSmAH4zrp2GztYUcT1Aszv7HxYZ/DyYmux4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gQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSLjae2W9PuIYPOjr1akngEXzyQ5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyAhNV6dOohAObmkx2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZDwNw49VLtw9mhHAAAAAElFTkSuQmCC" alt="" width="225" height="225" />

2.2 如果将 position 设为 relative (相对定位), absolute (绝对定位) 或者 fixed (固定定位), 这样的节点会覆盖没有设置 position 属性或者属性值为 static 的节点, 说明前者比后者的默认层级高.

    <style>
div{width: 200px; height: 200px;}
.a{ background-color: red; position: relative;}
.b{ background-color: green; margin-top: -100px; margin-left: 100px;}
</style>

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATUAAAE1CAYAAACGH3cEAAAFFUlEQVR4nO3csW0TYRiAYR/KKJkCJgAaKkagpqFDQpEYgLSMQEUDTABTZBfnTrYhxI5TOMaXl+eRfjm6KNJXvbq7P7+H5WgBEPHk1AMAPCRRA1LOtq4MwwnGgEfC25rZc6cGpIgakCJqQMq9Ufsxrukt27PjzwJwsHuj9nFcb8b1a7EKHMCc7Y3a1WIVs1fjejqur/9iIoAD7I3at/Xn83G9Htfno48DcJi9UXs7rk/rn1+uPy+POg7AYe6M2ub92SZm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A1JEDUgRNSBF1IAUUQNSRA1IETUgRdSAFFEDUkQNSBE1IEXUgBRRA1JEDUgRNSBF1IAUUQNSRA1IETUgRdSAFFEDUkQNSBE1IEXUgBRRA1JEDUgRNSBF1IAUUQNSRA1IETUgRdSAFFEDUkQNSBE1IEXUgBRRA1JEDUgRNSBF1IAUUQNSRA1IETUgRdSAFFEDUkQNSBE1IEXUgBRRA1JEDUgRNSBF1IAUUQNSRA1IETUgRdSAFFEDUkQNSBE1IEXUgBRRA1JEDUgRNSBF1IAUUQNSRA1IETUgRdSAFFEDUkQNSBE1IEXUgBRRA1JEDUgRNSBF1IAUUQNSRA1IETUgRdSAFFEDUkQNSBmWo1MPAfBQ3KkBKaIGpIgakCJqQIqoASnXPtU+5gHypcsAAAAASUVORK5CYII=" alt="" width="218" height="218" />

2.3 在没有 z-index 属性干扰的情况下, 根据顺序规则和定位规则, 我们可以做出更加复杂的结构. 这里我们对 A 和 B 都不设定 position, 但对 A 的子节点 A-1 设定 position:relative. 根据顺序规则, B 会覆盖 A, 又根据定位规则 A-1 会覆盖 B.

...
<style>
div{width: 200px; height: 200px;}
.a{ background-color: red;}
.a-1{width: 130px; height: 130px; background-color: blue; position: relative;}
.b{ background-color: green; margin-top: -100px; margin-left: 100px;}
</style>
</head>
<body>
<div class="a">这里是A的内容
<div class="a-1">A-1</div>
</div>
<div class="b">这里是B的内容</div>
</body>
...

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATcAAAE2CAYAAAAEftWXAAAMSUlEQVR4nO3dO48kVx3G4RrkT2DZZGS7SCAyEmsdkGIjJEdElggs2Rl24gyJRiIjWZyB5ACJyAlIltekBLackHGRvJs5M8hfYeg/U0dTW1O37pmdy7vPI5Vm+lLd1QP923Oqqtsnp3sdQJhv3fQGADwL4gZEemHy2pOTa94MuEPsybkTjNyASOIGRJqN26f75XcLK5709wG4jWbjdn+/vLew4sP98pcr35yb9Wo3HfQn3VnMnxz4eJ/2j3mZ7XnnEuvD82w2bvf2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iCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyK9MHnt6ek1bwaLfn1y01sAd46RGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEEjcgkrgBkcQNiCRuQCRxAyKJGxBJ3IBI4gZEOjndu+mNALhqRm5AJHEDIokbEEncgEjiBkQSNyDS/wBsV2E1IlWaSgAAAABJRU5ErkJggg==" alt="" width="246" height="246" />

三、参与规则

不用 position 属性, 在节点加上 z-index 属性. z-index 对节点是不起作用的.

W3C 对 z-index 属性的描述中提到 在 z-index 属性仅在节点的 position 属性为 relative, absolute 或者 fixed 时生效.

此处不做演示

四、默认值规则

如果所有节点都定义了 position:relative. z-index 为 0 的节点与没有定义 z-index 在同一层级内没有高低之分;

但 z-index 大于等于 1 的节点会遮盖没有定义 z-index 的节点; z-index的值为负数的节点将被没有定义 z-index 的节点覆盖.

...
<style>
div{width: 200px; height: 200px; position: relative;}
.a{ background-color: red;}
.b{ background-color: green; margin-top: -100px; margin-left: 100px; z-index: 1}
.c{ background-color: yellow; margin-top: -100px; margin-left: 200px;}
.d{ height: 450px; width: 450px; background-color: blue; color: #fff; margin-top: -400px; margin-left:0px;
z-index: -1;}
</style>
</head>
<body>
<div class="a">这里是A的内容</div>
<div class="b">这里是B的内容</div>
<div class="c">这里是C的内容这里是C的内容</div>
<div class="d">这里是D的内容这里是D的内容这里是D的内容</div>
</body>
...

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcoAAAHKCAYAAACDnAvcAAAYYUlEQVR4nO3dsW8b2Z0H8OFh/4JDki6dZeAW7tIEcnFtrEUAV1sFSBFA7mI32y1wKrZzI19nAykCXLVNDCxWTnuFjTTpfBvAcpcuOdy/wJtHzqyGw5kvh5RsmdLng7yVxRlyZkiR3/m994aZzWsVADDoX657BwDgUyYoASAQlAAQfLZ2y2x2DbsB+2FWGdKHm64/c0dFCQCBoASAYDAoX9XtWbjTrFkH2N7xWVWdHo4tXHb7HH/UPQKSwaA8qNuTcKfTur38ILtzfe5XwycH76vlicH7LR/vVfOYl9mfR5e4Px9ZHXzndcCdTUi4t++q6vHXIwtf1H+H9R/bw5uWlM3zM3SCcFh/oMzPF6tspZxwnJ9ebn+mvF5QzfuWJ7TzcmJ72vx7qFVhWWmHzTpT29C2pt73OOzHWbPOYVjnPBzP6YbHT9tN29zU2n06u8RjaFff0uLD0+Vb6HjCQ53V650ejiw/ni9WGL1/fb/ztTdutrat4+n3PTteve/x2fR1V56bkWM6PR+/X2plP85Pd385t3m9tNvV+kbHKL+qllVlW1H1WzVwW78COuslWvn9cCDp0kld/zH6bdMJ5TfN47+phruLS+V2MHA8bQX5bbU4yV871tQ1vY1HA4896+zT0chyXd/X52zk7fX68XL585Hl3b/zb+o/oMd/rN8PQ2dhz+sVHqzf3q9+Hs2Wk9Tb9qj+o3j/bPW20tLfSv8x+u3ZWFdKffv9/vr1m+mg3vdydvejpnJbPDfdY2oryPo/X9ypFz1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lAASCEgACQQkAgaAEgEBQAkAgKAEgEJQAEAhKAAgEJQAEghIAAkEJAIGgBIBAUAJAICgBIBCUABAISgAIBCUABIISAAJBCQCBoASAQFACQCAoASAQlAAQCEoACAQlAASCEgACQQkAgaAEgEBQAkAgKAEgmM1rKzfMrmtX4NM3n3uDwJjZbL55pT0w7x2GihIAAkEJAIGgBIBAUAJAICgBIBCUABAISgAIBCUABIISAAJBCQCBoASAQFACQCAoASAQlAAQCEoACAQlAASCEgACQQkAgaAEgEBQAkAgKAEgEJQAEAhKAAgEJQAEghIAAkEJAIGgBIBAUAJAICgBIBCUABAISgAIBCUABIISAAJBCQCBoASAQFACQCAoASAQlAAQCEoACAQlAASCEgACQQkAgaAEgEBQAkAgKAEgEJQAEAhKAAgEJQAEghIAAkEJAIGgBIBAUAJAICgBIBCUABAISgAIBCUABIISAAJBCQCBoASAQFACQCAoASAQlAAQCEoACAQlAASCEgACQQkAgaAEgEBQAkAgKAEgEJQAEAhKAAgEJQAEghIAAkEJAIGgBIBAUAJAICgBIBCUABAISgAIBCUABIISAAJBCQCBoASAQFACQCAoASAQlAAQCEoACAQlAASCEgACQQkAgaAEgEBQAkAgKAEgEJQAEAhKAAgEJQAEghIAAkEJAIGgBIDgs+veAdgns9n8uncB+MhUlAAQCEoACAQlAASCEgACQQkAgaAEgEBQAkAgKAEgEJQAEAhKAAgEJQAEghIAAkEJAIGgBIBAUAJAICgBIBCUABAISgAIBCUABIISAAJBCQCBoASAQFACQCAoASAQlAAQCEoACAQlAASCEgACQQkAgaAEgEBQAkAgKAEgEJQAEAhKAAgEJQAEghIAAkEJAIGgBIBAUAJAICgBIBCUABAISgAIBCUABIISAAJBCQCBoASAQFACQCAoASAQlAAQfNa/YT6/jt0AgE+TihIAAkEJAIGgBIDg/wGXa6PVle9kGgAAAABJRU5ErkJggg==" alt="" width="413" height="413" />

当 position设为 relative, absolute 或者 fixed, 而没有设置 z-index 时, IE8 以上和 W3C 浏览器 (下文我们统称为 W3C 浏览器) 的 z-index 默认值是 auto, 但 IE6 和 IE7 是 0.

五、从父规则

5.1 如果 A, B 节点都定义了 position:relative, A 节点的 z-index 比 B 节点大, 那么 A 的子节点必定覆盖在 B 的子节点前面.

...
<style>
div{width: 200px; height: 200px;}
.a{ background-color: red; position: relative; z-index: 2;}
.b{ background-color: green; margin-top: -100px; margin-left: 100px; position: relative; z-index: 1}
.a-1,.b-1{ background-color: yellow; width: 160px; height: 120px;}
</style>
</head>
<body>
<div class="a">这里是A的内容
<div class="a-1">这里是A的子内容A-1</div>
</div>
<div class="b">这里是B的内容这里是B的内容
<div class="b-1">这里是B的子内容B-1</div>
</div>
</body>
...

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATsAAAE4CAYAAAAkSFRpAAATW0lEQVR4nO3dP28jxxnH8WHgVxAk6dxJB+RynRtDKtJaOhhQdZUBFwHIzlJz3QGhAXdpqHQSkMJAKjU5wBDlNoUObtJdbOCozp0T5C0w83BnjqPRzOzsUhSpe74fYKET9z91++Mzs7vcwdwyAPCB+9WmNwAAHgJhB0AFwg6ACoQdABU+Ko4dDB5oM4BHiHN7jwqVHQAVCDsAKrSG3ZUdTgvjB24aANhmrWG3a4eTwviJHV7f2+Zsh32TDvgb04T7TcflXbllrrI9oxXmB1ARdjt2GJp8dXdsh/OWZcjBOugwpNZVO28pFK7cNKXgkSB74/Yrdmma92KnMP86fGua95gKGuivqs/upWmqO1/ZxINJvBaHztQO82CQ3/ei12QYFrYjXkY8TFr24xu3fAmzVHBICO4m9sdXchemCZ2acO5jlFj2INimw8x4QhBoVxV2vrqbmXLYhMPZGjY2d7D7odTc9hXbkWlCNtf0jgPVk0B5Zu7u5140f2q7Dt26ayrRSWIdpSFeP4C06rOxEl4HLdP4ZmJKHFS5ACg1iVep7C7dT9mHFy3ryW3/y8pp4w+FXBXbVomOzN0wpP8O6KcYdj684gok13SSainXDL2PZuwqlZ2M8+Hy3P2sbX629dU9aZn/nWmqwjbygSJ9hb45+9Q0Aet/F9fud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6ACYQdABcIOgAqEHQAVCDsAKhB2AFQg7ACoQNgBUIGwA6ACYQdABcIOgAqEHQAVCDsAKhB2AFQg7ACoQNgBUIGwA6ACYQdABcIOgAqEHQAVCDsAKhB2AFQg7ACoQNgBUIGwA6ACYQdABcIOgAqEHQAVCDsAKhB2AFQg7ACoQNgBUIGwA6ACYQdABcIOgAqEHQAVCDsAKhB2AFQg7ACoQNgBUIGwA6ACYQdABcIOgAqEHQAVCDsAKhB2AFQg7ACoQNgBUIGwA6DCR5veANQbfL3pLQAeLyo7ACoQdgBUIOwAqEDYAVCBsAOgAmEHQAXCDoAKhB0AFQg7ACoQdgBUIOwAqEDYAVCBsAOgAmEHQAXCDoAKhB0AFQg7ACoQdgBUIOwAqEDYAVCBsAOgAmEHQAXCDoAKhB0AFQg7ACoQdgBUIOwAqEDYAVCBsAOgAmEHQAXCDoAKhB0AFQg7ACoQdgBUIOwAqEDYAVCBsAOgwmBubXojAGDdqOwAqEDYAVCBsAOgAmEHQAXCDoAKhB0AFf4PrtdyicdWOkwAAAAASUVORK5CYII=" alt="" />

5.2 如果所有节点都定义了 position:relative, A 节点的 z-index 和 B 节点一样大, 但因为顺序规则, B 节点覆盖在 A 节点前面. 就算 A 的子节点 z-index 值比 B 的子节点大, B 的子节点还是会覆盖在 A 的子节点前面.

...
<style>
div{width: 200px; height: 200px;}
.a{ background-color: red; position: relative; z-index: 1;}
.b{ background-color: green; margin-top: -100px; margin-left: 100px; position: relative; z-index: 1}
.a-1,.b-1{ width: 160px; height: 120px; position: relative;}
.a-1{ background-color: yellow; z-index: 999;}
.b-1{ background-color: blue;}
</style>
</head>
<body>
<div class="a">这里是A的内容
<div class="a-1">这里是A的子内容A-1</div>
</div>
<div class="b">这里是B的内容这里是B的内容
<div class="b-1">这里是B的子内容B-1</div>
</div>
</body>
...

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAToAAAE2CAYAAADxgF4nAAAWk0lEQVR4nO3dPW8bWZbG8ctFf4LF7GSd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DmEXQAzCPoAJhH0AEwj6ADYB5BB8A8gg6AeQQdAPMIOgDmjWaNXa8EAGwTFR0A8wg6AOYRdADMI+gAmEfQATDv/wGAwgvfzTLP0gAAAABJRU5ErkJggg==" alt="" />

很多人将 z-index 设得很大, 9999 什么的都出来了, 如果不考虑父节点的影响, 设得再大也没用, 那是无法逾越的层级.

六、层级树规则

可能你会觉得,在 DOM 结构中的兄弟节点会拎出来进行比较并确定层级, 其实不然.

...
<style>
div{width: 200px; height: 200px;}
.a{ background-color: red; position: relative; z-index: 2;}
.b{ background-color: green; margin-top: -100px; margin-left: 100px;}
.a-1,.b-1{ width: 160px; height: 120px;}
.a-1{ background-color: yellow; position: relative; z-index: 0;}
.b-1{ background-color: blue; position: relative; z-index: 1; }
</style>
</head>
<body>
<div class="a">这里是A的内容
<div class="a-1">这里是A的子内容A-1</div>
</div>
<div class="b">这里是B的内容这里是B的内容
<div class="b-1">这里是B的子内容B-1</div>
</div>
</body>
...

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATkAAAEzCAYAAABKenSXAAAS40lEQVR4nO3dsW8bRxbH8eHBf8EhSZdOMnA+d2kCqbg2khFAlasAKQJQXaTGnYHjAe7S0NdZQIoAV6mJAMNS2itkpEnnSwDLXbrkcP8Cbx93XjgazczOLkVRev5+gIFM7pK7S3l/ejO7yx3NGg4AjPrTulcAAFaJkANgGiEHwLR72Smj0Q2uBnDHMJR9Z1DJATCNkANgWjHkzpr2vDB95OcBgNuqGHKbTTssTJ827eRaV2f9tl062N+5NtTf9Xy/M/+ey6zP/hKvB953xZDbaNrY5au5g6YddSxAdtJRj5ZaVu1rS2Fw5ucpBY4E2Gu/XbFXrv0sNgqvX4XvXPsZUzEDw3SOyT1xbTWnlUzcXOK5OGxOmzYLmjzeip6TNi6sR/wecZt2bMcz//4SYqnAkPDbTGyPVm7Hrg2bmlAeYj/x3qNgnXYz0wk/oKwz5LSau3DlkAnbixWsaG4n11bqVmuFtufacM11seMgVRIkD93V7dy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qg6AaYQcANMIOQCmEXIATCPkAJhGyAEwjZADYBohB8A0Qg6AaYQcANMIOQCmEXIATCPkAJhGyAEwjZADYBohB8A0Qg6AaYQcANMIOQCmEXIATCPkAJhGyAEwjZADYBohB8A0Qg6AaYQcANMIOQCmEXIATCPkAJhGyAEwjZADYBohB8A0Qg6AaYQcANMIOQCmEXIATCPkAJhGyAEwjZADYBohB8A0Qg6AaYQcANPurXsFUGkyWvcaAHcSlRwA0wg5AKYRcgBMI+QAmEbIATCNkANgGiEHwDRCDoBphBwA0wg5AKYRcgBMI+QAmEbIATCNkANgGiEHwDRCDoBphBwA0wg5AKYRcgBMI+QAmEbIATCNkANgGiEHwDRCDoBphBwA0wg5AKYRcgBMI+QAmEbIATCNkANgGiEHwDRCDoBphBwA0wg5AKYRcgBMI+QAmEbIATBtNGuseyUAYFWo5ACYRsgBMO3/K2jMts5MdPEAAAAASUVORK5CYII=" alt="" width="266" height="261" />

我们认为同时将 position 设为 relative, absolute 或者 fixed, 并且 z-index 经过整数赋值的节点, 会被放置到一个与 DOM 不一样的层级树里面, 并且在层级树中通过对比 z-index 决定显示的层级. 上面的例子如果用层级树来表示的话, 应该如下图所示.

图中虽然 A-1 (z-index:0) 的值比 B-1 (z-index:1) 小, 但因为在层级树里 A (z-index:2) 和 B-1 在一个层级, 而 A 的值比 B-1 大, 根据从父规则, A-1 显示在 B-1 前面.

七、参与规则 2

前面提到的参与规则认为只要节点的 position 属性为 relative, absolute 或者 fixed, 即可参与层级比较, 其实不准确. 如果所有节点都定义了 position:relative, 并且将 z-index 设为整数值, 根据从父规则, 父节点的层级决定了子节点所在层级.

例子中 A, B-1, C 作为父节点, z-index 的值相同, 根据顺序规则, C 在 B-1 之前, B-1 在 A 之前; 又根据从父规则, 无论子节点的 z-index 值是什么, C-1-1-1 在 B-1-1 之前, B-1-1 在 A-1 之前.

如果我们将所有父节点的 z-index 属性去除, 诡异的事情发生了. IE6 和 IE7 浏览器显示效果不变, 而 W3C 浏览器的子节点不再从父, 而是根据自身的 z-index 确定层级.

根据默认值规则, IE6 / IE7 和 W3C 浏览器上的元素存在 z-index 默认值的区别. 我们相信, 仅当 position 设为 relative, absolute 或者 fixed, 并且 z-index 赋整数值时, 节点被放置到层级树; 而 z-index 为默认值时, 只在 document 兄弟节点间比较层级. 在 W3C 浏览器中, A, B-1 和 C-1-1 的 z-index 均为 auto, 不参与层级比较.

而在 IE6 和 IE7 中, 因为 z-index 的默认值是 0, 所以也参与了层级比较.

设置了 position 而没有 z-index 的节点虽然不参与层级树的比较, 但还会在 DOM 中与兄弟节点进行层级比较.

我们对上个例子改造一下, 将 B-1 的 position 属性删除后, W3C 浏览器显示如下图. 根据定位规则, A 和 C 会显示在 B-1 的前面; 而根据顺序规则, C 又显示在 A 前面.

在 IE6 和 IE7 中, 因为 A 和 C-1-1 设置了 position:relative, 而且 z-index 的默认值为 0, 所以也参与层级树比较, 所以有如下效果.

转自:http://www.xiaoxiangzi.com/Programme/CSS/7884.html

CSS中z-index全解析的更多相关文章

  1. css中的一些属性解析

    1.inline-block 存在问题:inline-block的相互间距,元素之间会有一个左右2px的margin一样产生            请看中间的空隙. 为什么会产生这个空隙呢?? 怎么解 ...

  2. CSS中包含块原理解析

    CSS包含块原理解析 确定CSS中的包含块也确定就是元素的父元素.关键是:看元素是如何定位的.确定包含块很重要,比如设置百分比.另外也可以进行样式的继承等等. 分两个情况: 相对定位和静态定位 静态定 ...

  3. NCF WebApi中 Controller的全解析

    简介 上一篇我们说了如何实现一个WebApi,并运行起来 这次我们来说说WebApi的内在,我们到底做了哪些事情 仓库地址:https://github.com/NeuCharFramework/NC ...

  4. CSS Grid 网格布局全解析

    介绍 CSS Grid(网格) 布局使我们能够比以往任何时候都可以更灵活构建和控制自定义网格. Grid(网格) 布局使我们能够将网页分成具有简单属性的行和列.它还能使我们在不改变任何HTML的情况下 ...

  5. css中display的属性解析

    display 属性规定元素应该生成的框的类型. 他有很多属性值,见如下表格: none 此元素不会被显示. block 此元素将显示为块级元素,此元素前后会带有换行符. inline 默认.此元素会 ...

  6. Android: 在native中访问assets全解析

    本文总结在Android Native C++开发中访问APK中的assets资源的方法 在CMake中添加相关NDK LIB的 依赖 因为我们接下来用到的一些函数实现在NDK库libandroid. ...

  7. IDEA中配置maven 全解析教程(Day_08)

    每一个你讨厌的现在,都有一个不够努力的曾经. 一.选择一个maven的版本下载 本文中 maven 下载链接:(apache-maven-3.5.2.rar) https://files-cdn.cn ...

  8. CSS 中z-index全解析(摘自阿里西西)

    z-index全解析 Z-index属性决定了一个HTML元素的层叠级别.元素层叠级别是相对于元素在Z轴上(与X轴Y轴相对照)的位置而言.一个更高的Z-index值意味着这个元素在叠层顺序中会更靠近顶 ...

  9. Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析

    Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析 今天发一篇”水文”,可能很多读者都会表示不理解,不过我想把它作为并发序列文章中不可缺少的一块来介绍.本来以为花不了 ...

  10. C# 嵌入dll 动软代码生成器基础使用 系统缓存全解析 .NET开发中的事务处理大比拼 C#之数据类型学习 【基于EF Core的Code First模式的DotNetCore快速开发框架】完成对DB First代码生成的支持 基于EF Core的Code First模式的DotNetCore快速开发框架 【懒人有道】在asp.net core中实现程序集注入

    C# 嵌入dll   在很多时候我们在生成C#exe文件时,如果在工程里调用了dll文件时,那么如果不加以处理的话在生成的exe文件运行时需要连同这个dll一起转移,相比于一个单独干净的exe,这种形 ...

随机推荐

  1. SapScript

    * [OPEN_FORM] SAPscript: フォーム印刷の開始 * [START_FORM] SAPscript: 書式を開始 * [WRITE_FORM] SAPscript: 書式ウィンドウ ...

  2. Mac OS下搭建Hadoop + Spark集群

    首先注意版本兼容问题!!!本文采用的是Scala 2.11.8 + Hadoop 2.7.5 + Spark 2.2.0 请在下载Spark时务必看清对应的Scala和Hadoop版本! 一.配置JD ...

  3. c++实验3类和对象

     实 验 3: part 1:验证 part 2:graph #include <iostream> #include "graph.h" using namespac ...

  4. spring boot 中文文档地址

    spring boot 中文文档地址     http://oopsguy.com/documents/springboot-docs/1.5.4/index.html Spring Boot 参考指 ...

  5. 3招搞定APP注册作弊

    在说如何应对之前,易盾先给各位盾友梳理移动端APP可能遇到哪些作弊风险.1. 渠道商刷量,伪造大量的下载量和装机量,但没有新用户注册:2. 对于电商.P2P.外卖等平台,会面临散户或者团队刷子的注册- ...

  6. Android各版本代号、版本号、API/NDK级别、发布时间

    代号 版本号 API/NDK级别 发布时间 牛轧糖 Nougat 7.1.2 API level 25 2017-2 7.1.1 2016-10 7.0 API level 24 2016-05 棉花 ...

  7. 【Luogu P1935】[国家集训队]圈地计划

    题目 最近房地产商 GDOI (Group of Dumbbells Or Idiots) 从 NOI (Nuts Old Idiots) 手中得到了一块开发土地. 据了解,这块土地是一块矩形的区域, ...

  8. volatility的使用

    volatility取证的使用----windows内存 简介 kali下默认安装 可以对windows,linux,mac,android的内存进行分析 内存文件的准备 Win2003SP2x86下 ...

  9. python终极篇 --- django---班级管理系统

    周末没事自己写了个班级管理系统,虽然简单,但也算个前期学习的总结吧 from django.db import models # Create your models here. class Banj ...

  10. android问题笔记集

    开发工具:android studio2.2 调试:手机直连调试,版本(android6) 问题1: Error:Unable to start the daemon process. This pr ...