JSFiddle - React, Tailwind, and code Playground
HTML
<div class="mydiv"></div>
CSS
.mydiv{
width:200px;
height:100px;
}
JavaScript
Element.prototype.setGradient = function( fromColor, toColor ){
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var b = this.getBoundingClientRect();
var grd = ctx.createLinearGradient(0, 0, b.height*2, 0);
grd.addColorStop(0, fromColor);
grd.addColorStop(1, toColor);
ctx.fillStyle = grd;
ctx.fillRect(0, 0, b.width, b.height);
this.style.backgroundImage = 'url('+canvas.toDataURL()+')';
}
document.querySelector('.mydiv').setGradient('red','green');