0% completed
Flexbox makes aligning and distributing space among items easier. The key concept in flexbox is understanding the two axes: the main axis and the cross axis.
Main Axis:
flex-direction
property (default is row, meaning left to right).Cross Axis:
Knowing the difference between these axes helps you control the alignment and distribution of your flex items precisely.
Follow the code block below to create a basic flex container and see how the axes work:
/* Set up a flex container */ .container { display: flex; /* Enables flexbox layout */ flex-direction: row | column; /* Defines the main axis direction */ }
Explanation:
row
(default), items are arranged along the horizontal (main) axis and the vertical direction becomes the cross axis.column
, items are arranged along the vertical (main) axis and the horizontal direction becomes the cross axis.In this example, the flex container arranges its items in a row (default behavior). The main axis runs horizontally from left to right, while the cross axis runs vertically.
Explanation:
.container
, items are arranged from left to right along the horizontal main axis.align-items
.In this example, we change the flex-direction
to column
. Now, the main axis runs vertically from top to bottom, while the cross axis runs horizontally.
Explanation:
flex-direction: column
, the items stack vertically from top to bottom.This lesson helps you understand the core concepts of flexbox by distinguishing between the main axis and the cross axis. With these fundamentals, you are ready to explore how other container and item properties work in flexbox layouts.
.....
.....
.....