CSS Flexbox arranges items along one axis at a time: a row or a column. Set display: flex on a parent to make its in-flow children flex items, then use axis-aware properties to control their size, spacing, alignment, and wrapping. This guide builds a navigation bar and a wrapping card group, explains the flex shorthand, and shows when CSS Grid is a better fit.
How Flexbox works: container, items, and axes
A flex layout has a parent called the flex container and its in-flow children called flex items. The container establishes the layout context; properties on it arrange the items, while properties on individual items can adjust their sizing or alignment.
The flex-direction property establishes the main axis. The cross axis runs perpendicular to it. The default direction is row, so the main axis follows the row direction and the cross axis runs across it. With column, those roles rotate. Thinking in terms of axes is more reliable than saying “horizontal” and “vertical,” especially when direction changes or writing modes matter.
.toolbar {
display: flex;
flex-direction: row;
}
.toolbar--stacked {
flex-direction: column;
}
In the first example, the toolbar’s children are flex items arranged along a row. In the second, they are arranged along a column. The properties that align items follow the axes set by flex-direction.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →#1 Best Overall
- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
For background and the full learning path, see MDN’s Flexbox guide and its flexible box layout reference. W3C describes Flexbox as “a CSS box model optimized for user interface design” in the CSS Flexible Box Layout Module Level 1. The cited W3C document is a Candidate Recommendation Draft dated 14 October 2025; W3C notes that the draft is work in progress and may be updated, replaced, or obsoleted.
Build a one-line navigation bar
Start with semantic HTML and apply Flexbox to the element whose children should be laid out together. Here, the navigation element is the container and its links are the flex items.
<nav class="site-nav" aria-label="Main navigation">
<a href="/">Home</a>
<a href="/guides/">Guides</a>
<a href="/about/">About</a>
</nav>
.site-nav {
display: flex;
flex-direction: row;
align-items: center;
gap: 1rem;
padding: 1rem;
border-bottom: 1px solid #ccc;
}
.site-nav a {
color: #164b8b;
}
display: flex makes the navigation a flex container. gap creates consistent space between its items without adding margins to each link. Here, align-items: center aligns items on the cross axis. Because the direction is row, that is the perpendicular axis, not the axis along which the links are placed.
Align and space items along the axes
Use justify-content to distribute items along the main axis. Use align-items to align all items on the cross axis, or align-self to override that cross-axis alignment for one item. The axes depend on flex-direction; these properties do not always correspond to horizontal and vertical alignment.
Recommended Free Tools
Rank #2
- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
| Property | Applies to | Useful values | What it controls |
|---|---|---|---|
justify-content |
Flex container | flex-start, center, space-between, space-around, space-evenly |
How items are positioned or distributed along the main axis. |
align-items |
Flex container | stretch, flex-start, center, flex-end |
Default cross-axis alignment for the container’s items. |
align-self |
Flex item | auto, stretch, flex-start, center, flex-end |
Cross-axis alignment for an individual item. |
gap |
Flex container | A length such as 1rem, or row and column gaps |
Space between items or flex lines without adding space at the container’s outside edges. |
For example, a toolbar can keep one item at the start and push another to the far end by letting the middle space expand:
.toolbar {
display: flex;
align-items: center;
gap: 1rem;
}
.toolbar__spacer {
flex-grow: 1;
}
The spacer grows to take available positive free space, separating the other items. If you instead need equal distribution across the main axis, try justify-content: space-between. Choose the behavior that matches the design rather than adding arbitrary margins to individual items.
Wrap a group of cards onto additional lines
Flex items remain on one line by default. Set flex-wrap: wrap when a group should continue on additional lines as space runs out. Each resulting flex line is handled as a line in the cross direction; wrapping does not turn Flexbox into a layout system that coordinates both rows and columns as a grid.
.card-list {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 14rem;
}
This gives each card a starting basis of 14rem, allows it to grow when there is room, and allows it to shrink when needed. When the line cannot accommodate the items at their preferred sizes, items can move onto another flex line. The exact result also depends on the available container width and other size constraints.
Free tools Windows power users keep installed
One-click scans. No signup required.
Rank #3
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
flex-flow is shorthand for setting both direction and wrapping. For instance, flex-flow: row wrap is equivalent to flex-direction: row; flex-wrap: wrap;.
Understand flexible sizing and the flex shorthand
The three sizing components are flex-grow, flex-shrink, and flex-basis. The basis is the item’s starting main-axis size before free space is distributed. If there is positive free space, grow factors determine relative shares; if there is not enough space, shrink factors control reduction. The final size can also be constrained by other sizing rules on the item.
flex-growcontrols how an item shares positive free space with other growable items. A value of0means it does not grow on that basis; a positive value makes it eligible to grow.flex-shrinkcontrols how an item participates in reducing sizes when the line has insufficient space. A value of0opts it out of shrinking; a positive value permits shrinking.flex-basissets the starting size along the main axis. It can be a length such as12rem, a percentage, orauto.
The flex shorthand sets these components together. Common forms are:
| Declaration | Equivalent factors | Typical effect |
|---|---|---|
flex: initial |
0 1 auto |
Items use their automatic basis, can shrink, and do not grow into free space. |
flex: auto |
1 1 auto |
Items use their automatic basis and can both grow and shrink. |
flex: none |
0 0 auto |
Items use their automatic basis and neither grow nor shrink. |
flex: 2 |
Numeric factor with a zero basis | Sets a grow factor of 2; the shorthand’s omitted components use the specification’s numeric-factor behavior. |
flex: 1 1 12rem |
Grow 1, shrink 1, basis 12rem | Starts at the stated basis and can grow or shrink with the line. |
The first three mappings and the role of grow and shrink follow the W3C specification’s basic values and sizing definitions in the Flexbox specification. Prefer an explicit three-part shorthand when the intended starting size matters; it makes the sizing behavior easier to read and debug.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Rank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
Center a component with Flexbox
To center a component in both directions, make its parent the flex container and center on each axis. Give the container a height or minimum height if it needs a visible area in which to center the child.
.stage {
display: flex;
justify-content: center;
align-items: center;
min-height: 20rem;
}
.dialog {
width: min(100%, 28rem);
}
With the default row direction, justify-content centers along the main axis and align-items centers along the cross axis. If you switch the parent to flex-direction: column, the main and cross axes swap roles, so the same property names center along different physical directions.
Choose Flexbox or Grid
Flexbox is one-dimensional: it arranges and sizes items along a main axis, with a cross axis for alignment and wrapped lines. CSS Grid is two-dimensional: it can control rows and columns together. Choose based on the relationships the design needs, not on which tool is newer or more familiar. MDN explains this distinction in its CSS layout guide; its layout cookbook compares practical layout patterns.
| Question | Flexbox is a natural fit when… | Grid is a natural fit when… |
|---|---|---|
| What is the relationship? | Items primarily form one row or one column, such as navigation links or toolbar controls. | The design coordinates rows and columns as a two-dimensional layout. |
| What should sizing respond to? | Items should share or give up space along one direction. | Rows and columns need controlled tracks or coordinated placement. |
| What should happen when space runs out? | Items should wrap into additional flex lines. | Items need explicit two-dimensional placement or aligned row and column structure. |
Both tools can be appropriate for some patterns. If the layout’s key requirement is aligned columns across multiple rows, Grid is often clearer than trying to coordinate independently wrapped flex lines. If the content is a single flow of items that can wrap naturally, Flexbox is often simpler.
Best Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Debug a Flexbox layout in browser DevTools
- Select the intended parent. In the Elements or Inspector panel, verify that the element you expect to control the layout has
display: flexordisplay: inline-flex. - Confirm the items. Check which in-flow children are direct children of that container. A nested element is not a flex item of an outer container unless it is itself a direct child.
- Read the axes. Inspect the computed
flex-direction. Determine the main and cross axes from that value before changing alignment properties. - Check available space and wrapping. Inspect the container’s size, item bases, and
flex-wrap. A line that has no free space cannot distribute positive space through growth; insufficient room can cause shrinking or wrapping. - Change one value at a time. Toggle
justify-content,align-items,gap, or the relevantflexcomponent in the Styles panel, then observe the result before editing your stylesheet. - Check computed sizing and constraints. Look for explicit widths, min/max sizes, and content that resists fitting; these can affect the outcome even when the flex factors seem reasonable.
MDN’s CSS debugging guide covers browser DevTools and compatibility information. Use the browser’s own computed-style view to diagnose the page and check compatibility for the browsers your project supports.
Or skip the browser setup
If you want a rendered screenshot of a page to inspect or share while checking a layout, ScreenshotNeo can return one with a GET request. It is a website screenshot API and MCP server; it is separate from the CSS and DevTools steps above.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
See the ScreenshotNeo API documentation for request options. ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before a capture; each step can be turned off. Bot checks, blank pages, timeouts, and failed loads are not billed, and cache hits cost nothing; responses include X-Page-Verdict and X-Billed headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots.
Sign up for 1,000 free screenshots a month with no card.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Frequently Asked Questions
Does setting display: flex affect every descendant?
No. The flex items are the container’s in-flow children, not every nested descendant.
Can Flexbox make a true two-dimensional grid?
No. Flexbox lays out one axis at a time; use CSS Grid when the design needs coordinated row and column control.
Quick Recap
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.

