-1

everyone! I want to know how to create a random RGBA color with CSS: The css values I want to be random are this ones:

-webkit-box-shadow: 0px 3px 0px rgba(14,162,236,1), 0px 3px 6px rgba(0,0,0,.9);
    -moz-box-shadow: 0px 3px 0px rgba(14,162,236,1), 0px 3px 6px rgba(0,0,0,.9);
    box-shadow: 0px 3px 0px rgba(14,162,236,1), 0px 3px 6px rgba(0,0,0,.9);

Which are the color I want to be random...enter code here

Calicol
  • 23
  • 4

2 Answers2

0

You can't with just CSS but you can with javascript.

Here is one way of doing so:

function rainbow(numOfSteps, step) {
    // This function generates vibrant, "evenly spaced" colours (i.e. no clustering). This is ideal for creating easily distinguishable vibrant markers in Google Maps and other apps.
    // Adam Cole, 2011-Sept-14
    // HSV to RBG adapted from: http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
    var r, g, b;
    var h = step / numOfSteps;
    var i = ~~(h * 6);
    var f = h * 6 - i;
    var q = 1 - f;
    switch(i % 6){
        case 0: r = 1; g = f; b = 0; break;
        case 1: r = q; g = 1; b = 0; break;
        case 2: r = 0; g = 1; b = f; break;
        case 3: r = 0; g = q; b = 1; break;
        case 4: r = f; g = 0; b = 1; break;
        case 5: r = 1; g = 0; b = q; break;
    }
    var c = "#" + ("00" + (~ ~(r * 255)).toString(16)).slice(-2) + ("00" + (~ ~(g * 255)).toString(16)).slice(-2) + ("00" + (~ ~(b * 255)).toString(16)).slice(-2);
    return (c);
}

Please note that the above is not my code it was simply taken from one of the answer at Random color generator in JavaScript.

Community
  • 1
  • 1
Jesse Elser
  • 974
  • 2
  • 11
  • 39
0

It is not possible to do it using CSS only, unfortunately.

You can do it in a pretty simple way using javascript and jQuery, though.

var rgbaArray = ['14,162,236,1','0,0,0,0.9'] // the array containing the colors
var randomColor = hexArray[Math.floor(Math.random() * hexArray.length)];

When you have the random color in a variable, you can change the element color.

var boxShadowStyle = '0px 3px 0px rgba(' + randomColor + ')' ;
$("#divID").css("box-shadow", boxShadowStyle); // or .className