CSS Flexbox gives you precise control over the direction, spacing, and alignment of elements in a one-dimensional layout. You set the main axis with flex-direction, manage spacing with justify-content and gap, and align items on the cross axis with align-items and align-self. This article explains each property, how they work together, and common pitfalls to avoid.
Background: Why Flexbox Exists
Before Flexbox, laying out items in a row or column required floats, inline-block hacks, or tables — all fragile and hard to center vertically. Flexbox (Flexible Box Layout) was designed for one-dimensional layouts: either a row or a column. It distributes space along that single axis and aligns items perpendicular to it. The module became widely supported in browsers around 2015, and it remains the go-to tool for component-level layout, while CSS Grid handles two-dimensional page structure.
How Flexbox Works: The Two Axes
Every flex container has two axes. The main axis follows flex-direction (row by default, left-to-right). The cross axis is perpendicular. For a row, the cross axis is vertical; for a column, it’s horizontal.
flex-direction: row— main axis left to right (default)flex-direction: row-reverse— main axis right to leftflex-direction: column— main axis top to bottomflex-direction: column-reverse— main axis bottom to top
Changing the direction also flips which properties affect which axis. For example, justify-content always works on the main axis, while align-items works on the cross axis.
Controlling Direction
Set display: flex on the container, then choose a direction. The flex-direction property accepts four values. The row-reverse and column-reverse values also reverse the order of items visually, but they do not change the DOM order — screen readers and keyboard navigation still follow the source order. For example, a navigation bar with flex-direction: row-reverse will place the first item on the right, which is useful for right-to-left language layouts.
You can also use the dir attribute or the writing-mode CSS property to change the block flow, but flex-direction is the most direct way to control the main axis.
Spacing Along the Main Axis: justify-content
The justify-content property distributes space between and around items on the main axis. It accepts these values:
flex-start(default) — items packed at the startflex-end— items packed at the endcenter— items centeredspace-between— first item at start, last at end, equal space betweenspace-around— equal space around each item (half-size at edges)space-evenly— equal space between all items including edges
For example, a toolbar with three buttons and justify-content: space-between will push the first button to the left, the last to the right, and center the middle one. To add fixed space between items regardless of container width, use the gap property: gap: 16px adds 16 pixels between each item without affecting the edges.
Alignment on the Cross Axis: align-items and align-self
The align-items property controls alignment on the cross axis. For a row, this is vertical; for a column, it’s horizontal. Values include:
stretch(default) — items stretch to fill the container’s cross sizeflex-start— align to the start of the cross axisflex-end— align to the endcenter— center along the cross axisbaseline— align items by their text baseline
To override the alignment for a single item, use align-self on that item. For instance, in a row of buttons, you can set align-self: flex-end on one button to push it to the bottom while others stay centered.
For multi-line flex containers (when flex-wrap: wrap is set), align-content controls spacing between lines on the cross axis. It accepts the same values as justify-content, plus stretch.
How to Choose the Right Property
Ask two questions: Which axis am I working on? and Do I want to space items or align them?
- Main axis spacing →
justify-content - Cross axis alignment →
align-items(oralign-selffor one item) - Fixed gaps between items →
gap(works on both axes) - Multi-line cross-axis distribution →
align-content
A common mistake is using justify-content to center vertically in a row. That won’t work because the main axis is horizontal; you need align-items: center for vertical centering. Conversely, in a column, justify-content: center centers vertically, and align-items: center centers horizontally.
Common Problems and Misconceptions
One frequent issue is that align-items: stretch (the default) makes items fill the cross axis, which can cause unexpected heights. If you want items to size to their content, set align-items: flex-start.
Another misconception: flex: 1 does not mean “take one unit of space.” It’s shorthand for flex: 1 1 0%, meaning the item can grow and shrink equally from a base size of zero. To make items equal width, use flex: 1 on each item.
Also, justify-content: space-between with only one item will place it at the start, not center. If you need a single item to center, use justify-content: center or add a dummy item.
Frequently Asked Questions
How do I center a div both horizontally and vertically with Flexbox?
Set the parent to display: flex, then use justify-content: center (horizontal) and align-items: center (vertical). This works for any number of items, and it’s the simplest method for centering content.
What’s the difference between justify-content and align-items?
justify-content controls spacing along the main axis (the direction of flex-direction). align-items controls alignment along the cross axis (perpendicular). For a row, justify-content is horizontal and align-items is vertical; for a column, it’s reversed.
Why is my gap property not working?
Ensure you’re using a modern browser (Chrome 84+, Firefox 63+, Safari 14.1+). Also, gap works on flex containers, but not on all older versions. If you need legacy support, use margins on items instead.
How do I make flex items wrap to a new line?
Add flex-wrap: wrap to the container. Items will wrap onto multiple lines along the main axis. Use align-content to control spacing between those lines.
Can I use Flexbox for a full page layout?
Yes, but for complex two-dimensional layouts (rows and columns simultaneously), CSS Grid is often more appropriate. Flexbox excels at one-dimensional layouts like navbars, toolbars, card rows, or aligning items within a component.
What does flex: 1 actually do?
It’s shorthand for flex: 1 1 0%, meaning the item can grow (flex-grow: 1), shrink (flex-shrink: 1), and has a base size of 0. This makes all items with flex: 1 equal width or height, filling the container.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Disable Autocorrect on iPhone or iPad: Quick Guide
- Custom Keyboard Shortcuts on Windows 11 & macOS
- Restore Windows 11 Display Scaling to Recommended
- Semiconductor Cycle: How Investors Track It0.0
- Microsoft Store Error 0x80073d12: Fixes That Work
- View Notification Count on iPhone: Full Guide