Categories
JavaScript Answers

How to get width in pixels from element with style set with % with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *