October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk8 min

CSS Flexbox: A Practical Guide to Every Core Property

A practical CSS Flexbox reference covering the 12 core properties, key defaults, sizing, alignment, wrapping, and examples for common layouts.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Flexbox arranges items along one main axis and aligns them along a perpendicular cross axis. Set display: flex or display: inline-flex on a container; its direct children become flex items. This guide covers the 12 properties in the Flexbox module’s property reference, plus the related gap properties commonly used to space items.

Start with the axes and defaults

Flexbox is a one-dimensional layout model: it lays items out in a row or column, rather than controlling rows and columns as a two-dimensional grid does. The main axis follows flex-direction; the cross axis runs perpendicular to it. These axes are not simply synonyms for horizontal and vertical: writing direction affects what row means.

As an Amazon Associate I earn from qualifying purchases.

Most properties fall into two groups. Container properties set the flow and alignment of the layout; item properties control an individual child’s size, visual order, or alignment.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Role Properties What they control
Container flex-direction, flex-wrap, flex-flow, justify-content, align-items, align-content Axis, line wrapping, and alignment or distribution of items and lines.
Flex item order, flex-grow, flex-shrink, flex-basis, flex, align-self Visual order, size negotiation, and an item’s cross-axis alignment.
Related spacing properties gap, row-gap, column-gap Gutters between items or flex lines; these are useful with Flexbox but are not in the Flexbox module’s core property list.

With only display: flex, the initial behavior is a row with no wrapping. Items do not grow, may shrink, and use an automatic flex basis; they stretch across the cross axis by default. The initial values for justify-content, align-content, and align-items are flex-start, stretch, and stretch respectively. The default flex value is 0 1 auto. See the W3C CSS Flexible Box Layout Module Level 1 and MDN’s basic concepts guide.

Choose the direction and wrapping behavior

flex-direction: choose the main axis

flex-direction sets the main axis and the direction items follow. Its initial value is row, which follows the inline direction of the writing mode—not necessarily left to right.

  • row: main axis follows the inline direction.
  • row-reverse: reverses the visual flow of a row.
  • column: main axis follows the block direction.
  • column-reverse: reverses the visual flow of a column.

Changing from row to column also changes which dimension is the main axis. As a result, main-axis sizing and justify-content follow the column rather than the row.

flex-wrap: keep items on one line or create more

The initial value, nowrap, keeps items on one line. If they do not fit, they may shrink or overflow; they do not automatically move to another line. Set wrap to create additional lines, or wrap-reverse to reverse how those lines stack along the cross axis.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

flex-flow: set direction and wrapping together

flex-flow is shorthand for flex-direction and flex-wrap. Either component can be omitted; the omitted property takes its initial value.

.cards {
  display: flex;
  flex-flow: row wrap;
}

That rule makes a row-oriented container whose items can wrap onto additional lines.

Control how flex items size themselves

Flex sizing is a three-part decision: flex-basis supplies an item’s starting main size, flex-grow shares positive free space, and flex-shrink governs how negative free space is handled. The flex shorthand sets all three in that order.

flex-basis: set the starting main size

flex-basis sets the initial main size used in the flex calculation. It accepts content or a size value such as 12rem or 30%. Its initial value is auto: the browser consults the item’s relevant main-size property and, where appropriate, its content sizing. It is not simply another name for width, because the main axis can be a column.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

flex-grow: divide positive free space

The initial value is 0, so items do not grow by default. A positive non-negative number lets an item claim a proportional share of available positive space. For example, if two eligible items have grow factors of 1 and 2, they divide the available free space in a 1:2 ratio. That ratio describes the added space, not necessarily their final sizes: their starting bases and sizing constraints still matter.

flex-shrink: handle items that do not fit

The initial value is 1. When items exceed the available main-axis space, shrink factors influence how negative free space is removed. The calculation accounts for item bases and minimum sizes, so a factor twice as large does not guarantee exactly twice as many pixels of shrinkage. Content and minimum sizing can affect the result; equal flex values alone do not guarantee equal final widths.

flex: set grow, shrink, and basis

The longhand order is flex-grow, flex-shrink, then flex-basis. The spec’s initial value is 0 1 auto. These named forms are useful shortcuts:

Declaration Expansion Effect
flex: initial 0 1 auto Does not grow, can shrink, and uses an automatic basis.
flex: auto 1 1 auto Can grow or shrink from an automatic basis.
flex: none 0 0 auto Neither grows nor shrinks from an automatic basis.
flex: 1 A one-number shorthand commonly treated as growth with a zero-like basis Can grow to take available space; do not assume it overrides content and minimum-size constraints.

When you want equal-looking columns, flex: 1 is a common starting point, but the result still depends on content and minimum sizing. The shorthand’s serialized representation may use a percentage for the basis.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Align items and distribute free space

Alignment property names describe axes: justify-content works along the main axis; align-items and align-self work across the cross axis for items; align-content distributes multiple lines across that cross axis.

justify-content: place items along the main axis

This container property distributes remaining main-axis space after item sizing. Its initial value is flex-start. Common values include:

  • flex-start: pack items toward the main-start side.
  • flex-end: pack them toward the main-end side.
  • center: group them in the middle.
  • space-between: put free space between items, with no extra space at the ends.
  • space-around: distribute space around each item, leaving half-sized spaces at the ends relative to the gaps between items.
  • space-evenly: distribute equal spaces between items and at both ends.

If items grow enough to consume the available space, justify-content has little or no free space left to distribute. Modern Box Alignment also provides logical alignment forms; choose them when you want alignment expressed relative to writing direction rather than a physical side.

align-items: align items across a line

align-items sets the default cross-axis alignment for items in a flex line. Its initial value is stretch. The Flexbox property table also lists flex-start, flex-end, center, and baseline. Stretching applies when the item’s cross size is automatic and other constraints permit it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

align-self: override alignment for one item

Set align-self on an individual flex item to override its parent’s align-items value. Its initial value, auto, defers to the parent. For example, one item can be centered while its siblings stretch:

.toolbar {
  display: flex;
  align-items: stretch;
}

.toolbar__action {
  align-self: center;
}

align-content: distribute multiple lines

align-content distributes flex lines along the cross axis. It matters when the container has multiple lines and extra cross-axis space; it has no meaningful line-distribution effect for a single line. Its initial value is stretch. Flexbox values include flex-start, flex-end, center, space-between, space-around, and stretch.

For example, a wrapped container needs enough cross-axis room for the lines to be distributed visibly. align-items aligns items within a line; align-content distributes the lines themselves. MDN summarizes the cross-axis relationship: “Flex items can be aligned in the cross axis of the current line of the flex container, similar to justify-content but in the perpendicular direction.” W3C CSS Flexible Box Layout Module Level 1.

Set practical spacing with gap

gap sets gutters between flex items and, in a wrapped layout, between flex lines. Use row-gap and column-gap when you need different spacing by row or column; gap is the shorthand. These properties belong to the broader CSS alignment and spacing family, not the Flexbox module’s 12-property reference list.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
.nav {
  display: flex;
  gap: 1rem;
}

A gap belongs between adjacent items or lines, rather than adding space to each item’s outside edge. Margins remain useful when spacing belongs to a particular box or when you need behavior that a uniform gutter cannot express.

Understand visual order and source order

order: change visual placement

order assigns a flex item to an integer ordering group. Its initial value is 0; items with the same value retain their relative order. Items with lower values are placed before those with higher values in the visual layout.

row-reverse, column-reverse, and order change visual placement, not the document’s source order. Keep the DOM order meaningful for assistive technology and sequential navigation; visual reordering can create a mismatch between what sighted users see and the order other users encounter.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Build a small layout, changing one decision at a time

This example starts with three items. Each step changes one property so the effect is easier to identify.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

1. Reveal the default row

.demo {
  display: flex;
}

.demo > div {
  padding: 1rem;
  border: 1px solid;
}

The direct children line up on a single row, do not grow, may shrink, and stretch across the cross axis when sizing permits.

2. Add a consistent gutter

.demo {
  display: flex;
  gap: 1rem;
}

The items now have a gutter between them without attaching margins to each child.

3. Distribute remaining main-axis space

.demo {
  display: flex;
  gap: 1rem;
  justify-content: space-between;
}

With items that do not grow to fill the row, space-between places the remaining space between them.

4. Let the items grow

.demo > div {
  flex: 1;
}

The items can now grow to use available space. Because growth consumes free space, justify-content may have little left to distribute.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

5. Allow another line

.demo {
  display: flex;
  flex-flow: row wrap;
  gap: 1rem;
  align-content: space-between;
}

If items wrap into multiple lines and the container has extra cross-axis room, align-content distributes those lines. Without multiple lines and available cross-axis space, it will not produce a visible distribution.

6. Align just one item differently

.demo > div:last-child {
  align-self: center;
}

The final item uses its own cross-axis alignment rather than the container’s default.

Quick choices for common Flexbox tasks

Goal Starting point What to check
Center items horizontally in a row display: flex; justify-content: center; In a column, the main axis is vertical instead; writing direction also affects row start and end.
Center items across the cross axis align-items: center; This centers items within a line; it does not distribute multiple lines.
Center one child across the cross axis align-self: center; Apply it to the flex item, not its parent.
Make a row of items wrap flex-flow: row wrap; Check that the container has a constrained main-axis size so wrapping can occur.
Space items evenly with a gutter gap: 1rem; Use justify-content separately if you also need to distribute leftover main-axis space.

Why flex items may not wrap

  • The container still has flex-wrap: nowrap. Set flex-wrap: wrap or use flex-flow: row wrap.
  • The container has enough main-axis space for the items at their current sizes. Wrapping occurs when items cannot fit on a line under the layout constraints.
  • The main axis is not the one you assumed. Check flex-direction; a column’s main axis is not the row direction.
  • The children are not direct flex items. Only a flex container’s direct children become flex items.

For property definitions and the complete Flexbox model, consult the MDN Flexbox guide, its basic concepts, and the W3C specification.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.