向水平条形图添加标签

时间:2018-09-21 13:41:06

标签: html css html5 css3

我是HTML的新手。在下面的图表中,如何添加一些标签,例如

Team 1up
Team blue
Team tigers
Team watermelon
Team crazyred
Team Melt

图表左侧的轨道是否全部对齐?

$(function() {
      $('.progress-fill span').each(function(){
        var percent = $(this).html();
        $(this).parent().css('width', percent);
      });
});
body {
  background: #999;
}

.container {
  width: 500px;
  margin: 20px;
  background: #fff;
  padding: 20px;
  overflow: hidden;
  float: left;
}

.horizontal .progress-bar {
  float: left;
  height: 18px;
  width: 100%;
  padding: 3px 0;
}

.horizontal .progress-track {
  position: relative;
  width: 100%;
  height: 20px;
  background: #ebebeb;
}

.horizontal .progress-fill {
  position: relative;
  background: #666;
  height: 20px;
  width: 50%;
  color: #fff;
  text-align: center;
  font-family: "Lato","Verdana",sans-serif;
  font-size: 12px;
  line-height: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container horizontal flat">
  <h2>Team performances</h2>
  <div class="progress-bar horizontal">
    <div class="progress-track">
      <div class="progress-fill">
        <span>100%</span>
      </div>
    </div>
  </div>

  <div class="progress-bar horizontal">
    <div class="progress-track">
      <div class="progress-fill">
        <span>75%</span>
      </div>
    </div>
  </div>

  <div class="progress-bar horizontal">
    <div class="progress-track">
      <div class="progress-fill">
        <span>60%</span>
      </div>
    </div>
  </div>

  <div class="progress-bar horizontal">
    <div class="progress-track">
      <div class="progress-fill">
        <span>20%</span>
      </div>
    </div>
  </div>

  <div class="progress-bar horizontal">
    <div class="progress-track">
      <div class="progress-fill">
        <span>34%</span>
      </div>
    </div>
  </div>

  <div class="progress-bar horizontal">
    <div class="progress-track">
      <div class="progress-fill">
        <span>82%</span>
      </div>
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:2)

您可以创建一个名为progress-group的组件,并将progress-labelprogress-bar放入其中。 progress-group负责儿童结盟。在这种情况下,我使用了flex。如果将来要添加额外的组件,则可以提供更多控制权。

https://codepen.io/anon/pen/oPVyYm

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container horizontal flat">
  <h2>Team performances</h2>
  <div class="progress-group">
    <div class="progress-label">Team 1up</div>
    <div class="progress-bar horizontal">
      <div class="progress-track">
        <div class="progress-fill">
          <span>100%</span>
        </div>
      </div>
    </div>
  </div>
  <div class="progress-group">
    <div class="progress-label">Team blue</div>
    <div class="progress-bar horizontal">
      <div class="progress-track">
        <div class="progress-fill">
          <span>75%</span>
        </div>
      </div>
    </div>
  </div>
  <div class="progress-group">
    <div class="progress-label">Team tigers</div>
    <div class="progress-bar horizontal">
      <div class="progress-track">
        <div class="progress-fill">
          <span>60%</span>
        </div>
      </div>
    </div>
  </div>
  <div class="progress-group">
    <div class="progress-label">Team watermelon</div>
    <div class="progress-bar horizontal">
      <div class="progress-track">
        <div class="progress-fill">
          <span>20%</span>
        </div>
      </div>
    </div>
  </div>
  <div class="progress-group">
    <div class="progress-label">Team crazyred</div>
    <div class="progress-bar horizontal">
      <div class="progress-track">
        <div class="progress-fill">
          <span>34%</span>
        </div>
      </div>
    </div>
  </div>
  <div class="progress-group">
    <div class="progress-label">Team Melt</div>
    <div class="progress-bar horizontal">
      <div class="progress-track">
        <div class="progress-fill">
          <span>82%</span>
        </div>
      </div>
    </div>
  </div>
</div>




body {
  background: #999;
}

.container {
  width: 500px;
  margin: 20px;
  background: #fff;
  padding: 20px;
  overflow: hidden;
  float: left;
}

.horizontal .progress-bar {
  float: left;
  height: 18px;
  width: 100%;
  padding: 3px 0;
}

.horizontal .progress-track {
  position: relative;
  width: 100%;
  height: 20px;
  background: #ebebeb;
}

.horizontal .progress-fill {
  position: relative;
  background: #666;
  height: 20px;
  width: 50%;
  color: #fff;
  text-align: center;
  font-family: "Lato","Verdana",sans-serif;
  font-size: 12px;
  line-height: 20px;
}

.progress-group {
  display: flex;
}

.progress-label {
  width: 170px;
  text-align: right;
  padding-right: 10px;
}

.progress-label::after {
  content: ":";
}



$(function() {
      $('.progress-fill span').each(function(){
        var percent = $(this).html();
        $(this).parent().css('width', percent);
      });
});

答案 1 :(得分:1)

像这样的先生吗?

https://codepen.io/JABedford/pen/RYdMjV

我只是将条形图浮动到右侧,并将宽度减小到80%,并为相关文本创建了另一个跨度。

<div class="progress-bar horizontal">
<span class="name">Team Name</span>
<div class="progress-track">
  <div class="progress-fill">
    <span>75%</span>
  </div>
</div>

    body {
      background: #999;
    }

    .container {
      width: 500px;
      margin: 20px;
      background: #fff;
      padding: 20px;
      overflow: hidden;
      float: left;
    }

    .horizontal .progress-bar {
      float: right;
      height: 18px;
      width: 100%;
      padding: 3px 0;
    }

    .horizontal .progress-track {
      position: relative;
      float: left;
      width: 80%;
      height: 20px;
      background: #ebebeb;
    }

    .horizontal .progress-fill {
      position: relative;
      background: #666;
      height: 20px;
      width: 50%;
      color: #fff;
      text-align: center;
      font-family: "Lato","Verdana",sans-serif;
      font-size: 12px;
      line-height: 20px;
    }