Flex Alignment Playground
Buttons change flex direction and alignment on three boxes, with the CSS shown live.
AI prompt
Educational diagram showing a flex container as a dashed rectangle holding three coloured boxes of different heights, arrows along the main axis and the cross axis, four small panels in a grid illustrating justify-content values start, center, space-between and end, labels in clean monospace under each, flat vector style, light background, soft pastel boxes. Negative: gradients, shadows, text errors, clutter. Reduced motion: static diagram.About this pattern
Definition
Flexbox lays items along a main axis. justify-content aligns them along that axis and align-items aligns them across it. Switching flex-direction swaps which axis is which, which is why the two properties seem to change places.
Purpose & UX principles
- flex-direction: row or column sets the main axis.
- justify-content: start, center, space-between or end along the main axis.
- align-items: stretch, start, center or end across it.
- gap sets spacing between items without margins.
Interaction states
- Three button groups and a live CSS read-out.
Motion specification
- Items move instantly.
- Reduced motion: nothing animates.
Responsive behaviour
- Use gap instead of margins on children.
- To centre one thing both ways, set justify-content and align-items to center.
Code
<div class="bx"><div class="fx" id="fx"><i style="height:30px">1</i><i style="height:52px">2</i><i style="height:40px">3</i></div><div class="gp" id="gp"></div><pre class="cs" id="cs"></pre></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.fx{display:flex;height:110px;border:1px dashed #b9b9b4;border-radius:10px;padding:6px;gap:8px;background:var(--surface)}.fx i{width:46px;border-radius:7px;display:grid;place-items:center;font-style:normal;font-size:12px;font-weight:700;color:rgba(0,0,0,.55)}.fx i:nth-child(1){background:#f6d7b8}.fx i:nth-child(2){background:#cfe5f5}.fx i:nth-child(3){background:#d9efc9}
.gp{display:grid;gap:5px}.g{display:flex;gap:4px;align-items:center;flex-wrap:wrap;font-size:11px;color:var(--muted)}.g span{width:62px}.g button{height:24px;padding:0 9px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}.g button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}.cs{font:11.5px/1.4 'JetBrains Mono',ui-monospace,monospace;background:var(--pv);border-radius:8px;padding:6px 10px;margin:0}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const G=[['Direction','flex-direction',['row','column']],['Justify','justify-content',['flex-start','center','space-between','flex-end']],['Align','align-items',['stretch','flex-start','center','flex-end']]],fx=document.getElementById('fx'),gp=document.getElementById('gp'),st={'flex-direction':'row','justify-content':'flex-start','align-items':'stretch'};
gp.innerHTML=G.map((g,gi)=>'<div class="g" role="group" aria-label="'+g[0]+'"><span>'+g[0]+'</span>'+g[2].map(v=>'<button type="button" data-k="'+g[1]+'" data-v="'+v+'" aria-pressed="false">'+v.replace('flex-','')+'</button>').join('')+'</div>').join('');
function up(){for(const k in st)fx.style[k]=st[k];fx.querySelectorAll('i').forEach(i=>{i.style.width=st['flex-direction']==='column'?'':'46px';if(st['align-items']==='stretch'&&st['flex-direction']==='row')i.style.height='';});
const hs=['30px','52px','40px'];fx.querySelectorAll('i').forEach((i,n)=>{if(st['flex-direction']==='row'&&st['align-items']!=='stretch')i.style.height=hs[n];else if(st['flex-direction']==='column'){i.style.height='24px';i.style.width=['50px','90px','70px'][n];if(st['align-items']==='stretch')i.style.width='';}});
gp.querySelectorAll('button').forEach(b=>b.setAttribute('aria-pressed',String(st[b.dataset.k]===b.dataset.v)));document.getElementById('cs').textContent='display:flex;'+String.fromCharCode(10)+'flex-direction:'+st['flex-direction']+';'+String.fromCharCode(10)+'justify-content:'+st['justify-content']+';'+String.fromCharCode(10)+'align-items:'+st['align-items']+';';}
gp.addEventListener('click',e=>{const b=e.target.closest('button');if(!b)return;st[b.dataset.k]=b.dataset.v;up();});up();One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Flex Alignment Playground — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.fx{display:flex;height:110px;border:1px dashed #b9b9b4;border-radius:10px;padding:6px;gap:8px;background:var(--surface)}.fx i{width:46px;border-radius:7px;display:grid;place-items:center;font-style:normal;font-size:12px;font-weight:700;color:rgba(0,0,0,.55)}.fx i:nth-child(1){background:#f6d7b8}.fx i:nth-child(2){background:#cfe5f5}.fx i:nth-child(3){background:#d9efc9}
.gp{display:grid;gap:5px}.g{display:flex;gap:4px;align-items:center;flex-wrap:wrap;font-size:11px;color:var(--muted)}.g span{width:62px}.g button{height:24px;padding:0 9px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}.g button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}.cs{font:11.5px/1.4 'JetBrains Mono',ui-monospace,monospace;background:var(--pv);border-radius:8px;padding:6px 10px;margin:0}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="fx" id="fx"><i style="height:30px">1</i><i style="height:52px">2</i><i style="height:40px">3</i></div><div class="gp" id="gp"></div><pre class="cs" id="cs"></pre></div>
</div>
<script>
const G=[['Direction','flex-direction',['row','column']],['Justify','justify-content',['flex-start','center','space-between','flex-end']],['Align','align-items',['stretch','flex-start','center','flex-end']]],fx=document.getElementById('fx'),gp=document.getElementById('gp'),st={'flex-direction':'row','justify-content':'flex-start','align-items':'stretch'};
gp.innerHTML=G.map((g,gi)=>'<div class="g" role="group" aria-label="'+g[0]+'"><span>'+g[0]+'</span>'+g[2].map(v=>'<button type="button" data-k="'+g[1]+'" data-v="'+v+'" aria-pressed="false">'+v.replace('flex-','')+'</button>').join('')+'</div>').join('');
function up(){for(const k in st)fx.style[k]=st[k];fx.querySelectorAll('i').forEach(i=>{i.style.width=st['flex-direction']==='column'?'':'46px';if(st['align-items']==='stretch'&&st['flex-direction']==='row')i.style.height='';});
const hs=['30px','52px','40px'];fx.querySelectorAll('i').forEach((i,n)=>{if(st['flex-direction']==='row'&&st['align-items']!=='stretch')i.style.height=hs[n];else if(st['flex-direction']==='column'){i.style.height='24px';i.style.width=['50px','90px','70px'][n];if(st['align-items']==='stretch')i.style.width='';}});
gp.querySelectorAll('button').forEach(b=>b.setAttribute('aria-pressed',String(st[b.dataset.k]===b.dataset.v)));document.getElementById('cs').textContent='display:flex;'+String.fromCharCode(10)+'flex-direction:'+st['flex-direction']+';'+String.fromCharCode(10)+'justify-content:'+st['justify-content']+';'+String.fromCharCode(10)+'align-items:'+st['align-items']+';';}
gp.addEventListener('click',e=>{const b=e.target.closest('button');if(!b)return;st[b.dataset.k]=b.dataset.v;up();});up();
</script>
</body></html>