我需要将条形排列在底部,而不是顶部

  • 本文关键字:底部 顶部 排列 css
  • 更新时间 :
  • 英文 :


尝试构建垂直条形图。知道了,但是,酒吧在顶部排队。我希望它们排在底部。似乎无法弄清楚那部分没有运气。我打电话给 css 的大师寻求帮助。我相信它既简单又小,会让我看起来像一个巴丰......就这样吧!

.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>

最简单的方法是添加

  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 步:使实际条形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>

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

.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>

最新更新