James Nelson
James Nelson

Reputation: 71

I need to line the bars up at the bottom, not the top

Trying to build a vertical bar chart. Got that but, the bars are lining up at the top. I want them to line up at the bottom. Can't seem to figure that part out with no luck. I call to the Guru's of css for help. I'm sure it simple and small and will make me look like a bafone... So be it!

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

Upvotes: 0

Views: 100

Answers (3)

Temani Afif
Temani Afif

Reputation: 274384

If it's only for the visual you can simplify your code like below:

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

You can make it responsive by removing px value:

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

Upvotes: 1

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196306

The simplest route would be to add

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

to your .bar-container class

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

Upvotes: 2

connexo
connexo

Reputation: 56853

Step 1: Make the actual bars display: inline-block; vertical-align: bottom;

Step 2: Add line-height: 100px; font-size: 0; to div.bar-container.

Step 3: Enjoy :)

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

Upvotes: 1

Related Questions