Add style using javascript?

Hello,
Its possible to add style using javascript?
I have this on the header:

<style>
  #canvas-gradient {
    position: absolute;
    display: block;
    width: 100%;
    height: 100%;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}
</style>

But I want to know if its possible to add that style using javascript.
Thanks

document.getElementsByTagName('canvas')[0].setAttribute("id","canvas-gradient")
(Note: This will only work for one element, because only one element can have an ID.)

1 Like

Yes, you can add the style via JavaScript instead. It’s just not as convenient as doing it with CSS.

var el = document.querySelector("#canvas-gradient");
el.style.position = "absolute";
el.style.display = "block";
...
el.style.left = "0";
1 Like

Thanks both! Worked!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.