Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple background color for div

I have a div

<div class="test">
   Some text
</div>

I would like to have different background color for the same div by percent (Horizontal coloring)

-----------------------------
| 20%   |  30%   | 50%      |
| Red   | Yellow | Green    |
-----------------------------

Is this possible with CSS?

like image 358
ddb Avatar asked Nov 19 '13 09:11

ddb


3 Answers

You can use CSS3 Gradients[1] to achieve such effect

div {
    background: linear-gradient(to right,  #ff3236 0%,#ff3033 32%,#3e30ff 32%,#3e30ff 63%,#33ff30 63%,#33ff30 100%);
    height: 400px;
}

Demo

You can create such gradients over here


You can also use px as a unit, along with % if you are looking for static gradient widths

Demo (Please add browser-prefixes if you are looking for a cross browser solution, I've not added all the rules in this demo)

Demo 2 (Vertical Split, just change to right to to bottom)

1. More on CSS3 Gradients       2. Browser Support

like image 62
Mr. Alien Avatar answered Sep 27 '22 22:09

Mr. Alien


You could achieve this by using a gradient:

Either google it and create an own. Or use a generator like this:

http://www.colorzilla.com/gradient-editor/

which gives you the following css-code:

background: #ff3019; /* Old browsers */
background: -moz-linear-gradient(left,  #ff3019 0%, #d40000 20%, #f2f600 20%, #f2f600 50%, #1e7a00 50%, #1e7a00 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, right top, color-stop(0%,#ff3019), color-stop(20%,#d40000), color-stop(20%,#f2f600), color-stop(50%,#f2f600), color-stop(50%,#1e7a00), color-stop(100%,#1e7a00)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(left,  #ff3019 0%,#d40000 20%,#f2f600 20%,#f2f600 50%,#1e7a00 50%,#1e7a00 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(left,  #ff3019 0%,#d40000 20%,#f2f600 20%,#f2f600 50%,#1e7a00 50%,#1e7a00 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(left,  #ff3019 0%,#d40000 20%,#f2f600 20%,#f2f600 50%,#1e7a00 50%,#1e7a00 100%); /* IE10+ */
background: linear-gradient(to right,  #ff3019 0%,#d40000 20%,#f2f600 20%,#f2f600 50%,#1e7a00 50%,#1e7a00 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ff3019', endColorstr='#1e7a00',GradientType=1 ); /* IE6-9 */
like image 44
Igle Avatar answered Sep 27 '22 22:09

Igle


You could create three descendant divs within the parent. Absolutely position them, make the parent transparent, then give the three divs a z-index of 0 so they sit underneath the text, not on top.

like image 37
Edward G-Jones Avatar answered Sep 27 '22 23:09

Edward G-Jones