Below are the methods which have always worked for me
Set the display of the parent div to display: flex;
and the you can align the child elements inside the div using the justify-content: center;
(to align the items on main axis) and align-items: center;
(to align the items on cross axis).
If you have more than one child element and want to control the way they are arranged (column/rows), then you can also add flex-direction
property.
Working example:
.parent {_x000D_
align-items: center;_x000D_
border: 1px solid black;_x000D_
display: flex;_x000D_
justify-content: center;_x000D_
height: 250px;_x000D_
width: 250px;_x000D_
}_x000D_
_x000D_
.child {_x000D_
border: 1px solid black;_x000D_
height: 50px;_x000D_
width: 50px;_x000D_
}
_x000D_
<div class="parent">_x000D_
<div class="child"></div>_x000D_
</div>
_x000D_
2. (older method) Using position, margin properties and fixed size
Working example:
.parent {_x000D_
border: 1px solid black;_x000D_
height: 250px;_x000D_
position: relative;_x000D_
width: 250px;_x000D_
}_x000D_
_x000D_
.child {_x000D_
border: 1px solid black;_x000D_
margin: auto;_x000D_
top: 0;_x000D_
bottom: 0;_x000D_
left: 0;_x000D_
right: 0;_x000D_
height: 50px;_x000D_
position: absolute;_x000D_
width: 50px;_x000D_
}
_x000D_
<div class="parent">_x000D_
<div class="child"></div>_x000D_
</div>
_x000D_