Getting Started with CSS Flexbox
1. Flex Layout Basics
Flex (Flexible Box Layout) is a powerful and commonly used layout method that makes it easy to align elements, adjust spacing, and create responsive designs. Below, I’ll start from the basics, introduce common techniques, and end with a simple example for practice.
Basic Concepts
- Flex Container: The element with
display: flex
applied. - Flex Items: The direct child elements inside the container.
- Main Axis: The default horizontal direction, determining the primary arrangement of Flex items.
- Cross Axis: The direction perpendicular to the main axis.
Enabling Flex
1 |
|
With this, all direct child elements of .container
become Flex items, arranged horizontally by default.
2. Common Flex Properties
Flex layout is divided into container properties (applied to the parent element) and item properties (applied to the child elements). I’ll cover the most commonly used ones first.
Container Properties
flex-direction
: Defines the main axis directionrow
(default): Horizontal, left to rightrow-reverse
: Horizontal, right to leftcolumn
: Vertical, top to bottomcolumn-reverse
: Vertical, bottom to top1
2
3
4.container {
display: flex;
flex-direction: column;
}
justify-content
: Alignment along the main axisflex-start
: Align to the startflex-end
: Align to the endcenter
: Center alignmentspace-between
: Justify with equal space between itemsspace-around
: Equal space around each item1
2
3
4.container {
display: flex;
justify-content: space-between;
}
align-items
: Alignment along the cross axisflex-start
: Align to the start of the cross axisflex-end
: Align to the end of the cross axiscenter
: Center alignmentstretch
(default): Stretch to fill the cross axis1
2
3
4.container {
display: flex;
align-items: center;
}
flex-wrap
: Whether items wrap to a new linenowrap
(default): No wrapping, items shrink to fitwrap
: Wrap to the next linewrap-reverse
: Wrap and reverse the order1
2
3
4.container {
display: flex;
flex-wrap: wrap;
}
Item Properties
flex-grow
: How items distribute extra space- Default is
0
, meaning no extra space is taken. - Set to
1
to evenly distribute remaining space.1
2
3.item {
flex-grow: 1;
}
- Default is
flex-shrink
: How items shrink when space is limited- Default is
1
, allowing items to shrink. - Set to
0
to prevent shrinking.1
2
3.item {
flex-shrink: 0;
}
- Default is
flex-basis
: Initial size of an item- Can be a specific value (e.g.,
200px
) orauto
.1
2
3.item {
flex-basis: 200px;
}
- Can be a specific value (e.g.,
flex
: Shorthand forflex-grow
,flex-shrink
, andflex-basis
- Common usage:
flex: 1
(equivalent toflex: 1 1 0%
).1
2
3.item {
flex: 1;
}
- Common usage:
3. Simple Practice Example
Let’s create a basic navigation bar using Flexbox to achieve horizontal alignment, centered items, and even distribution.
index.html
1 |
|
style.css
1 |
|
Explanation of the Effect
- The
.nav
container enables Flex, and its child elements (.nav-item
) are arranged horizontally. justify-content: space-around
evenly distributes the navigation items.align-items: center
vertically centers the items.- Additional styles (like background color and hover effect) make it look more like a navigation bar.
4. Practice Suggestions
- Try changing
flex-direction
tocolumn
and see the result.
style.css
1 |
|
- Switch
justify-content
toflex-end
orcenter
and observe the changes.
flex-end
style.css
1 |
|
center
style.css
1 |
|
- Add
flex-grow: 1
to a specific.nav-item
and see how it takes up the remaining space.
style.css
1 |
|
Flexbox becomes very intuitive with practice, and I recommend experimenting with different combinations. Feel free to ask me if you have any questions!