CSS Flexbox Perfect Centering
Learn how to perfectly center elements both horizontally and vertically using CSS Flexbox with this simple and effective technique.
Responsive Design
Yes
Dark Mode Support
No
lines
53
Browser Compatibility
Chrome · Firefox · Safari · Edge
Live Preview
Interact with the component without leaving the page.
CSS Flexbox Perfect Centering
Centering elements in CSS has historically been one of the most challenging tasks for web developers. With CSS Flexbox, this becomes incredibly simple and reliable across all modern browsers.
The Problem
Traditional centering methods often involve:
- Complex positioning calculations
- Browser compatibility issues
- Inflexible solutions that break with dynamic content
The Flexbox Solution
Flexbox provides a clean, modern approach to centering that works with any content size:
<div class="flex-container">
<div class="centered-content">
<h2>Perfectly Centered!</h2>
<p>This content is centered both horizontally and vertically.</p>
<button>Click Me</button>
</div>
</div>.flex-container {
display: flex;
justify-content: center; /* Horizontal centering */
align-items: center; /* Vertical centering */
min-height: 100vh; /* Full viewport height */
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 20px;
box-sizing: border-box;
}
.centered-content {
background: white;
padding: 2rem;
border-radius: 12px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
text-align: center;
max-width: 400px;
width: 100%;
}
.centered-content h2 {
color: #333;
margin-bottom: 1rem;
font-size: 1.5rem;
}
.centered-content p {
color: #666;
margin-bottom: 1.5rem;
line-height: 1.6;
}
.centered-content button {
background: #667eea;
color: white;
border: none;
padding: 12px 24px;
border-radius: 6px;
font-size: 1rem;
cursor: pointer;
transition: background 0.3s ease;
}
.centered-content button:hover {
background: #5a6fd8;
}Key Properties Explained
display: flex
Transforms the container into a flex container, enabling flexbox layout.
justify-content: center
Centers flex items along the main axis (horizontally by default).
align-items: center
Centers flex items along the cross axis (vertically by default).
min-height: 100vh
Ensures the container takes up at least the full viewport height.
Advantages of This Method
- Simple: Just three CSS properties
- Responsive: Works with any content size
- Reliable: Consistent across modern browsers
- Flexible: Easy to modify for different layouts
Browser Support
This technique is supported in all modern browsers:
- Chrome 21+
- Firefox 28+
- Safari 9+
- Edge 12+
Variations
You can easily modify this technique for different centering needs:
/* Center only horizontally */
.horizontal-center {
display: flex;
justify-content: center;
}
/* Center only vertically */
.vertical-center {
display: flex;
align-items: center;
min-height: 100vh;
}
/* Center with space around */
.spaced-center {
display: flex;
justify-content: space-around;
align-items: center;
}Conclusion
CSS Flexbox makes centering elements straightforward and reliable. This technique should be your go-to method for centering content in modern web development.
HTML
7
lines
CSS
46
lines
<div class="flex-container">
<div class="centered-content">
<h2>Perfectly Centered!</h2>
<p>This content is centered both horizontally and vertically.</p>
<button>Click Me</button>
</div>
</div>
Browser Compatibility
Chrome
≥ 21
Firefox
≥ 28
Safari
≥ 9
Edge
≥ 12