<div style="height:100px;width:100px;border:1px solid black"></div>
How can I position this div in the center?
<div style="height:100px;width:100px;border:1px solid black"></div>
How can I position this div in the center?
.parent {
position: relative;
}
.child {
width: 300px;
height: 100px;
padding: 20px;
position: absolute;
top: 50%;
left: 50%;
margin: -70px 0 0 -170px;
}
More you find here.
On the parent of that div:
div.parent {
display: flex;
justify-content: center;
align-items: center;
}
You can absolutely position the element with top: 50%; left: 50%;
then use translate()
to move it back/up 50% of it's own width to put it in the absolute center.
div {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%,-50%);
}
<div style="height:100px;width:100px;border:1px solid black"></div>