Css Center Vertically And Horizontally

center div horizontally and vertically
.parent {
position: relative;
}
.child {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
Source:codepen.io
css center vertically and horizontally
body {
display: flex;
min-height: 100vh;
flex-direction: column;
justify-content: center;
align-items: center;
}
/* to make "align-items: center" work: use "min-height: 100vh;"
100vh = 100% Viewport Height */
css center horizontally and vertically
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
how to center a div vertically and horizontally
.container{
margin: 0;
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
width: /* Define the width here; */
height: /* Define the height here; */
}

/*You have to define the width and height! */
center align div vertically and horizontally css
.parent{
display: flex;
justify-content: center;
align-items: center;
}
/* Child elements will be perfectly centered automatically */
vertical and horizontal align center div
.center {
height: 200px;
position: relative;
border: 3px solid green;
}

.center p {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

 

Leave a Comment