According to w3schools:
The scrollWidth and scrollHeight properties return the entire height and width of an element, including the height and width that is not viewable (because of overflow).
If that's the case, why is the scrollWidth of an element inside a narrower flex parent reported as the visible portion only?
document.body.append("Bar 1 scrollWidth = " + document.getElementById("inner1").scrollWidth);
document.body.appendChild(document.createElement("br"));
document.body.append("Bar 2 scrollWidth = " + document.getElementById("inner2").scrollWidth);
div {
outline: 1px solid grey;
margin-bottom: 10px;
}
.outer {
width: 200px;
background-color: yellow;
padding: 3px;
}
.inner {
width: 300px;
position: relative;
display: inline-block;
background-color: pink;
}
#outer2 {
display: flex;
background-color: lightgreen;
}
<div class="outer" id="outer1">
<div class="inner" id="inner1">Bar 1</div>
</div>
<div class="outer" id="outer2">
<div class="inner" id="inner2">Bar 2</div>
</div>