user12345 user12345 - 5 months ago 73
CSS Question

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

Answer

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.

Comments