how to centre a div horizontally and vertically in css 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: 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:
height:
}
Example 3: how to center horizontally and vertically block div
#html code
<div class="parent">
<h1 class="child">CSS is cool</h1>
</div>
#css code
.parent{
display:grid;
place-items:center;
width:560px;
height: 560px;
}