Flexbox in CSS
Flex Container and Flex Items
Flexbox is built on two key components: the Flex Container and the Flex Items. Understanding their roles and how they interact is essential for mastering Flexbox.
Flex Container
The Flex Container is the parent element that contains and organizes the Flex Items. To create a Flex Container, we need to apply the display
property with the values flex
or inline-flex
to an element.
css
The display: flex;
makes the container behave as a block-level flex container, while display: inline-flex;
makes the flex container behave as an inline flex container.
Flex Container Example
html
css
[Placeholder: Image of a flex container with three items inside, distributed horizontally with space between them]
Flex Items
The Flex Items are the direct children of the Flex Container. These items are arranged within the container based on the rules defined by Flexbox.
Flex Items Example
Continuing with the previous example, the flex items are the div
s within the flex-container
.
html
In this case, flex-container
is the Flex Container and each flex-item
is a Flex Item.
Properties Applicable to the Flex Container
Here are some key properties applicable to the Flex Container that define how the Flex Items are arranged:
flex-direction
: Defines the direction of the main axis.flex-wrap
: Allows the Flex Items to wrap onto multiple lines.justify-content
: Aligns the Flex Items along the main axis.align-items
: Aligns the Flex Items along the cross axis.align-content
: Aligns the lines of Flex Items along the cross axis when there is extra space in the container.
Example of Flex Container Properties Usage
css
Properties Applicable to the Flex Items
Here are some key properties applicable to the Flex Items:
order
: Changes the order of the Flex Items.flex-grow
: Allows the Flex Items to grow if necessary.flex-shrink
: Allows the Flex Items to shrink if necessary.flex-basis
: Defines the flexible basis (initial size) of an item.flex
: Is a shorthand forflex-grow
,flex-shrink
, andflex-basis
.align-self
: Allows the alignment of a single Flex Item along the cross axis to be overridden.
Example of Flex Items Properties Usage
css
Complete Example
html
[Placeholder: Image showing the flex container with three items having different order
and flex-grow
settings, distributed in a way that illustrates how they affect each other]
Setting up the Flex Container and its items enables you to create highly flexible and adaptable layouts. As we progress, we will explore these properties in more detail so you can fully leverage Flexbox in your projects.
- Introduction to Flexbox in CSS
- Basic Concepts of Flexbox
- Flex Container and Flex Items
- Direction of Main Axis and Cross Axis
- Flex-direction Property
- Property flex-wrap
- Justify-content Property
- Align-items Property
- align-content Property
- Property flex-grow, flex-shrink, and flex-basis
- Flex Property and Shorthand
- Ordering Elements with `order`
- Individual Alignment with align-self
- Flexbox and Responsive Design
- Conclusions and Best Practices with Flexbox