简体   繁体   English

CSS步骤向导Z索引无效

[英]css step wizard z-index not working

I've coded the step wizard as below. 我已经将步骤向导编码如下。

 ul.progress[data-steps="2"] li { width: 49%; } ul.progress[data-steps="3"] li { width: 33%; } ul.progress[data-steps="4"] li { width: 24%; } ul.progress[data-steps="5"] li { width: 19%; } ul.progress[data-steps="6"] li { width: 16%; } ul.progress[data-steps="7"] li { width: 14%; } ul.progress[data-steps="8"] li { width: 12%; } ul.progress[data-steps="9"] li { width: 11%; } .progress { width: 100%; list-style: none; list-style-image: none; padding: 20px 0; margin:0; overflow:hidden; border:2px solid #000; } .progress li { float: left; text-align: center; position: relative; } .progress .name { display: block; vertical-align: bottom; text-align: center; margin-bottom: 25px; color: black; opacity: 0.3; } .progress .step { color: black; border: 3px solid silver; background-color: silver; border-radius: 50%; line-height: 1.2; width: 30px; height: 30px; display: inline-block; z-index: 10; } .progress .step span { opacity: 0.3; } .progress .active .name, .progress .active .step span { } .progress .step:before { content: ""; display: block; background-color: silver; height: 5px; width: 50%; position: absolute; bottom: 15px; left: 0; z-index: 9; } .progress .step:after { content: ""; display: block; background-color: silver; height: 5px; width: 50%; position: absolute; bottom: 15px; right: 0; z-index: 9; } .progress li:first-of-type .step:before { display: none; } .progress li:last-of-type .step:after { display: none; } .progress .done .step, .progress .done .step:before, .progress .done .step:after, .progress .active .step, .progress .active .step:before { background-color: yellowgreen; } .progress .done .step, .progress .active .step { border: 3px solid yellowgreen; } 
  <div> <ul class="progress" data-steps="4"> <li class="done"> <span class="name">Foo</span> <span class="step"><span>1</span></span> </li> <li class="done"> <span class="name">Bar</span> <span class="step"><span>2</span></span> </li> <li class="active"> <span class="name">Baz</span> <span class="step"><span>3</span></span> </li> <li> <span class="name">Quux</span> <span class="step"><span>4</span></span> </li> </ul> </div> 

But the z-index doesn't work. 但是z索引不起作用。
z-index of the class "step" is 10, while the one of "step:before,step:after" is 9. 类“ step”的z-index为10,而“ step:before,step:after”之一为9。
Why does the "step:after" element , gray line , put above the green circle? 为什么将“ step:after”元素(灰线)放在绿色圆圈上方?
So I don't understand the z-index well. 所以我不太了解z-index。
Thanks. 谢谢。

Please try this one. 请试试这个。 1. I've added the z-index in .active .step span and and made it to circle with background. 1.我在.active .step span中添加了z-index,并使其与背景一起旋转。

.progress .active .name,
.progress .active .step span {
  position:relative;
  z-index:11;
  opacity: 1;
}

.progress .active .step span {
    border-radius: 50%;
    line-height: 1.2;
    width: 35px;
    height: 29px;
    display: inline-block;
    z-index: 12;
    margin:-2px 0 0 -2px;
    padding-top:6px;
  background-color: yellowgreen;
}
  1. I've added the lower z-index in .active .step:after than .step. 我在.active .step:after中添加了比.step更低的z-index。

    .progress .active .step:after { z-index:8; .progress .active .step:之后{z-index:8; } }

Please see whole code below; 请参见下面的完整代码;

 ul.progress[data-steps="2"] li { width: 49%; } ul.progress[data-steps="3"] li { width: 33%; } ul.progress[data-steps="4"] li { width: 24%; } ul.progress[data-steps="5"] li { width: 19%; } ul.progress[data-steps="6"] li { width: 16%; } ul.progress[data-steps="7"] li { width: 14%; } ul.progress[data-steps="8"] li { width: 12%; } ul.progress[data-steps="9"] li { width: 11%; } .progress { width: 100%; list-style: none; list-style-image: none; padding: 20px 0; margin:0; overflow:hidden; border:2px solid #000; } .progress li { float: left; text-align: center; position: relative; } .progress .name { display: block; vertical-align: bottom; text-align: center; margin-bottom: 25px; color: black; opacity: 0.3; } .progress .step { color: black; border: 3px solid silver; background-color: silver; border-radius: 50%; line-height: 1.2; width: 30px; height: 30px; display: inline-block; z-index: 10; } .progress .step span { opacity: 0.3; display:inline-block; margin-top:4px; position:relative; z-index:10; } .progress .active .step span { border-radius: 50%; line-height: 1.2; width: 35px; height: 29px; display: inline-block; z-index: 12; margin:-2px 0 0 -2px; padding-top:6px; background-color: yellowgreen; } .progress .active .name, .progress .active .step span { position:relative; z-index:11; opacity: 1; } .progress .step:before { content: ""; display: block; background-color: silver; height: 5px; width: 50%; position: absolute; bottom: 15px; left: 0; z-index: 9; } .progress .step:after { content: ""; display: block; background-color: silver; height: 5px; width: 50%; position: absolute; bottom: 15px; right: 0; z-index: 9; } .progress li:first-of-type .step:before { display: none; } .progress li:last-of-type .step:after { display: none; } .progress .done .step, .progress .done .step:before, .progress .done .step:after, .progress .active .step, .progress .active .step:before { background-color: yellowgreen; } .progress .active .step:after { z-index:8; } .progress .done .step, .progress .active .step { border: 3px solid yellowgreen; } 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <div> <ul class="progress" data-steps="4"> <li class="done"> <span class="name">Foo</span> <span class="step"><span>1</span></span> </li> <li class="done"> <span class="name">Bar</span> <span class="step"><span>2</span></span> </li> <li class="active"> <span class="name">Baz</span> <span class="step"><span>3</span></span> </li> <li> <span class="name">Quux</span> <span class="step"><span>4</span></span> </li> </ul> </div> </body> </html> 

I am able to achieve the result by not given the .step class a z-index and then giving the :before and :after element a negative z-index . 通过不给 .step类一个z-index ,然后给:before:after元素一个 z-index我可以实现结果。

I believe that giving the .step class a z-index value affects its :before and :after elements. 我相信.step类一个z-index值会影响它的:before:after元素。 For more details on that please see Michael Coker's great technical explanation in the comments below. 有关这方面的更多详细信息,请参阅下面的评论,了解Michael Coker的出色技术说明。

If there are other elements on the page like a background, give the background a z-index value of -2 like I did with the body 如果页面上还有其他元素(例如背景),请像我对body所做的那样,将背景z-index值设置为-2

 body { background:rgba(0, 0, 0, 0.77); z-index:-2; } ul.progress[data-steps="2"] li { width: 49%; } ul.progress[data-steps="3"] li { width: 33%; } ul.progress[data-steps="4"] li { width: 24%; } ul.progress[data-steps="5"] li { width: 19%; } ul.progress[data-steps="6"] li { width: 16%; } ul.progress[data-steps="7"] li { width: 14%; } ul.progress[data-steps="8"] li { width: 12%; } ul.progress[data-steps="9"] li { width: 11%; } .progress { width: 100%; list-style: none; list-style-image: none; padding: 20px 0; margin:0; overflow:hidden; border:2px solid #000; } .progress li { float: left; text-align: center; position: relative; } .progress .name { display: block; vertical-align: bottom; text-align: center; margin-bottom: 25px; color: white; opacity: 1; } .progress .step { color: black; border: 3px solid silver; background-color: silver; border-radius: 50%; line-height: 1.2; width: 30px; height: 30px; display: inline-block; } .progress .step span { opacity: 1; } .progress .active .name, .progress .active .step span { } .progress .step:before { content: ""; display: block; background-color: silver; height: 5px; width: 50%; position: absolute; bottom: 15px; left: 0; z-index: -1; } .progress .step:after { content: ""; display: block; background-color: silver; height: 5px; width: 50%; position: absolute; bottom: 15px; right: 0; z-index: -1; } .progress li:first-of-type .step:before { display: none; } .progress li:last-of-type .step:after { display: none; } .progress .done .step, .progress .done .step:before, .progress .done .step:after, .progress .active .step, .progress .active .step:before { background-color: yellowgreen; } .progress .done .step, .progress .active .step { border: 3px solid yellowgreen; } 
 <div> <ul class="progress" data-steps="4"> <li class="done"> <span class="name">Foo</span> <span class="step"><span>1</span></span> </li> <li class="done"> <span class="name">Bar</span> <span class="step"><span>2</span></span> </li> <li class="active"> <span class="name">Baz</span> <span class="step"><span>3</span></span> </li> <li> <span class="name">Quux</span> <span class="step"><span>4</span></span> </li> </ul> </div> 

请尝试将元素之前和之后的z-index设置为-1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM