0

How can I get the CSS value used with JavaScript?

For example, I am testing on a canvas element with the css below but because it is not directly set to the element and is not in px which could be calculated via getComputedStyle(canvas).width, I am not sure how to get this value(100%).

canvas{
  width: 100%;
}
Damien Golding
  • 1,003
  • 4
  • 15
  • 28

1 Answers1

-1

You have to get the element by Id then you will get style property of that object.

var canvas = document.getElementById('canvas');
canvas.style.border = "1px solid red"
canvas.style.width = "130px"
<div id="canvas"> <h3> Canvas </h3> </div>

This was you can pass the css property as a string, make sure you use right cssText property

Sahid Hossen
  • 1,377
  • 12
  • 14