Horizontal and Vertical Center Alignment with Flexbox in CSS3

CSSWeb DevelopmentFront End Technology

For horizontal and vertical center alignment with Flexbox, use the align-items property in CSS3. Set the align-items property to center.

Following is the code for horizontal and vertical center alignment with flexbox −


 Live Demo

<!DOCTYPE html>
body {
   font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
.h-container {
   display: flex;
   width: 300px;
   height: 300px;
   background-color: lightblue;
   justify-content: center;
   align-items: center;
   border: 2px solid black;
.h-1 {
   font-size: 30px;
   width: 150px;
   height: 150px;
   background-color: red;
   color: white;
   border: 2px solid rgb(58, 2, 83);
<h2>Horizontal and vertical center alignment using flexbox</h2>
<div class="h-container">
<div class="h-1">Centered Div</div>


The above code will produce the following output −

Published on 12-May-2020 09:40:00