进度栏,元素和z-index的重叠:如何使其工作?

时间:2018-08-31 08:02:00

标签: css z-index

我在这里有一个进度条,其中包含三个html元素:一个黄色块,一个红色块和一个文本块,用作进度标签。

演示https://codepen.io/anon/pen/wEgJmL

transparents

红色和黄色块被叠加,因此它们混合成代表进度百分比的颜色(红色条具有一定的不透明度设置)。

但是我的文本元素没有显示,我也不明白为什么...

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

我已经记录了.pinim-pc-bar-text的CSS更改。当元素绝对定位时,它们将从文档流中删除。然后需要对文本进行相同的操作。

.pinim-pc-bar {
  position: relative;
  display: block;
  background-color: rgba(204, 204, 204, 0.4);
  clear: both;
  width: 100%;
  height: 1.3em;
  line-height: 1.3em;
}

.pinim-pc-bar .pinim-pc-bar-fill {
  display: block;
  height: 100%;
  text-align: left;
  -moz-transition: all 0.5s ease-out;
  -o-transition: all 0.5s ease-out;
  -webkit-transition: all 0.5s ease-out;
  transition: all 0.5s ease-out;
  position: relative;
}

.pinim-pc-bar .pinim-pc-bar-fill .pinim-pc-bar-fill-color {
  width: 100%;
  height: 100%;
  position: absolute;
}

.pinim-pc-bar .pinim-pc-bar-fill .pinim-pc-bar-fill-color.pinim-pc-bar-fill-yellow {
  background-color: #ffe000;
}

.pinim-pc-bar .pinim-pc-bar-fill .pinim-pc-bar-fill-color.pinim-pc-bar-fill-red {
  background-color: red;
}

.pinim-pc-bar .pinim-pc-bar-fill .pinim-pc-bar-text {
  margin: auto;
  display: block;
  /* text-align: center; DOES NOT WORK */
  /* ADDED */  
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  /* END ADDED */
}

.pinim-pc-bar .pinim-pc-bar-fill .pinim-pc-bar-text i.fa {
  padding: 0 0.5em;
}

.pinim-pc-bar.color-light .pinim-pc-bar-text {
  color: white;
}

.pinim-pc-bar.empty .pinim-pc-bar-fill {
  width: 100% !important;
}

.pinim-pc-bar.empty .pinim-pc-bar-fill-color {
  display: none;
}

.pinim-pc-bar.empty .pinim-pc-bar-text {
  color: inherit;
  font-size: 0.8em;
}

.pinim-pc-bar.incomplete .pinim-pc-bar-text {
  text-align: right;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<p class="pinim-pc-bar incomplete"><span class="pinim-pc-bar-fill" style="width:80%"><span class="pinim-pc-bar-fill-color pinim-pc-bar-fill-yellow"></span><span class="pinim-pc-bar-fill-color pinim-pc-bar-fill-red" style="opacity:0.2"></span><span class="pinim-pc-bar-text">8/10<i class="fa fa-refresh" aria-hidden="true"></i></span></span>
</p>

答案 1 :(得分:0)

只需在条形图上添加负数z-index,并在其容器中添加z-index:0,即可将其保留在相同的stacking context中,并在上方显示文本。

.pinim-pc-bar {
  position: relative;
  display: block;
  background-color: rgba(204, 204, 204, 0.4);
  clear: both;
  width: 100%;
  height: 1.3em;
  line-height: 1.3em;
}
.pinim-pc-bar .pinim-pc-bar-fill {
  display: block;
  height: 100%;
  text-align: left;
  -moz-transition: all 0.5s ease-out;
  -o-transition: all 0.5s ease-out;
  -webkit-transition: all 0.5s ease-out;
  transition: all 0.5s ease-out;
  position: relative;
  z-index:0;
}
.pinim-pc-bar .pinim-pc-bar-fill .pinim-pc-bar-fill-color {
  width: 100%;
  height: 100%;
  position: absolute;
  z-index:-1;
}
.pinim-pc-bar .pinim-pc-bar-fill .pinim-pc-bar-fill-color.pinim-pc-bar-fill-yellow {
  background-color: #FFE000;
}
.pinim-pc-bar .pinim-pc-bar-fill .pinim-pc-bar-fill-color.pinim-pc-bar-fill-red {
  background-color: red;
}
.pinim-pc-bar .pinim-pc-bar-fill .pinim-pc-bar-text {
  margin: auto;
  display: block;
  text-align: center;
}
.pinim-pc-bar .pinim-pc-bar-fill .pinim-pc-bar-text i.fa {
  padding: 0 0.5em;
}
.pinim-pc-bar.color-light .pinim-pc-bar-text {
  color: white;
}
.pinim-pc-bar.empty .pinim-pc-bar-fill {
  width: 100% !important;
}
.pinim-pc-bar.empty .pinim-pc-bar-fill-color {
  display: none;
}
.pinim-pc-bar.empty .pinim-pc-bar-text {
  color: inherit;
  font-size: 0.8em;
}
.pinim-pc-bar.incomplete .pinim-pc-bar-text {
  text-align: right;
}
<p class="pinim-pc-bar incomplete"><span class="pinim-pc-bar-fill" style="width:80%"><span class="pinim-pc-bar-fill-color pinim-pc-bar-fill-yellow"></span><span class="pinim-pc-bar-fill-color pinim-pc-bar-fill-red" style="opacity:0.2"></span><span class="pinim-pc-bar-text">8/10<i class="fa fa-refresh" aria-hidden="true"></i></span></span></p>

<p>Hi!  This progress bar (colored part) contains three html elements : a yellow block (<em>.pinim-pc-bar-fill-yellow</em>), a red block (<em>.pinim-pc-bar-fill-red</em>), and a text block (<em>.pinim-pc-bar-text</em>),used as label for the progression.</p>

<p>
  The red & yellow blocks are surimposed so they mix to a color that represent the percentage of progression (the red bar has a certain opacity set).
</p>

<p>BUT Why is my text element not displayed ?  Thanks !</p>

答案 2 :(得分:0)

使用以下CSS代码:

.pinim-pc-bar.incomplete .pinim-pc-bar-text{position: relative;}