Is there is a way in CSS to make it pick a random font color from an array? I know I can do this with server side or javascript, but I am wondering if there is pure CSS way to do this.
backgroundColor = 'rgb('+ rgb. join(',') +')'; If you want to constrict it to known colors, you can create an array of the colors and randomly select it like so.
setProperty('--main-bg-color', randomColor); Note that you can also create CSS variables for specific elements, not just the root document element. Or you could use a completely different way of selecting a random color (like user input). This allows for possibilities like theming.
CSS expressions (allowing for dynamic script content via CSS) were abominations cast in the bowels of the hell of inefficiency alongside Web Forms, only ever supported by IE7 and below. But since you asked.
<style> blink marquee { color: expression("rgb(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ")"); } </style> <blink> <marquee> color me beautiful </marquee> </blink>
This is not possible in CSS, which is firmly deterministic. You could do this with client-side JavaScript, though:
var colors = ['#ff0000', '#00ff00', '#0000ff']; var random_color = colors[Math.floor(Math.random() * colors.length)]; document.getElementById('title').style.color = random_color;
If you're using jQuery, the last line could become
$('#title').css('color', random_color);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With