javascript – Get width of element and add as variable to :root

Question

I have a ‘main’ container on a page that has padding-left and padding-right added by means of a calculation so the values are dynamic as such. I need to find the ‘net’ width of the container (excluding the padding) and add the value as a style attribute on the page.

My current code is this;

var uw = document.getElementById('main').offsetWidth;
document.documentElement.style.setProperty( '--uw', `${uw}px` ); 

That gets me the total width INCLUDING the padding.

How can I get the ‘net’ width?

0
Briany Hearne 5 months 2022-03-12T15:25:02-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse