我需要将条形图放在底部而不是顶部

时间:2019-07-13 14:12:16

标签: css

尝试构建垂直条形图。知道了,但是酒吧在顶部排成一行。我希望他们排在最下面。似乎无法找出没有运气的那部分。我打电话给css的大师寻求帮助。我敢肯定它既简单又小巧,它将使我看起来像个bafone ...就这样吧!

.box {
  float: left;
  height: 70%;
}


/* The bar container */

.bar-container {
  height: 100px;
  width: 100%;
  background-color: #f1f1f1;
}


/* Individual bars */

.bar-5 {
  height: 60%;
  width: 4px;
  background-color: green;
}

.bar-4 {
  height: 30%;
  width: 4px;
  background-color: blue;
}

.bar-3 {
  height: 10%;
  width: 4px;
  background-color: orange;
}

.bar-2 {
  height: 4%;
  width: 4px;
  background-color: red;
}

.bar-1 {
  height: 15%;
  width: 4px;
  background-color: purple;
}
<div class="box">
  <div class="bar-container">
    <div class="bar-5"></div>
  </div>
</div>
<div class="box">
  <div class="bar-container">
    <div class="bar-4"></div>
  </div>
</div>
<div class="box">
  <div class="bar-container">
    <div class="bar-3"></div>
  </div>
</div>
<div class="box">
  <div class="bar-container">
    <div class="bar-2"></div>
  </div>
</div>
<div class="box">
  <div class="bar-container">
    <div class="bar-1"></div>
  </div>
</div>

3 个答案:

答案 0 :(得分:2)

最简单的方法是添加

  display:flex;
  align-items:flex-end;

进入您的.bar-container

.box {
  float: left;
  height: 70%;
}


/* The bar container */

.bar-container {
  height: 100px;
  width: 100%;
  background-color: #f1f1f1;
  display:flex;
  align-items:flex-end;
}

/* Individual bars */

.bar-5 {
  height: 60%;
  width: 4px;
  background-color: green;
}

.bar-4 {
  height: 30%;
  width: 4px;
  background-color: blue;
}

.bar-3 {
  height: 10%;
  width: 4px;
  background-color: orange;
}

.bar-2 {
  height: 4%;
  width: 4px;
  background-color: red;
}

.bar-1 {
  height: 15%;
  width: 4px;
  background-color: purple;
}
<div class="box">
  <div class="bar-container">
    <div class="bar-5"></div>
  </div>
</div>
<div class="box">
  <div class="bar-container">
    <div class="bar-4"></div>
  </div>
</div>
<div class="box">
  <div class="bar-container">
    <div class="bar-3"></div>
  </div>
</div>
<div class="box">
  <div class="bar-container">
    <div class="bar-2"></div>
  </div>
</div>
<div class="box">
  <div class="bar-container">
    <div class="bar-1"></div>
  </div>
</div>

答案 1 :(得分:1)

步骤1:制作实际的小节display: inline-block; vertical-align: bottom;

第2步:将line-height: 100px; font-size: 0;添加到div.bar-container

第3步:享受:)

.box {
  float: left;
  height: 70%;
}


/* The bar container */

.bar-container {
  font-size: 0;
  line-height: 100px;
  height: 100px;
  width: 100%;
  background-color: #f1f1f1;
}

.bar-container * { 
  display: inline-block;
  vertical-align: bottom;
}


/* Individual bars */

.bar-5 {
  height: 60%;
  width: 4px;
  background-color: green;
}

.bar-4 {
  height: 30%;
  width: 4px;
  background-color: blue;
}

.bar-3 {
  height: 10%;
  width: 4px;
  background-color: orange;
}

.bar-2 {
  height: 4%;
  width: 4px;
  background-color: red;
}

.bar-1 {
  height: 15%;
  width: 4px;
  background-color: purple;
}
<div class="box">
  <div class="bar-container">
    <div class="bar-5"></div>
  </div>
</div>
<div class="box">
  <div class="bar-container">
    <div class="bar-4"></div>
  </div>
</div>
<div class="box">
  <div class="bar-container">
    <div class="bar-3"></div>
  </div>
</div>
<div class="box">
  <div class="bar-container">
    <div class="bar-2"></div>
  </div>
</div>
<div class="box">
  <div class="bar-container">
    <div class="bar-1"></div>
  </div>
</div>

答案 2 :(得分:1)

如果仅用于视觉效果,则可以简化代码,如下所示:

.bar-container {
  height: 100px;
  width:20px;
  background: 
    /* Color                       position           /width height */
    linear-gradient(green,green)   bottom 0 left 0    / 4px 60%,
    linear-gradient(blue,blue)     bottom 0 left 4px  / 4px 30%,
    linear-gradient(orange,orange) bottom 0 left 8px  / 4px 10%,
    linear-gradient(red,red)       bottom 0 left 12px / 4px 4%,
    linear-gradient(purple,purple) bottom 0 left 16px / 4px 15%,
    #f1f1f1; /*background-color */
  background-repeat:no-repeat;
}
<div class="bar-container"></div>

您可以通过删除px值来使其具有响应性:

.bar-container {
  height: 100px;
  width:20px;
  background: 
    /* Color                       position            / width        height */
    linear-gradient(green,green)   calc(0*100%/4) 100% / calc(100%/5) 60%,
    linear-gradient(blue,blue)     calc(1*100%/4) 100% / calc(100%/5) 30%,
    linear-gradient(orange,orange) calc(2*100%/4) 100% / calc(100%/5) 10%,
    linear-gradient(red,red)       calc(3*100%/4) 100% / calc(100%/5) 4%,
    linear-gradient(purple,purple) calc(4*100%/4) 100% / calc(100%/5) 15%,
    #f1f1f1; /*background-color */
  background-repeat:no-repeat;
  
  display:inline-block;
}
<div class="bar-container"></div>

<div class="bar-container" style="width:60px;height:150px"></div>


<div class="bar-container" style="width:150px;height:200px"></div>