user481610
user481610

Reputation: 3270

Creating a multi-color bar using CSS

I would like to create a multi-color bar like the one in the picture below:

enter image description here

Is it possible to create CSS that will achieve this? I've managed to create the color gradients using the following CSS:

.gold{
  background-color: #faa732;
  background-image: -moz-linear-gradient(top, #eab92d, #c79810);
  background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#eab92d), to(#c79810));
  background-image: -webkit-linear-gradient(top, #eab92d, #c79810);
  background-image: -o-linear-gradient(top, #eab92d, #c79810);
  background-image: linear-gradient(to bottom, #eab92d, #c79810);
  background-repeat: repeat-x;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fffbb450', endColorstr='#fff89406', GradientType=0);
}

.blue {
  background-color: #faa732;
  background-image: -moz-linear-gradient(top, #034a96, #0663c7);
  background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#034a96), to(#0663c7));
  background-image: -webkit-linear-gradient(top, #034a96, #0663c7);
  background-image: -o-linear-gradient(top, #034a96, #0663c7);
  background-image: linear-gradient(to bottom, #034a96, #0663c7);
  background-repeat: repeat-x;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fffbb450', endColorstr='#fff89406', GradientType=0);
}

.green {
  background-color: #faa732;
  background-image: -moz-linear-gradient(top, #0D7626, #0a611e);
  background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#0D7626), to(#0a611e));
  background-image: -webkit-linear-gradient(top, #0D7626, #0a611e);
  background-image: -o-linear-gradient(top, #0D7626, #0a611e);
  background-image: linear-gradient(to bottom, #0D7626, #0a611e);
  background-repeat: repeat-x;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fffbb450', endColorstr='#fff89406', GradientType=0);
}

I'm just not sure how to make them appear next to each other like in the picture and also how to have different percentage widths (e.g. blue gradient 50% of the bar, green gradient 40%, and gold gradient 10%).

Upvotes: 1

Views: 12829

Answers (4)

Steeve
Steeve

Reputation: 443

i will prefer table over div. try this

<table>
<tr width="300px">
<td style="background:#50c690; width:250px;height:25px;"></td><td style="background:#FE6; width:50px;height:25px;"></td>
</tr>
</table>

Upvotes: 0

CrookedCreek
CrookedCreek

Reputation: 31

You can also use grids, like:

<div class="my-grid">
<div class="col-1-3">(be yellow)</div>
<div class="col-2-3">(be green)</div>
<div class="col-3-3">(be blue)</div>
</div>

Then in CSS

.my-grid {
width: 100%;
}
.col-1-3 {
width: 33%;
float: left;
background-color: yellow;
}
.col-2-3 {
width: 33%;
float: left;
background-color: green;
}
.col-3-3 {
width: 33%;
float: left;
background-color: blue;
}
.my-grid:after {
clear:both;
}

Be sure to do the my-grid:after part.

Upvotes: 0

What you need is :before and :after pseudo elements. They add content to the beginning and end inside the given selector.

HTML:

<div></div>

CSS:

div {
  height: 2em;

  background-color: #faa732;
  background-image: -moz-linear-gradient(top, #eab92d, #c79810);
  background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#eab92d), to(#c79810));
  background-image: -webkit-linear-gradient(top, #eab92d, #c79810);
  background-image: -o-linear-gradient(top, #eab92d, #c79810);
  background-image: linear-gradient(to bottom, #eab92d, #c79810);
  background-repeat: repeat-x;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fffbb450', endColorstr='#fff89406', GradientType=0);
}

div:before {
  height: 2em;
  width: 50%;
  display: block;
  content: "";
  float: left;

  background-color: #faa732;
  background-image: -moz-linear-gradient(top, #034a96, #0663c7);
  background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#034a96), to(#0663c7));
  background-image: -webkit-linear-gradient(top, #034a96, #0663c7);
  background-image: -o-linear-gradient(top, #034a96, #0663c7);
  background-image: linear-gradient(to bottom, #034a96, #0663c7);
  background-repeat: repeat-x;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fffbb450', endColorstr='#fff89406', GradientType=0);
}

div:after {
  height: 2em;
  width: 40%;
  display: block;
  content: "";
  float: right;

  background-color: #faa732;
  background-image: -moz-linear-gradient(top, #0D7626, #0a611e);
  background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#0D7626), to(#0a611e));
  background-image: -webkit-linear-gradient(top, #0D7626, #0a611e);
  background-image: -o-linear-gradient(top, #0D7626, #0a611e);
  background-image: linear-gradient(to bottom, #0D7626, #0a611e);
  background-repeat: repeat-x;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fffbb450', endColorstr='#fff89406', GradientType=0);
}

Result:

enter image description here

Demo: http://jsbin.com/umaden/3/edit

PS In real usage you should apply this to a class or id, not an element selector.

Upvotes: 7

Matal
Matal

Reputation: 107

Well.. if you're asking what I think you're asking, it's as simple as this. HTML:

<table>
    <tr> 
        <td class="color1"></td>
        <td class="color2"></td>
        <td class="color3"></td>
    </tr>
</table>    

CSS:

table { border-collapse: collapse; }
td{
   width: 100px;
   height: 20px;
   padding: 0px;
}
.color1{
  background-color: red;
}
.color2{
  background-color: blue;
}
.color3{
   background-color: yellow;
}

Or something along those lines.

http://jsfiddle.net/waDFz/

Here's what that looks like. You can edit the height/width/colors/class names.

Upvotes: 4

Related Questions