Performance: Justify content
Controls horizontal alignment of items in a flex or grid container, affecting layout calculation and paint.
Jump into concepts and practice - no test required
display: flex; justify-content: space-between; /* no manual margins on children */
display: flex; justify-content: space-between; /* then manually adding margins on children */ .child { margin-left: 20px; margin-right: 20px; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using justify-content only | Minimal DOM changes | 1 reflow per layout change | Low paint cost | [OK] Good |
| Justify-content plus manual margins | Extra DOM style changes | Multiple reflows if margins change | Higher paint cost | [X] Bad |
justify-content control in a flex container?justify-contentalign-items, not justify-content.justify-content in a flex container?justify-contentflex-start, center, space-between, and space-around.center, so the syntax is justify-content: center;.div.container {
display: flex;
justify-content: space-between;
width: 300px;
}
<div class="container">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>justify-content: space-between;justify-content from working:div.container {
display: block;
justify-content: center;
}justify-content only works on flex or grid containers, but here display is set to block.display: flex; or display: grid;, justify-content has no effect.justify-content value should you use in your flex container?space-between puts equal space only between items, no space at edges. space-around adds equal space around each item, including edges.space-around is the correct choice.