How to get an element’s padding value using JavaScript?

Spread the love

To get an element’s padding values using JavaScript, you can use the window.getComputedStyle() method.


To do this we write:

Get Element's Padding

This is a div element.



In this example, we first get the element by its ID using document.getElementById().
Then, we use window.getComputedStyle() to get the computed style of the element, which includes all styles applied to the element, including padding.
Finally, we use getPropertyValue() method to retrieve the values of padding-top, padding-right, padding-bottom, and padding-left properties.
Keep in mind that the values returned by window.getComputedStyle() are in pixels (px) by default, but you can use additional functions to convert them to other units if needed.