Question
How can I center a div horizontally and vertically inside a container using Flexbox?
In the example below, I want each number to appear on its own row, with every row centered horizontally inside the container.
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;
}
.row {
width: 100%;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
}
<div class="flex-container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>
What Flexbox properties should be used so the items stack vertically and stay centered horizontally, and how does vertical centering work in this setup?
Short Answer
By the end of this page, you will understand how Flexbox aligns items on the main axis and cross axis, how to stack items vertically with flex-direction: column, and how to center content horizontally and vertically inside a container.
Concept
Flexbox is a CSS layout system designed to align and distribute space between elements inside a container.
The most important idea in Flexbox is that alignment depends on direction:
- The main axis is controlled by
flex-direction - The cross axis is perpendicular to the main axis
For example:
- If
flex-direction: row, items go left to rightjustify-contentaligns items horizontallyalign-itemsaligns items vertically
- If
flex-direction: column, items go top to bottomjustify-contentaligns items verticallyalign-itemsaligns items horizontally
This matters because many centering problems come from using the right property on the wrong axis.
In your case, the goal is:
- place items one below another
- center them horizontally
- optionally center the whole group vertically inside the container
That means the container should usually use:
display: flex;
flex-direction: column;
align-items: center;
: center;
Mental Model
Think of a Flexbox container like a line of people standing in a room.
flex-directiondecides whether they stand in a row or a columnjustify-contentdecides how they are placed along the direction they are facingalign-itemsdecides how they are placed sideways
If the people stand in a column:
justify-content: centermoves the whole group up or down in the roomalign-items: centermoves each person left or right so they are centered
So for stacked numbers, imagine lining them up vertically, then sliding the whole stack into the middle of the room.
Syntax and Examples
The core Flexbox syntax for centering stacked items looks like this:
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
Example: stack items vertically and center them
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
: ;
}
{
: ;
: ;
: ;
: tomato;
: white;
: flex;
: center;
: center;
: ;
: bold;
}
Step by Step Execution
Consider this example:
<div class="container">
<div class="box">1</div>
<div class="box">2</div>
</div>
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 400px;
}
.box {
width: 100px;
height: 100px;
}
What happens step by step
display: flexmakes.containera flex container.flex-direction: columnchanges the main axis from horizontal to vertical.- The two
.boxelements are now placed one below the other.
Real World Use Cases
Flexbox centering is used constantly in real interfaces.
Common examples
- Login screens
- Center a form both horizontally and vertically on the page
- Empty states
- Center a message like “No data available” in a dashboard panel
- Card layouts
- Center text and icons inside fixed-size cards
- Loading screens
- Center a spinner in the viewport
- Navigation and toolbars
- Align buttons and labels evenly inside a header or sidebar
- Modal dialogs
- Place the dialog in the center of the screen
In your case
A vertical list of centered boxes is similar to:
- quiz answer cards
- menu options
- onboarding steps
- dashboard action tiles
Real Codebase Usage
In real projects, developers often use Flexbox centering in a few common patterns.
1. Full-page centering
.page {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
Used for login pages, error pages, and splash screens.
2. Vertical stack with centered children
.list {
display: flex;
flex-direction: column;
align-items: center;
gap: 1rem;
}
Used for button groups, option lists, and wizard steps.
3. Centering inside a component
.card {
display: flex;
align-items: center;
justify-content: center;
}
Used to center text, icons, or badges inside cards and panels.
4. Guarding layout with explicit height
Developers often add one of these when vertical centering is needed:
height: 100vhmin-height: 100vh
Common Mistakes
1. Forgetting flex-direction: column
Broken example:
.container {
display: flex;
align-items: center;
justify-content: center;
}
This centers items, but they stay in a row by default.
Fix:
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
2. Using the wrong alignment property for the axis
Beginners often think:
justify-contentalways means horizontalalign-itemsalways means vertical
That is not true. It depends on flex-direction.
3. Expecting vertical centering without container height
Broken example:
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
Comparisons
| Layout approach | Best for | Strengths | Limitations |
|---|---|---|---|
text-align: center | Centering inline text horizontally | Very simple | Does not vertically center block elements |
margin: 0 auto | Centering a fixed-width block horizontally | Easy for single blocks | No vertical centering |
| Flexbox | Aligning items in one dimension | Great for horizontal or vertical centering | Less ideal for two-dimensional grid layouts |
| CSS Grid | Two-dimensional layouts | Excellent for full-page centering and grid placement | Can be more than needed for simple linear layouts |
Flexbox row vs column
Cheat Sheet
/* Stack items vertically */
.container {
display: flex;
flex-direction: column;
}
/* Center horizontally when using column */
.container {
align-items: center;
}
/* Center vertically when using column */
.container {
justify-content: center;
}
/* Make vertical centering visible */
.container {
min-height: 100vh;
}
Rules to remember
- Default Flexbox direction is
row justify-contentworks on the **main axis`align-itemsworks on the cross axis- Changing
flex-directionchanges what horizontal and vertical mean - Vertical centering needs extra container height
- Use
.rowfor a class selector, notrow
Common pattern
.container {
display: flex;
flex-direction: column;
align-items: center;
: center;
: ;
: ;
}
FAQ
How do I center a div both horizontally and vertically with Flexbox?
Use display: flex, then choose the correct axis:
.container {
display: flex;
justify-content: center;
align-items: center;
}
If you want items stacked vertically, also add flex-direction: column.
Why is justify-content: center not centering horizontally?
Because it centers along the main axis. If flex-direction: column, the main axis is vertical, not horizontal.
Why is vertical centering not working in my flex container?
Usually because the container has no height. Add height or min-height, such as min-height: 100vh.
Should I use div or span for flex items?
Either can work, but div is usually easier for box-like layouts. A span is inline by default, so width and height can be confusing unless you change its display.
How do I put each item on a new row with Flexbox?
Mini Project
Description
Build a simple full-page number panel where several boxes are stacked vertically and centered in the browser window. This project demonstrates how flex-direction, align-items, justify-content, and container height work together in a realistic layout.
Goal
Create a vertically stacked list of boxes that is centered horizontally and vertically using Flexbox.
Requirements
- Create a container that fills the visible page height.
- Stack four boxes vertically inside the container.
- Center the stack horizontally and vertically.
- Center the number inside each box.
- Add spacing between boxes without relying on line-height hacks.
Keep learning
Related questions
Allow Only Numeric Input (0-9) in HTML Input Using jQuery
Learn how to allow only digits 0-9 in an HTML input using jQuery, with examples, validation tips, common mistakes, and best practices.
CSS :not() Selector for Excluding a Class or Attribute
Learn how to use the CSS :not() selector to target elements that do not have a specific class or attribute, with examples and common mistakes.
Can HTML Checkboxes Be Readonly? Understanding readonly vs disabled in HTML Forms
Learn why HTML checkboxes do not support readonly, how disabled differs, and practical ways to prevent changes while still submitting values.