Flexbox Centering Practice
What you'll build
An HTML page with 3 boxes demonstrating horizontal centering, vertical centering, and full centering (both axes) using flexbox.
Goals
- Use justify-content to center on the main axis
- Use align-items to center on the cross axis
- Combine both for full centering
- Understand how flex-direction affects which axis is which
Prerequisites
- HTML structure
- Basic CSS including display: flex
Steps
- 1
Set up the HTML
Create centering.html with 3 demo containers, each with a child box inside.
Code<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Flexbox Centering</title> <link rel="stylesheet" href="centering.css"> </head> <body> <div class="demo horizontal"> <div class="box">H-Center</div> </div> <div class="demo vertical"> <div class="box">V-Center</div> </div> <div class="demo both"> <div class="box">Centered</div> </div> </body> </html>Expected result
Three containers each with a box inside, no styling yet.
- 2
Style the demo containers and boxes
Create centering.css. Give each demo a fixed height and background, and the box a visible style.
Code.demo { display: flex; height: 150px; margin-bottom: 1rem; background: #f1f5f9; border: 2px dashed #94a3b8; } .box { background: #2563eb; color: #fff; padding: 1rem 2rem; border-radius: 0.5rem; }Expected result
Three gray containers with blue boxes at the left edge.
- 3
Add horizontal centering
For the .horizontal demo, use justify-content: center to center the box on the main axis (horizontal in a row).
Code.horizontal { justify-content: center; }Expected result
The first box is horizontally centered in its container.
- 4
Add vertical and full centering
For .vertical use align-items: center. For .both use both justify-content and align-items center.
Code.vertical { align-items: center; } .both { justify-content: center; align-items: center; }Expected result
Second box is vertically centered, third box is centered on both axes.
Hint
With flex-direction: row (default), justify-content controls horizontal and align-items controls vertical.
- 5
Add responsive styles
Use media queries to adjust the layout for mobile: switch multi-column layouts to single column on small screens, reduce font sizes, and adjust spacing. Mobile-first means small-screen experience matters equally.
Code@media (max-width: 640px) { .container { flex-direction: column; padding: 1rem; } h1 { font-size: 1.5rem; } .card { width: 100%; margin-bottom: 1rem; } }Expected result
Below 640px viewport, the layout becomes single-column with smaller font and spacing; content does not overflow.
Try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flexbox Centering</title>
<link rel="stylesheet" href="centering.css">
</head>
<body>
<!-- TODO: 3 demo containers -->
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flexbox Centering</title>
<link rel="stylesheet" href="centering.css">
</head>
<body>
<div class="demo horizontal"><div class="box">H-Center</div></div>
<div class="demo vertical"><div class="box">V-Center</div></div>
<div class="demo both"><div class="box">Centered</div></div>
</body>
</html>
/* centering.css */
.demo { display: flex; height: 150px; margin-bottom: 1rem; background: #f1f5f9; border: 2px dashed #94a3b8; }
.box { background: #2563eb; color: #fff; padding: 1rem 2rem; border-radius: 0.5rem; }
.horizontal { justify-content: center; }
.vertical { align-items: center; }
.both { justify-content: center; align-items: center; }What you learned
You mastered flexbox centering — the most common CSS layout task. justify-content centers on the main axis, align-items on the cross axis, and combining both centers perfectly in both directions.
Related Resources
Related Lessons
- CSS Flexbox
Flexbox alignment properties.
Related Practice
- Complete the Flex Centering
Practice flex centering.
- Predict Flex Justify-Content
Predict flex layout results.
Related Cheatsheets
- CSS Cheatsheet
Flexbox reference.
Related Glossary
- CSS
Cascading Style Sheets.
Related Glossary
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
- CSS
Cascading Style Sheets.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.