Sometimes, we want to get width in pixels from element with style set with % with JavaScript.
In this article, we’ll look at how to get width in pixels from element with style set with % with JavaScript.
How to get width in pixels from element with style set with % with JavaScript?
To get width in pixels from element with style set with % with JavaScript, we can use the offsetWidth
property.
For instance, we write
document.querySelector("div").offsetWidth;
to select the div with querySelector
.
Then we get the width of the element with offsetWidth
in pixels.
Conclusion
To get width in pixels from element with style set with % with JavaScript, we can use the offsetWidth
property.