Flexbox
Also called flex, display: flex, or flexible box layout
A CSS layout mode that lines items up in a row or a column and controls how they share space, align, and wrap.
import { Menu } from '@base-ui/react/menu';
const LINKS = [
{ href: '/product', label: 'Product' },
{ href: '/pricing', label: 'Pricing' },
{ href: '/docs', label: 'Docs' }
];
export function Navbar() {
return (
// One row: items centered vertically, pushed to the ends and the middle.
<header className='flex h-16 items-center justify-between gap-4 px-6'>
<a href='/' className='font-semibold'>
Tripmate
</a>
<nav className='hidden gap-8 text-sm sm:flex'>
{LINKS.map((link) => (
<a key={link.href} href={link.href} className='hover:underline'>
{link.label}
</a>
))}
</nav>
<div className='flex items-center gap-2'>
<a
href='/signin'
className='rounded-full bg-primary px-4 py-2 text-sm font-medium text-primary-foreground'
>
Sign in
</a>
<Menu.Root>
<Menu.Trigger className='rounded-full border px-4 py-2 text-sm sm:hidden'>
Menu
</Menu.Trigger>
<Menu.Portal>
<Menu.Positioner sideOffset={8} align='end'>
<Menu.Popup className='flex min-w-40 flex-col rounded-xl border bg-background p-1 shadow-lg'>
{LINKS.map((link) => (
<Menu.LinkItem
key={link.href}
href={link.href}
className='rounded-lg px-3 py-2 text-sm outline-none data-highlighted:bg-muted'
>
{link.label}
</Menu.LinkItem>
))}
</Menu.Popup>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>
</div>
</header>
);
}Lay out the navbar with flexbox: display: flex, align-items: center, justify-content: space-between, 64px tall with 24px horizontal padding. Put the logo on the left, the links in the middle in their own flex row with a 32px gap, and the "Sign in" button on the right. Under 640px, hide the links behind a menu button.
Paste into Layerwise or any AI coding tool.
What is Flexbox?
Turn it on with display: flex on a parent, and its children line up along a main axis: a row by default, or a column with flex-direction: column. justify-content spreads them along that axis (start, center, space-between), and align-items positions them across it (top, middle, bottom, or stretched to fill).
Flexbox is the tool for one-dimensional layouts: navigation bars, button groups, a row of avatars, an icon next to a label, or centering something in a box with justify-content: center and align-items: center. Use gap for the space between items and flex-wrap: wrap to let them flow onto new lines.
When rows and columns need to line up with each other, such as a photo gallery or a page layout, CSS Grid is usually simpler. In Tailwind these map to classes like flex, flex-col, justify-between, items-center, and gap-4, which are handy words to use in prompts.
Often confused with
- CSS Grid
- Flexbox arranges items along one line at a time, a row or a column. CSS Grid places items on rows and columns at once, so they line up in both directions.
Related terms
All termsCSS Grid
A CSS layout system that places items on rows and columns at once, so content lines up in both directions.
Padding vs margin
Padding is the space inside an element’s border, around its content, while margin is the space outside the border that pushes other elements away.
Responsive design
Designing one layout that adapts to any screen width, rearranging columns, sizes, and navigation at set widths called breakpoints.
Layout animation
An animation that smoothly moves and resizes elements when the layout changes, such as list items gliding to new spots after a sort or filter.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.