Flexbox Sizing and Alignment
W3C CSS Snapshot 2026
Choose how flex items grow or shrink. Then control spacing across wrapped lines and the position of individual items.
On this page
Put a flex group inside a sentence
Both flex and inline-flex arrange the direct children as flex items. The difference is how the container itself sits beside other content. A nested element stays inside its parent item.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Put a flex group inside a sentence</title>
<style>
.layout { display: inline-flex; gap: 6px; }
.layout { border: 1px dashed #577; }
.layout > * { box-sizing: border-box; padding: 4px; background: #d6efea; }
</style>
</head>
<body>
<p>Topics <span class="layout"><span>HTML</span><span>CSS</span></span></p>
</body>
</html>
What to expect The two tags sit beside “Topics”. Change inline-flex to flex and the group starts on a new line.
Calculate how much each item grows or shrinks
Read flex: 1 1 60px as grow, shrink, basis. Each item starts at 60px. At 300px there are 120px left. Grow factors of 1, 2 and 1 split that extra width into 30px, 60px and 30px. When space is short, shrink factors are weighted by the starting sizes. Minimum sizes can limit the result.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Calculate how much each item grows or shrinks</title>
<style>
.layout { display: flex; width: 300px; max-width: 100%; }
.layout > * { flex: 1 1 60px; min-width: 0; overflow-wrap: anywhere; }
.layout > :nth-child(2) { flex-grow: 2; }
.layout { border: 1px dashed #577; }
.layout > * { box-sizing: border-box; padding: 4px; background: #d6efea; }
</style>
</head>
<body>
<div class="layout">
<div>Pen</div>
<div>Notebook</div>
<div>Folder</div>
</div>
</body>
</html>
What to expect At 300px the widths are 90px, 120px and 90px. Set width to 150px and all three shrink to 50px. Give the middle item flex-shrink: 0 and it keeps 60px.
Keep a button fixed while the file name shrinks
The initial flex value is 0 1 auto. flex: auto (1 1 auto) starts from the item’s width or content and lets it grow or shrink. flex: none (0 0 auto) keeps its size. For equal shares, flex: 1 1 0 starts from zero. In a row, min-width: 0 allows a label to shrink below its content width.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Keep a button fixed while the file name shrinks</title>
<style>
.layout { display: flex; align-items: center; gap: 8px; width: 14rem; max-width: 100%; }
.name { flex: auto; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
button { flex: none; }
.layout { border: 1px dashed #577; }
.layout > * { box-sizing: border-box; padding: 4px; background: #d6efea; }
</style>
</head>
<body>
<div class="layout"><span class="name">November-project-notes-final.pdf</span><button>Download</button></div>
</body>
</html>
What to expect At 14rem the label ends with an ellipsis and Download remains visible. Increase the width to reveal the full name.
Align whole lines and individual items
flex-flow combines direction and wrapping. With row wrap, each line distributes its own space. align-content positions the lines when the cross axis has spare room. align-items aligns all items within their lines, and align-self overrides that for one item. Try stretch, center, end or baseline for item alignment.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Align whole lines and individual items</title>
<style>
.layout { display: flex; flex-flow: row wrap; width: 200px; height: 150px;
gap: 8px; align-content: space-between; align-items: start; }
.layout > * { flex: 0 0 90px; }
.layout > :first-child { height: 52px; }
.layout > :nth-child(2) { align-self: end; }
.layout { border: 1px dashed #577; }
.layout > * { box-sizing: border-box; padding: 4px; background: #d6efea; }
</style>
</head>
<body>
<div class="layout">
<div>Pen</div>
<div>Notebook</div>
<div>Folder</div>
</div>
</body>
</html>
What to expect The first and last lines touch opposite edges. Notebook sits at the bottom of the taller first line. Change space-between to center, then try wrap-reverse to reverse the line stacking direction.
Push the last toolbar action to the far edge
gap sets the gutter between neighbors. An auto margin takes available space on its side before justify-content distributes what remains. margin-inline-start follows the writing direction, so the same toolbar works in a right-to-left layout.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Push the last toolbar action to the far edge</title>
<style>
.layout { display: flex; gap: 8px; }
.layout > :last-child { margin-inline-start: auto; }
.layout { border: 1px dashed #577; }
.layout > * { box-sizing: border-box; padding: 4px; background: #d6efea; }
</style>
</head>
<body>
<div class="layout"><button>New</button><button>Open</button><button>Help</button></div>
</body>
</html>
What to expect New and Open stay together. Help sits at the far edge. Add dir="rtl" to the div and check which edge Help uses.
Compare visual order with keyboard order
Items default to order: 0. Lower numbers appear first and ties follow source order. row-reverse and column-reverse reverse the main direction. These changes affect the layout while ordinary keyboard navigation still follows the HTML. Write the meaningful sequence in the HTML first.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare visual order with keyboard order</title>
<style>
.layout { display: flex; gap: 8px; }
.layout > :nth-child(2) { order: -1; }
.layout { border: 1px dashed #577; }
.layout > * { box-sizing: border-box; padding: 4px; background: #d6efea; }
</style>
</head>
<body>
<div class="layout"><button>Draft</button><button>Review</button><button>Publish</button></div>
</body>
</html>
What to expect Review appears first. Click Draft and press Tab. Focus moves to Review, the next button in the HTML.