CSS Flexbox
Flexbox is a powerful layout module in CSS that simplifies arranging items within a container. It offers flexibility in controlling alignment, direction, and order, making it ideal for both small-scale component layouts and complex application designs.
Here’s a breakdown of key Flexbox concepts:
1. Flex Container:
The parent element containing the flex items. You enable flexbox by setting the display
property to flex
or inline-flex
.
.container {
display: flex; /* or inline-flex */
}
2. Flex Items:
The direct children of the flex container. Their layout is controlled by the flex container’s properties.
3. Main Axis & Cross Axis:
Flexbox lays out items along two axes:
- Main Axis: The primary direction items are placed (default is row, horizontally).
- Cross Axis: The perpendicular direction to the main axis.
4. Key Flex Container Properties:
flex-direction
: Determines the main axis direction. Common values:row
(default): left to rightrow-reverse
: right to leftcolumn
: top to bottomcolumn-reverse
: bottom to top
justify-content
: Aligns items along the main axis. Common values:flex-start
(default): items at the start of the main axisflex-end
: items at the end of the main axiscenter
: items at the center of the main axisspace-between
: items evenly distributed, space between themspace-around
: items evenly distributed, space around each item
align-items
: Aligns items along the cross axis. Common values:flex-start
: items at the start of the cross axisflex-end
: items at the end of the cross axiscenter
: items at the center of the cross axisstretch
(default): items stretched to fill the cross axis
flex-wrap
: Controls whether items wrap onto multiple lines.nowrap
(default): items stay on one line, may overflowwrap
: items wrap onto multiple lineswrap-reverse
: items wrap onto multiple lines in reverse order
5. Key Flex Item Properties:
flex-grow
: How much an item can grow relative to other items.flex-shrink
: How much an item can shrink relative to other items.flex-basis
: The initial size of the item.order
: Controls the order in which items appear (lower numbers appear first).align-self
: Allows overriding thealign-items
property for individual items.
Example:
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
</div>
.container {
display: flex;
justify-content: space-between; /* Space between items */
align-items: center; /* Center vertically */
}
.item {
background-color: lightblue;
padding: 10px;
border: 1px solid blue;
}
This example creates a flex container with three items spaced evenly apart and centered vertically. Experimenting with the different properties will give you a better understanding of how Flexbox works. Flexbox’s intuitive syntax and powerful features make it a must-know for modern web development.