
Brandon T. answered 05/30/19
Web Developer with 2 years of experience
It may not be quite as difficult as you think if you use flexbox.
Flexbox is a built-in CSS tool that allows you to more easily format layouts, grids and alignment.
To use it you would specify that the parent (or container) div is a flexbox. Then you would use some of the goodies inside flexbox to center the content vertically and horizontally. See Markup & CSS below:
<div class="flexDiv">
<p> Center Me </p>
</div>
<style>
.flexDiv {
width: 100%;
height: auto;
display: flex;
justify-content: center;
align-items: center;
}
</style>
And that should do it. If you copy this code above you should see the paragraph tag centered in the middle of the div on both axes. If you don't, you may need to give the div an explicit pixel amount for height & width.
I could tell you which one is which (horizontal vs. vertical) but I recommend you play around with it yourself.
IMPORTANT NOTE: Using display:flex; can break some of the previous styling on a block element. It's typically not a problem because all of that styling can be re-done easily in the new flex div.