Flexbox is the CSS layout system for arranging items in a row or a column. It answers questions that used to need floats, tables or JavaScript: how do I spread navigation links evenly, how do I put a logo on the left and buttons on the right, how do I make cards line up and stretch to the same height. Once you understand the container and item relationship, most everyday interface layouts become a handful of predictable properties.
This guide builds three real layouts step by step: a centered hero block, a navigation bar, and a responsive card row. It also covers the alignment properties in plain language, because flexbox vocabulary is the main hurdle for beginners. If centering is your specific problem, the dedicated guide to ways to center a div in CSS goes deeper, and for two dimensional page layouts you will eventually want CSS Grid alongside flexbox.
The Core Idea: Container and Items
Setting display flex on an element turns it into a flex container, and its direct children become flex items. The container decides direction and alignment; the items can grow, shrink or keep their natural size. Two axes are always in play. The main axis runs in the direction items are laid out, by default left to right. The cross axis runs across it, by default top to bottom.
.container {
display: flex;
flex-direction: row; /* row | column | row-reverse | column-reverse */
justify-content: flex-start; /* main axis alignment */
align-items: stretch; /* cross axis alignment */
gap: 16px;
}
Flex direction sets the main axis. Justify content distributes items along the main axis, controlling spacing between and around them. Align items positions items on the cross axis, which is how you center things vertically in a row. Gap adds consistent space between items without margins, which removes a whole category of first and last item spacing hacks. Almost everything in flexbox is a combination of these four properties, so time spent understanding them pays off across every layout that follows.
Example 1: Centering Content
The classic flexbox win is centering a box both horizontally and vertically inside a section. With flexbox it takes three lines on the parent, and it works regardless of the child’s size, which is why it replaced older tricks that needed fixed heights or transforms.
.hero {
display: flex;
justify-content: center; /* horizontal centering */
align-items: center; /* vertical centering */
min-height: 60vh;
}
.hero-box {
max-width: 560px;
text-align: center;
padding: 24px;
}
The parent, hero, becomes the flex container and is given a minimum height so there is vertical space to center within. Justify content center packs the child into the middle of the main axis, which is horizontal in a row. Align items center places it in the middle of the cross axis. The child itself needs no positioning at all; its max width keeps text lines readable, and text align centers the words inside the box. Change the parent height or the child content and the centering still holds, which is exactly the behaviour you want for hero sections, empty states and login panels.
Example 2: A Navigation Bar
A navbar is a row with groups: branding on one side, links in the middle or on the other side, and perhaps a button at the far end. Flexbox handles this with space between distribution and a nested flex row for the link list.
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 24px;
gap: 24px;
}
.nav-links {
display: flex;
gap: 20px;
list-style: none;
margin: 0;
padding: 0;
}
.nav-links a {
text-decoration: none;
}
The outer container uses space between, which pushes the first child, the logo, to the start and the last child, the link list or a call to action button, to the end, with remaining space in the middle. Align items center keeps the logo and links on the same visual line even when their heights differ. The inner list is itself a flex container, showing that flex containers nest happily: the navbar arranges groups, the list arranges links. Removing list bullets and default spacing keeps the bar clean. On narrow screens you would typically allow wrapping or switch to a stacked layout, and the patterns in CSS responsive media queries show how to set those breakpoints cleanly.
Pushing One Item to the End
A useful variation is a row where most items sit together and one item sits apart, such as a toolbar with several buttons and a lone delete or settings button on the right. An auto margin on that item absorbs all free space before it, pushing it to the far end without needing space between on the whole row. This trick works because flexbox treats auto margins as space eaters, and it keeps the left group tightly packed, which space between would not.
Example 3: A Responsive Card Row
Cards are where flexbox earns its place on content sites: product cards, article teasers, pricing tiers, feature boxes. The goal is a row on wide screens that wraps gracefully on smaller ones, with every card in a row sharing the same height so buttons line up.
.card-row {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card {
flex: 1 1 260px;
display: flex;
flex-direction: column;
padding: 20px;
border: 1px solid #ddd;
border-radius: 12px;
}
.card .card-text {
flex-grow: 1;
}
Flex wrap allows items to move onto a new line when they no longer fit, instead of squeezing into unreadable slivers. The flex shorthand on each card sets grow, shrink and basis in one line: items may grow to fill a row, may shrink when needed, and aim for a base width of 260 pixels. That single value acts like a soft breakpoint, because cards wrap when the container cannot fit another 260 pixel card plus the gap. Inside each card, a column flex layout with the text block set to grow pushes any footer or button to the bottom, so cards of different text lengths still align. Nesting flex inside flex items, once again, is normal and expected.
Notice what is absent: no percentage maths, no clearing floats, no negative margins. The gap property handles spacing, the basis handles sizing intent, and the browser does the arithmetic. For a card layout that must follow strict columns across many rows, grid is the stronger tool, which is why the two systems are usually learned as a pair.
Controlling Individual Items
Container properties set the overall pattern, but item properties refine it. Flex grow decides how extra space is shared. Flex shrink decides how items give up space when the container is too small. Flex basis is the starting size before growing or shrinking. Align self overrides the container’s align items for one item, handy when a single card or button should sit at the end while siblings center. Order changes visual order without changing the HTML, which should be used carefully because screen readers and keyboard focus still follow the document order, so a large visual reordering can confuse people navigating by keyboard.
.toolbar .spacer {
flex-grow: 1;
}
.card.featured {
align-self: stretch;
order: -1; /* appears first visually */
}
The spacer example shows a common toolbar pattern: an empty flexible element that soaks up space between groups. The featured card example shows order minus one pulling one card to the front of a row for emphasis. Both are small, targeted adjustments rather than new layouts, and that is typical of experienced flexbox use: a simple container pattern, plus one or two item tweaks.
Common Flexbox Mistakes
The most common beginner mistake is applying alignment properties to the wrong element. Justify content and align items belong on the container, not on the item you are trying to move. The second is forgetting that flex items do not collapse margins the way block elements do, and that min width defaults can stop items shrinking below their content size, causing overflow with long words or images; setting min width to zero on the item often fixes stubborn overflow. The third is reaching for fixed heights to force alignment, when align items stretch or a growing inner element would solve it flexibly.
Finally, remember that flexbox is one dimensional. If you find yourself fighting to line items up in both rows and columns at once, with items in row two needing to align with items in row one, that is the signal to switch that section to grid. Many production layouts use grid for the page shell and card galleries, and flexbox for navbars, toolbars, button groups and the insides of cards, each where it is strongest. Defining reusable values such as gap sizes and colours with CSS variables keeps both systems consistent across a site.
Example 4: A Media Object and a Simple Form Row
Two smaller patterns show how often flexbox solves daily details. A media object pairs a fixed image or avatar with flexible text beside it, a pattern used in comments, notifications and article teasers. A form row pairs a label with an input that should take all remaining width, so forms stay aligned without a table or fixed label columns inside every breakpoint.
.media {
display: flex;
gap: 14px;
align-items: flex-start;
}
.media img {
width: 56px;
height: 56px;
border-radius: 50%;
}
.media-body {
flex: 1;
min-width: 0;
}
.form-row {
display: flex;
gap: 12px;
align-items: center;
}
.form-row label {
flex: 0 0 120px;
}
.form-row input {
flex: 1;
min-width: 0;
padding: 10px;
}
In the media example, align items flex start keeps the avatar level with the first line of text instead of stretching it, and the body is allowed to grow while its minimum width is reset to zero so long words or links cannot push the layout wider than its container. In the form example, the label keeps a steady basis and never shrinks, while the input absorbs the rest of the row. On very narrow screens you can switch the form row to a column direction, stacking the label above the field, with the same markup and only a couple of changed values. Patterns like these, repeated across a site, are why a small flexbox vocabulary ends up covering most interface work.
Conclusion
Flexbox becomes straightforward once the container and item roles are clear: direction sets the axis, justify content handles main axis spacing, align items handles cross axis position, gap handles breathing room, and the flex shorthand gives each item its sizing behaviour. The hero, navbar and card row in this guide cover the majority of real interface needs, and the same properties recombine to build footers, media objects, forms and toolbars without new concepts.
Tagged: css css snippets flexbox layout navbar responsive



4 thoughts on “CSS Flexbox Layouts: Center, Navbar and Card Row Examples”