You can use CSS Flexbox or Grid to center a <div> both horizontally and vertically. This is a short Flexbox example:.
.center-div {
display: flex;
justify-content: center; /* Horizontally center */
align-items: center; /* Vertically center */
height: 100vh; /* Ensures it covers the entire viewport height */
}
All you have to do is give your <div> the center-div class. The flex container created by this code aligns its child both horizontally and vertically. Once the height is adjusted as necessary, your <div> will be centered. By utilizing place-items: center and display: grid, you may use CSS Grid to get a similar effect.
Comments
Post a Comment