使用CSS实现百分比

时间:2018-11-30 04:03:31

标签: html css flexbox

我必须实现带有百分比标记的垂直线,该百分比线必须相对于百分比栏定位,如屏幕截图所示:

enter image description here

我尝试进行基本的实现,但没有成功https://stackblitz.com/edit/js-74f8sl?file=index.html如何为这种情况实现标记和样式?我应该使用相对定位还是一些flexbox的东西就足够了?

1 个答案:

答案 0 :(得分:2)

我可能会混合使用flex和相对/绝对定位。根据所使用的处理器(无效)的不同,您可以对其进行自动前缀以使其也与较旧版本的浏览器兼容。

这是一个例子:

body {
  background-color: lightgray;
}

.track-bar {
  width: 600px;
  height: 30px;
  margin-top: 30px;
  background-color: white;
  display: flex;
  align-items: center;
  align-content: center;
  flex-wrap: no-wrap;
  position: relative;
  z-index: 1030;
}

.rating {
  height: 100%;
  color: white;
  opacity: 0;
  position: relative;
  text-align: center;
  text-transform: uppercase;
  display: flex;
  align-items: center;
  align-content: center;
  justify-content: center;
}

#zero-thirty {
  flex: 0 0 30%;
  background-color: red;
}

#thirty-seventy {
  flex: 0 0 40%;
  background-color: green;
}

#seventy-hundred {
  flex: 0 0 30%;
  background-color: green;
}

.active {
  opacity: 1;
}

.marker {
  height: 40px;
  width: 1px;
  background-color: black;
  position: absolute;
  bottom: 0;
  z-index: 1050;
}

.marker span {
  position: absolute;
  left: 50%;
  transform: translateX(-50%) translateY(-100%);
}

.label-track {
  margin-top: 10px;
  position: relative;
  color: rgba(0,0,0,.6);
  width: 600px;
}

.label-track span {
  position: absolute;
}
<div class="track-bar">

    <div class="rating" id="zero-thirty">
    </div>

    <div class="rating active" id="thirty-seventy">
      <span>Good</span>
    </div>

    <div class="rating" id="seventy-hundred">
    </div>

    <div class="marker" style="left: 25.07%;">
      <span>25.07%</span>
    </div>

  </div>

  <div class="label-track">

    <span style="left: 0%;">0%</span>
    <span style="left: 30%;">30%</span>
    <span style="left: 70%;">70%</span>
    <span style="right: 0%;">100%</span>

  </div>