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 −
Example
<!DOCTYPE html>
<html>
<head>
<style>
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);
}
</style>
</head>
<body>
<h2>Horizontal and vertical center alignment using flexbox</h2>
<div class="h-container">
<div class="h-1">Centered Div</div>
</div>
</body>
</html>Output
The above code will produce the following output −
