Flexbox Basics - HTML Tutorial
Flexbox (short for Flexible Box Layout) is a CSS layout system for arranging items in a row or a column. It makes jobs that used to be awkward, like putting a menu in a line, spacing items evenly or centering something vertically, quick and reliable.
Turning Flexbox On
Flexbox works on a container and its direct children. Set display: flex on the container, and its children (called flex items) line up in a row:
<style>
.row {
display: flex;
}
.row div {
background-color: #fff3b0;
border: 1px solid #c9a800;
padding: 1rem;
}
</style>
<div class="row">
<div>Coffee</div>
<div>Tea</div>
<div>Juice</div>
</div>
Without display: flex, the three boxes would stack on top of each other. With it, they sit side by side, each one only as wide as its content.
Adding Space with gap
The gap property puts space between flex items, without adding any space around the outside edges:
.row {
display: flex;
gap: 1rem;
}
The Two Axes
Flexbox arranges items along two directions, called axes:
- The main axis is the direction items flow in. For a row, it runs left to right.
- The cross axis runs at right angles to it. For a row, it runs top to bottom.
The flex-direction property sets the main axis. The default is row. Use column to stack items vertically instead, which is useful when you want the alignment features of Flexbox in a vertical layout.
Aligning Along the Main Axis
justify-content controls how items are spread out along the main axis:
flex-start(the default): items gather at the start.flex-end: items gather at the end.center: items gather in the middle.space-between: the first item is at the start, the last is at the end and the rest share the space between them.space-aroundandspace-evenly: space is shared around all the items.
Aligning Along the Cross Axis
align-items controls how items line up across the other direction:
stretch(the default): items stretch to fill the container's height.flex-start,centerandflex-end: items line up at the top, middle or bottom.
Example: A Navigation Bar
Flexbox is perfect for the kind of header you built in Semantic Layout: the logo on the left and the menu on the right.
<style>
.site-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem;
background-color: #8b4513;
color: white;
}
.site-header ul {
display: flex;
gap: 1.5rem;
list-style: none;
margin: 0;
padding: 0;
}
.site-header a {
color: white;
}
</style>
<header class="site-header">
<p class="logo">Corner Cafe</p>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="menu.html">Menu</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
</header>
There are two flex containers here:
- The
headerusesspace-betweento push the logo and thenavto opposite ends, andalign-items: centerto line them up vertically. - The
ulputs the links in a row with a gap between them.list-style: noneremoves the bullets, and the margin and padding are reset to remove the list's default indentation.
Example: Centering Anything
Centering something both horizontally and vertically takes just three lines:
.hero {
display: flex;
justify-content: center; /* horizontally */
align-items: center; /* vertically */
min-height: 20rem;
}
Whatever you put inside .hero sits right in the middle.
Growing and Wrapping
By default, flex items are only as wide as their content, and they all squeeze onto one line. Two properties change that:
flex: 1on the items lets them grow to share the available space equally.flex-wrap: wrapon the container lets items move onto a new line when there isn't enough room.
<style>
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 15rem;
padding: 1rem;
border: 1px solid #ddd;
border-radius: 8px;
}
</style>
<div class="cards">
<article class="card"><h3>Breakfast</h3><p>Served until 11am.</p></article>
<article class="card"><h3>Lunch</h3><p>Served 11am to 3pm.</p></article>
<article class="card"><h3>Cakes</h3><p>Baked fresh every morning.</p></article>
</div>
flex: 1 1 15rem is shorthand for three settings: each card can grow (1), can shrink (1) and starts at a width of 15rem. On a wide screen the cards sit side by side. On a narrow screen, they wrap onto new lines. Resize your browser window to watch it happen.
Tip
In Chrome, Edge and Firefox Developer Tools, flex containers have a small "flex" badge in the Elements panel. Click it to show an overlay of the layout on the page.
Flexbox is ideal for arranging items in one direction. When you need rows and columns at the same time, CSS Grid is often a better fit. That's the next section.