You can do this by setting the parent's position to relative. Then set the child's top, right, bottom, and left properties to 0, and setting the margin to auto.
.parent {
width: 300px;
height: 300px;
border: 1px solid #ddd;
position: relative;
}
.child {
width: 100px;
height: 100px;
background: #ddd;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
<div class="parent">
<div class="child"></div>
</div>
Edit
As mentioned, using top, right, bottom, and left properties set to 0 with auto margins only works if the child element has a set width and height. Otherwise, the child will simply fill the entire width and height of the parent. If you want to center a child without specifically setting it's width and height, you can do it using the CSS translate() function.
First you can center the top left corner of the child by setting the child's top and left to 50% of it's parent's width and height. Then shift it over and up by 50% of it's own width and height.
.parent {
width: 400px;
height: 300px;
border: 1px solid #ddd;
position: relative;
}
.child {
padding: 20px;
background: #ddd;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
margin: auto;
}
<div class="parent">
<div class="child">
<p>Lorem ipsum dolor.</p>
</div>
</div>