There is a simple div in the document with some styles. Its width is not set in HTML nor in CSS. I tried to calculate its actual width in JavaScript using different methods:
offsetWidthclientWidthgetBoundingClientRect().widthgetComputedStyle(element).getPropertyValue('width')
but none of them returned the actual width which can be seen in Chrome Developer Tools.
Here is a code that demonstrate this issue:
var resumeHeader = document.querySelector('#resume-header');
var resumeHeaderComputedWidth = getComputedStyle(resumeHeader, null).getPropertyValue('width');
console.log(resumeHeaderComputedWidth)
console.log(resumeHeader.offsetWidth);
console.log(resumeHeader.clientWidth)
console.log(resumeHeader.getBoundingClientRect().width)
#resume-header {
display: inline-block;
background-color: #F5D061;
padding: 2px 6px;
font-weight: bold;
font-size: 14px;
position: relative;
top: 25px;
left: 0;
transition: width 1s ease;
font-family: "Merriweather", serif;
line-height: 1.8;
letter-spacing: 1px;
box-sizing: border-box;
}
<link href="https://fonts.googleapis.com/css?family=Merriweather:300,400" rel="stylesheet">
<div id="resume-header">
Resume
</div>
Please note that in the above snippet, the calculated width is sometimes correct and sometimes wrong, while in Chrome, it is always wrong.
innerWidthandouterWidth? :)windowproperties? How can I use them in this case?