center div horizontally and vertically code example
Example 1: center div horizontally and vertically
.parent {
position: relative;
}
.child {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
Example 2: css center horizontally and vertically
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Example 3: css vertical center
/*Remove comment to become a better programmer. */
/* No Flexbox */
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
/* With Flexbox */
.parent {
display: flex;
flex-direction: column;
justify-content: center;
}
Example 4: 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! */
Example 5: css align center vertical and horizontal
.parent-class {
display: flex;
align-items: center;
justify-content: space-around;
}
Example 6: 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 */