Performance: Flex container
Flex containers affect how the browser calculates layout and paints elements, impacting rendering speed and visual stability.
Jump into concepts and practice - no test required
display: flex; flex-wrap: nowrap; justify-content: space-between; align-items: center; width: 100%; /* Use fixed flex-basis and avoid nested flex containers */
display: flex; flex-wrap: wrap; justify-content: center; align-items: center; width: 100%; /* Each item uses flex-grow: 1 with complex nested flex containers */
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Nested flex containers with many flex-grow items | High (many nodes recalculated) | Multiple reflows on resize | High paint cost due to layout changes | [X] Bad |
| Single-level flex container with fixed flex-basis | Low (few recalculations) | Single reflow on resize | Low paint cost | [OK] Good |
display: flex; on a container do?display: flex;display: flex; on a container activates flexbox layout for its children.display with the value flex.display: flex;. Others are invalid CSS. .container { display: flex; }
.box { width: 50px; height: 50px; background: red; margin: 5px; } <div class='container'>
<div class='box'></div>
<div class='box'></div>
<div class='box'></div>
</div>display: flex;, which arranges children in a row by default..container {
display: flex;
flex-direction: column;
}flex-direction: column; stacks children vertically.align-items: center; centers items along the cross axis (horizontal in column direction).