By the end of this lesson, you will:
Make sure you have:
:information_source: Info What is CSS Flexbox? CSS Flexbox is a layout module in CSS that provides a flexible way to distribute and align elements within a container. It makes arranging items in rows or columns super easy!
Think of it like organizing toys on a shelf - you can:
Flexbox helps you create dynamic and responsive layouts. This means your webpage looks good on phones, tablets, and computers!
To create a flexbox layout, follow these simple steps:
:bulb: Tip: Quick Start The magic happens when you add
display: flex
to a container. This turns it into a flex container, and all its children become flex items that you can control!
Important notes:
Traditional layouts use blocks that stack vertically. Flexbox introduces a new way of thinking about layout with two axes:
note Key Concept: Flex Axes
Think of it like reading a book:
Items arrange along the main axis (from main-start to main-end) and align along the cross axis (from cross-start to cross-end).
Try this interactive example: Open in EduBlocks
Code | Output |
---|---|
![]() |
![]() |
Now let's explore the powerful properties you can use on a flex container. Each property controls how items arrange and behave:
Property | Value | Description and Output |
---|---|---|
display | flex | This is the starting point! Add this to create a flex container.![]() ![]() |
Property | Value | Description and Output |
---|---|---|
flex-direction | row (default)row-reversecolumncolumn-reverse | Controls the main axis direction. Like choosing if items line up horizontally or vertically!![]() ![]() ![]() |
Property | Value | Description and Output |
---|---|---|
flex-wrap | nowrapwrapwrap-reverse | Decides if items should wrap to a new line when they run out of space.![]() ![]() |
Property | Value | Description and Output |
---|---|---|
justify-content | flex-startflex-endcenterspace-betweenspace-aroundspace-evenly | Controls spacing along the main axis. Perfect for centering or spreading items!![]() ![]() ![]() |
Property | Value | Description and Output |
---|---|---|
align-items | stretchflex-startflex-endcenter | Controls alignment along the cross axis. Use this to vertically center items!![]() ![]() |
CSS Flexbox makes layout easy! Here's what you learned:
display: flex
to any elementflex-direction
to change item flowjustify-content
to space items perfectlyalign-items
to align items vertically:bulb: Tip: Pro Tip Start simple! Add
display: flex
to a container, then experiment withjustify-content: center
andalign-items: center
to center everything perfectly. It's like magic! :sparkles:
Now it's your turn to practice:
Having issues? Check these common problems:
display: flex
to the parent containerflex-direction
valueflex-wrap: wrap
justify-content: center
AND align-items: center
Remember: Flexbox properties go on the container, not the individual items!