The CSS Flexbox layout is used to create flexible and responsive designs. It helps align items easily in rows or columns, making layouts cleaner and more organized. Flex applies only in child element
Flex properties apply only to the child elements of a flex container.
Key Concepts:
Flex Container: The parent element where Flexbox is applied.
Turns the container into a flexbox.
Default flex direction is row.
The child elements inside the container set in row direction.
display: flex;
Main Concepts
Flex Direction: controls the direction in which items are placed in the container. row is set value in Horizontal layout. column is set value in vertical layout.
flex-direction: row;
Justify Content: Aligns items along the main axis (horizontal by default).
justify-content: center;
Align-items: aligns items along the cross axis (vertical by default).
align-items: center; /* Center items vertically */
Flex-wrap: allows items to wrap onto the next line if they don’t fit in one row/column.
flex-wrap: wrap;
Align-Content: Aligns multiple rows along the cross axis. but it’s only work when flex-wrap is used.
flex-start
,flex-end
,center
,space-between
,space-around
,stretch
.
align-content: space-between; /* Space rows evenly */
How Flex Work?
Flex work on cross axis and main axis. align-items property work on cross axis and justify-content property works on main-axis.
You can easily understand using this diagram.
- Understanding the main axis and cross axis is crucial for controlling how items are laid out and aligned in a flex container. With these axes, you can precisely manage the positioning of items in any layout!
Flexbox Cheat Sheet
Conclusion
If you found this article helpful, Like and share it with others and feel free to leave your feedback—I’d love to hear your thoughts!
I write articles on renilpatel.hashnode.dev and also post development related content on the following platforms: