Search code examples
javascriptcsspropertieslinear-gradients

How do I create a linear gradient as a JavaScript property value?


Is it possible to set a linear gradient as a property value? I only know how to a hex value to the property, but how would you do the gradient equivalent of a CSS value in a JavaScript property value?

CSS

background: linear-gradient(180deg, #FAF9FA 0%, #F2F5FE 65.75%);

JavaScript to add a gradient to the background property.

this.opts = {
  background: 0x000000,
  fill: 0xffffff,
  ...options,
};

Solution

  • Have you tried to implement it like this?

    this.opts = {
      background: 'linear-gradient(180deg, #FAF9FA 0%, #F2F5FE 65.75%)',
      ...options,
    };