CSS
Perfect Centering with Flexbox
Achieve perfect vertical and horizontal centering for any element within its container using a simple Flexbox pattern, ideal for modals, cards, or hero sections.
.container {
display: flex;
justify-content: center; /* Horizontally center */
align-items: center; /* Vertically center */
min-height: 200px; /* Example height for container */
border: 1px dashed #ccc;
}
.centered-item {
padding: 20px;
background-color: #f0f0f0;
border: 1px solid #ddd;
}
How it works: This snippet demonstrates how to perfectly center an item both horizontally and vertically within its parent container using Flexbox. By setting `display: flex` on the container, `justify-content: center` centers items along the main axis (horizontally by default), and `align-items: center` centers them along the cross axis (vertically by default).