I need to find out the width which is defined via CSS of an element on the page; I do not need the actual width which was calculated by the browser!
It's no problem to get the width value from an inline style attribute, but how do I get the width-definition when it is inside a CSS file?
Here's a short sample with 3 div-elements:
- The first div uses an inline CSS width, which I can find.
- The second div has a relative width defined via CSS → How can I find the "50%" value?
- The third div has no width definition → How can I find out that this div has no width?
var box1 = document.getElementById('item1');
var box2 = document.getElementById('item2');
var box3 = document.getElementById('item3');
console.log('Box 1 width:', box1.style.width); // all good!
console.log('Box 2 width:', box2.style.width); // required result: "50%"
console.log('Box 3 width:', box3.style.width); // required result: "", "initial" or undefined
#item1 { width: 200px; }
#item2 { width: 50%; }
#item1, #item2, #item3 { background: #ccc; margin: 2px; padding: 5px }
<div id="item1" style="width: auto">Box 1</div>
<div id="item2">Box 2</div>
<div id="item3">Box 3</div>