


What is the difference between the main axis and the cross axis in Flexbox?
Jul 01, 2025 am 12:21 AMThe main axis in Flexbox is determined by the flex-direction property, running horizontally with row and vertically with column, while the cross axis is always perpendicular to it. The main axis is the direction in which flex items are laid out, set using flex-direction: row (horizontal) or flex-direction: column (vertical). The cross axis is perpendicular to the main axis—vertical when the main axis is horizontal, and horizontal when the main axis is vertical. Justify-content aligns items along the main axis, controlling horizontal alignment with row and vertical alignment with column, with values like flex-start, center, space-between, and space-around. Align-items aligns items along the cross axis, affecting vertical alignment with row and horizontal alignment with column. Align-self overrides align-items for individual items on the cross axis, and align-content adjusts spacing between multiple lines of items on the cross axis when flex-wrap is used.
When working with Flexbox, you’ll often hear terms like main axis and cross axis. Understanding the difference between them is key to controlling how elements align and distribute space inside a flex container.
The main axis is the primary direction in which flex items are laid out. It’s defined by the flex-direction
property. If you set flex-direction: row
, the main axis runs horizontally. If you use flex-direction: column
, the main axis runs vertically.
The cross axis, on the other hand, is perpendicular to the main axis. So if your main axis is horizontal (row), the cross axis will be vertical — and vice versa.
Once that’s clear, the rest of Flexbox makes a lot more sense. Let’s break it down further with some practical points.
How justify-content
works along the main axis
This property aligns items along the main axis. Think of it as adjusting spacing and positioning in the direction your flex items flow.
- If you’re using
flex-direction: row
,justify-content
controls horizontal alignment. - With
flex-direction: column
, it affects vertical alignment.
Common values include:
-
flex-start
(default): items line up at the start -
center
: centered within the container -
space-between
: first and last items stick to edges, others spaced evenly -
space-around
: each item gets equal space around it
So if your items aren’t lining up the way you expect, double-check both flex-direction
and justify-content
.
Aligning items with align-items
on the cross axis
Now this one trips people up sometimes. The align-items
property works across the cross axis, meaning it adjusts how items are positioned in the opposite direction of the main axis.
Let’s say you're using flex-direction: row
. In that case:
- Main axis = left to right
- Cross axis = top to bottom
Soalign-items
will affect vertical alignment.
If you switch to flex-direction: column
, the cross axis becomes horizontal, and align-items
now affects horizontal alignment.
Try setting align-items: center
for nicely centered content — just keep in mind that its effect changes based on the flex direction.
What about align-self
and align-content
?
These two are worth mentioning because they also relate to the cross axis.
-
align-self
: overridesalign-items
for individual items. You can shift one item differently from the rest across the cross axis. -
align-content
: only matters when there are multiple lines in a flex container (flex-wrap: wrap
). It controls how those lines are spaced along the cross axis.
Both give you finer control, but they only work if your layout actually has room to adjust — like wrapping items or varied heights/widths.
At this point, you should have a better feel for how the main and cross axes shape layout behavior in Flexbox. Once you get used to how flex-direction
sets the main axis and everything else flows from that, these properties become much easier to use.
You don't need to memorize every detail — just remember:
- Main axis = direction of flow
- Cross axis = the perpendicular direction
And from there,justify-content
works on the main,align-items
on the cross.
That’s basically it.
The above is the detailed content of What is the difference between the main axis and the cross axis in Flexbox?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to flexibly use the position attribute in H5. In H5 development, the positioning and layout of elements are often involved. At this time, the CSS position property will come into play. The position attribute can control the positioning of elements on the page, including relative positioning, absolute positioning, fixed positioning and sticky positioning. This article will introduce in detail how to flexibly use the position attribute in H5 development.

CSS layout attribute optimization tips: positionsticky and flexbox In web development, layout is a very important aspect. A good layout structure can improve the user experience and make the page more beautiful and easy to navigate. CSS layout properties are the key to achieving this goal. In this article, I will introduce two commonly used CSS layout property optimization techniques: positionsticky and flexbox, and provide specific code examples. 1. positions

HTML Tutorial: How to Use Flexbox for Vertical Height Layout In web development, layout has always been an important issue. Especially when it is necessary to implement vertical equal-height layout, the traditional CSS layout method often encounters some difficulties. This problem can be easily solved using Flexbox layout. This tutorial will introduce in detail how to use Flexbox for vertical equal height layout and provide specific code examples. Flexbox is a new feature in CSS3 that can be used to create flexible, responsive layouts.

HTML tutorial: How to use Flexbox for adaptive equal-height, equal-width, equal-spacing layout, specific code examples are required. Introduction: In modern web design, layout is a very critical factor. For pages that need to display a large amount of content, how to reasonably arrange the position and size of elements to achieve good visibility and ease of use is an important issue. Flexbox (flexible box layout) is a very powerful tool through which various flexible layout needs can be easily realized. This article will introduce Flexbox in detail

How to use CSS3’s flexbox technology to achieve even distribution of web content? With the development of web design, people have higher and higher requirements for web page layout. In order to achieve even distribution of web content, CSS3's flexbox technology has become a very effective solution. This article will introduce how to use flexbox technology to achieve even distribution of web content, and give some practical examples. 1. What is flexbox technology? Flexbox (elastic layout) is a new feature added in CSS3.

HTML Tutorial: How to Use Flexbox for Evenly Distributed Layout Introduction: In web design, it is often necessary to layout elements. Traditional layout methods have some limitations, and Flexbox (flexible box layout) is a layout method that can provide more flexibility and power. This article will introduce how to use Flexbox to achieve even distribution layout, and give specific code examples. 1. Introduction to Flexbox Flexbox is a flexible box layout model introduced in CSS3, which allows elements to

HTML tutorial: How to use Flexbox for adaptive equal-height layout, specific code examples are required. Introduction: In web design and development, implementing adaptive equal-height layout is a common requirement. Traditional CSS layout methods often face some difficulties when dealing with equal height layout, and Flexbox layout provides us with a simple and powerful solution. This article will introduce the basic concepts and common usage of Flexbox layout, and give specific code examples to help readers quickly master the use of Flexbox to implement their own

HTML tutorial: How to use Flexbox for scalable equal-height layout, specific code examples are required. Introduction: In web page layout, we often encounter the need to achieve equal-height layout effects. The traditional method is more cumbersome and needs to be implemented using JavaScript or table layout. Using Flexbox, you can easily implement scalable equal-height layouts without relying on other technologies. This article will introduce how to use Flexbox to implement scalable equal-height layout, and attach detailed code examples. one,
