I've been trying to learn more about when layout/reflow gets triggered using Chrome Dev Tools. I have this simple example set up in JSFiddle, where I am changing the "left" position of a group of divs one at a time in a for loop. Then, I'm immediately checking the "left" property, which I understand should force a layout (since the browser must recompute the position of the element in order to provide the updated value):
JavaScript:
const colors = document.body.querySelectorAll("div")
const button = document.querySelector(".my-button")
button.onclick = handleClick
function handleClick () {
colors.forEach(color => {
color.style["left"] = "50px"
const left = color.style["left"] <--- This should force a Layout since "left" was just updated
})
}
HTML:
<html>
<head>
</head>
<body>
<button class="my-button">
Click
</button>
<div class="red-color">
red
</div>
<div class="blue-color">
blue
</div>
<div class="green-color">
green
</div>
<div class="yellow-color">
yellow
</div>
<div class="black-color">
black
</div>
</body>
</html>
CSS:
div {
position: relative
}
In the dev tools, I was expecting to see that on each iteration of the for loop, a layout would be triggered. Specifically, I was looking out for purple Layout bars (one for each iteration) to be under the yellow Function Call bar (which should correspond to the handleClick callback). Instead, it looks like there's a single Layout that occurs after handleClick is called:

Here are the questions I have:
- Why don't I see any layouts being triggered during handleClick when I am manipulating the positions of div elements and immediately checking their new positions?
- Should I expect to see a new layout being triggered on each iteration of the for loop? I would assume so since the browser needs to provide the updated "left" position on each iteration.
Checking the
Element.style.propertyvalue will not trigger a reflow no. This value is not the "computed value", and this doesn't require the layout to be recalculated.Maybe you were confusing it with
getComputedStyle(element).propertywhich will cause a reflow: