SolutionsCase StudiesAI ToolsResourcesAboutContact
Get Your Free AI Automation Audit Book a Free 30-Minute Strategy Call
Playground~/frontend Open editor

~/css/flexbox-layouts

7 CSS Flexbox Layouts

6 Pure CSS1 with JSPublished 2026-10-05Free · no sign-up

Seven flexbox patterns used everywhere: a navbar where one auto margin pushes links apart, tags that wrap with gap, an input with a prefix and a button, the media object, columns that are truly equal, an alignment playground and a vertical stack spaced by gap.

01. Navbar with margin-left: auto

FlexboxNavigation

In a flex row, margin-left: auto on one item pushes it and everything after it to the far end. Here the logo stays left and the sign-in link is pushed right, with no wrapper divs and no space-between guessing.

View code
<nav class="fx1" aria-label="Main"><a class="logo" href="#home">Pebble</a><a href="#product">Product</a><a href="#pricing">Pricing</a><a class="push" href="#signin">Sign in</a></nav>
.fx1 { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 16px; width: 560px; max-width: 100%; box-sizing: border-box; padding: 12px 16px; border: 1px solid #e4e7ec; border-radius: 12px; background: #fff; font: 600 14px Inter, system-ui, sans-serif; }
.fx1 a { color: #344054; text-decoration: none; }
.fx1 .logo { margin-right: 8px; color: #101828; font-weight: 900; font-size: 18px; }
.fx1 .push { margin-left: auto; padding: 7px 12px; border-radius: 8px; background: #101828; color: #fff; }
.fx1 a:focus-visible { outline: 3px solid #84adff; outline-offset: 2px; border-radius: 6px; }

02. Wrapping Tags with gap

FlexboxWrap

Tags that wrap onto new lines as space runs out, with even spacing in both directions from a single gap property. Before gap worked in flexbox this needed negative margins on the container; now it is one line.

View code
<ul class="fx2"><li>Accessibility</li><li>Performance</li><li>CSS Grid</li><li>Design systems</li><li>Animation</li><li>Forms</li><li>Typography</li></ul>
.fx2 { display: flex; flex-wrap: wrap; gap: 8px; width: 360px; max-width: 100%; margin: 0; padding: 0; list-style: none; font: 600 13px Inter, system-ui, sans-serif; }
.fx2 li { padding: 6px 12px; border: 1px solid #334155; border-radius: 999px; background: #1e293b; color: #e2e8f0; }

03. Input Group with Prefix and Button

FlexboxForms

A URL field with a fixed prefix, a flexible input and a button, all in one flex row. The input has flex: 1 and min-width: 0, which lets it shrink below its default size instead of pushing the button out of the box.

View code
<form class="fx3"><label for="fx3u" class="sr">Your shop address</label><div class="g"><span aria-hidden="true">https://</span><input id="fx3u" value="my-shop" autocomplete="off"><button>Check</button></div></form>
.fx3 { width: 340px; max-width: 100%; font: 14px Inter, system-ui, sans-serif; }
.fx3 .g { display: flex; border: 1px solid #98a2b3; border-radius: 10px; overflow: hidden; background: #fff; }
.fx3 .g:focus-within { border-color: #2563eb; box-shadow: 0 0 0 3px rgba(37,99,235,.2); }
.fx3 span { display: grid; place-items: center; padding: 0 10px; background: #f2f4f7; color: #475467; border-right: 1px solid #d0d5dd; }
.fx3 input { flex: 1; min-width: 0; padding: 10px; border: 0; font: inherit; }
.fx3 input:focus { outline: none; }
.fx3 button { flex: none; padding: 0 14px; border: 0; background: #101828; color: #fff; font: 700 13px Inter, system-ui, sans-serif; cursor: pointer; }
.fx3 button:focus-visible { outline: 3px solid #84adff; outline-offset: -3px; }
.fx3 .sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

04. Media Object

FlexboxPattern

The media object: an image or avatar on one side and text that wraps beside it, never under it. The image is flex: none so it keeps its size, the text block is flex: 1 with min-width: 0 so long words wrap instead of overflowing.

View code
<article class="fx4"><span class="av" aria-hidden="true">KO</span><div><b>Kemi Oduya</b><p>Replied to your comment on Spring launch checklist: Let's move the email to Thursday so the photos are ready.</p></div></article>
.fx4 { display: flex; align-items: flex-start; gap: 12px; width: 360px; max-width: 100%; box-sizing: border-box; padding: 14px; border-radius: 14px; background: #151b2e; font: 14px/1.5 Inter, system-ui, sans-serif; }
.fx4 .av { flex: none; display: grid; place-items: center; width: 44px; height: 44px; border-radius: 50%; background: #fbbf24; color: #451a03; font-weight: 800; }
.fx4 div { flex: 1; min-width: 0; }
.fx4 b { color: #fff; } .fx4 p { margin: 2px 0 0; color: #94a3b8; overflow-wrap: anywhere; }

05. Truly Equal Columns

FlexboxSizing

flex: 1 alone can make columns different widths, because the content's size is added before the free space is shared. flex: 1 1 0 starts every item from zero, so the columns come out equal whatever is inside them. The two rows compare both.

View code
<div class="fx5"><p>flex: 1 1 auto</p><div class="r auto"><span>A</span><span>A much longer label</span><span>B</span></div><p>flex: 1 1 0</p><div class="r zero"><span>A</span><span>A much longer label</span><span>B</span></div></div>
.fx5 { width: 380px; max-width: 100%; font: 13px Inter, system-ui, sans-serif; }
.fx5 p { margin: 8px 0 4px; color: #475467; font-family: "JetBrains Mono", ui-monospace, monospace; font-size: 12px; }
.fx5 .r { display: flex; gap: 6px; }
.fx5 span { padding: 10px 6px; border-radius: 8px; background: #eef4ff; color: #1e3a8a; font-weight: 600; text-align: center; min-width: 0; overflow-wrap: anywhere; }
.fx5 .auto span { flex: 1 1 auto; } .fx5 .zero span { flex: 1 1 0; background: #ecfdf3; color: #054f31; }

06. Alignment Playground

FlexboxInteractive

Pick justify-content and align-items values and watch three boxes move. It is the quickest way to learn the two axes: justify works along the main axis (the row), align across it. The selects are labelled and update the container's style directly.

View code
<div class="fx6"><div class="ctl"><label>justify-content<select data-p="justifyContent"><option>flex-start</option><option selected>center</option><option>flex-end</option><option>space-between</option><option>space-around</option><option>space-evenly</option></select></label><label>align-items<select data-p="alignItems"><option>flex-start</option><option selected>center</option><option>flex-end</option><option>stretch</option></select></label></div><div class="box"><i style="height:30px"></i><i style="height:50px"></i><i style="height:40px"></i></div></div>
.fx6 { width: 340px; max-width: 100%; font: 12px Inter, system-ui, sans-serif; color: #cbd5e1; }
.fx6 .ctl { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; }
.fx6 label { display: grid; gap: 4px; font-family: "JetBrains Mono", ui-monospace, monospace; }
.fx6 select { min-width: 0; padding: 6px; border: 1px solid #334155; border-radius: 6px; background: #1e293b; color: #f8fafc; font: 12px Inter, system-ui, sans-serif; }
.fx6 select:focus-visible { outline: 2px solid #38bdf8; outline-offset: 1px; }
.fx6 .box { display: flex; justify-content: center; align-items: center; gap: 6px; height: 140px; padding: 8px; border: 1px dashed #475569; border-radius: 10px; }
.fx6 i { width: 40px; min-height: 20px; border-radius: 6px; background: #38bdf8; }
.fx6 i:nth-child(2) { background: #a78bfa; } .fx6 i:nth-child(3) { background: #f472b6; }
.fx6 .box[style*="stretch"] i { height: auto !important; }
var box = document.querySelector('.fx6 .box');
document.querySelectorAll('.fx6 select').forEach(function (s) { s.onchange = function () { box.style[s.dataset.p] = s.value; }; });

07. Vertical Stack with gap

FlexboxSpacing

A column of elements spaced by the container's gap rather than margins on each child, the stack pattern. Spacing lives in one place, the first and last items need no special cases, and a modifier class changes the rhythm for a whole section.

View code
<div class="fx7"><h3>Newsletter</h3><p>One email a month, no tracking pixels.</p><input aria-label="Email" placeholder="you@example.com"><button>Subscribe</button></div>
.fx7 { display: flex; flex-direction: column; gap: 12px; width: 280px; max-width: 100%; box-sizing: border-box; padding: 20px; border: 1px solid #e4e7ec; border-radius: 16px; background: #fff; font: 14px Inter, system-ui, sans-serif; }
.fx7 h3 { margin: 0; color: #101828; } .fx7 p { margin: 0; color: #475467; }
.fx7 input { min-width: 0; padding: 10px; border: 1px solid #98a2b3; border-radius: 8px; font: inherit; }
.fx7 button { padding: 10px; border: 0; border-radius: 8px; background: #7f56d9; color: #fff; font: 700 14px Inter, system-ui, sans-serif; cursor: pointer; }
.fx7 :focus-visible { outline: 3px solid #d6bbfb; outline-offset: 2px; }

About these CSS Flexbox Layouts

Auto margins push

In a flex row, margin-left: auto on an item absorbs all the free space before it, pushing it and its followers to the end. It is cleaner than wrapping groups in extra divs.

min-width: 0 lets items shrink

Flex items will not shrink below their content's minimum width by default, so a long input or word can overflow the row. min-width: 0 on the flexible item fixes it.

flex: 1 versus flex: 1 1 0

flex: 1 starts each item from its content size, so long content gets a wider column. flex: 1 1 0 starts every item from zero and shares the space equally.

gap for spacing

Use gap on the container instead of margins on items. Spacing stays in one place, works when items wrap and needs no first or last child exceptions.

Frequently asked questions

Flexbox or grid?

Flexbox for one direction of content-sized items, grid for two-dimensional layouts with defined tracks.

Why do my flex items overflow?

Usually the default min-width: auto; set min-width: 0 on the item that should shrink.

Does order affect accessibility?

Yes. The order property changes only the visual order, so keyboard focus can jump around; reorder the HTML instead when you can.