Flexbox In-Depth Documentation
Flexbox is a CSS layout module that makes it easier to design flexible and responsive layout structures without using float or positioning. It is especially useful for arranging elements in a predictable way when the size of the container or the items is unknown or dynamic.
Basic Concepts of Flexbox
Flex Container and Flex Items
A flex container is defined by setting the
display
property toflex
orinline-flex
.The children of a flex container are called flex items.
Main Axis and Cross Axis
- The main axis is the primary axis along which flex items are laid out. The cross axis is perpendicular to the main axis.
Flex Container Properties
display
display: flex;
ordisplay: inline-flex;
Defines a flex container and enables a flex context for all its direct children.
flex-direction
Defines the direction in which the flex items are placed in the flex container.
Possible values:
row
(default): left to rightrow-reverse
: right to leftcolumn
: top to bottomcolumn-reverse
: bottom to top
Example:
.container { display: flex; flex-direction: row; }
flex-wrap
Controls whether the flex container is single-line or multi-line, and the direction of the cross-axis.
Possible values:
nowrap
(default): single-linewrap
: multi-linewrap-reverse
: multi-line with reverse order
Example:
.container { display: flex; flex-wrap: wrap; }
justify-content
Defines the alignment along the main axis.
Possible values:
flex-start
(default): items are packed toward the start of the main axisflex-end
: items are packed toward the end of the main axiscenter
: items are centered along the main axisspace-between
: items are evenly distributed, with the first item at the start and the last item at the endspace-around
: items are evenly distributed with equal space around themspace-evenly
: items are evenly distributed with equal space between them
Example:
.container { display: flex; justify-content: space-between; }
align-items
Defines the default behavior for how flex items are laid out along the cross axis.
Possible values:
stretch
(default): items stretch to fill the containerflex-start
: items are packed toward the start of the cross axisflex-end
: items are packed toward the end of the cross axiscenter
: items are centered along the cross axisbaseline
: items are aligned along their baseline
Example:
.container { display: flex; align-items: center; }
align-content
Aligns a flex container’s lines within the flex container when there is extra space in the cross axis.
Possible values:
stretch
(default)flex-start
flex-end
center
space-between
space-around
space-evenly
Example:
.container { display: flex; align-content: space-around; }
Flex Item Properties
order
Controls the order in which flex items appear within the flex container.
Default value is
0
.Example:
.item { order: 2; }
flex-grow
Defines the ability for a flex item to grow if necessary.
Default value is
0
.Example:
.item { flex-grow: 1; }
flex-shrink
Defines the ability for a flex item to shrink if necessary.
Default value is
1
.Example:
.item { flex-shrink: 1; }
flex-basis
Defines the default size of an element before the remaining space is distributed.
Default value is
auto
.Example:
.item { flex-basis: 200px; }
flex
A shorthand for
flex-grow
,flex-shrink
, andflex-basis
.Example:
.item { flex: 1 0 200px; /* flex-grow | flex-shrink | flex-basis */ }
align-self
Allows the default alignment to be overridden for individual flex items.
Possible values:
auto
(default)flex-start
flex-end
center
baseline
stretch
Example:
.item { align-self: flex-end; }
Practical Examples
Simple Flexbox Layout
<div class="container"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> </div> <style> .container { display: flex; justify-content: space-between; align-items: center; height: 100vh; } .item { background: #f3f3f3; padding: 20px; border: 1px solid #ccc; } </style>
Responsive Navbar
<nav class="navbar"> <div class="logo">MyLogo</div> <ul class="nav-links"> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> <style> .navbar { display: flex; justify-content: space-between; align-items: center; padding: 10px; background: #333; } .logo { color: #fff; font-size: 24px; } .nav-links { display: flex; list-style: none; } .nav-links li { margin: 0 10px; } .nav-links a { color: #fff; text-decoration: none; } </style>
Learning Through Games
To reinforce the understanding of Flexbox, we played two interactive games:
Flexbox Froggy
A game where you help Froggy and friends by writing CSS code.
Flexbox Defense
A tower defense game that teaches you CSS flexbox.
By understanding and practicing with Flexbox, you will be able to create complex layouts more easily and efficiently. Flexbox provides a more robust and predictable way to handle layout compared to traditional methods like floats and positioning.