Flexbox really changed the game with the alignment of elements in a fluid manner. Define your container element as display: flex
, and then align your inner child elements that you would use justify-content: center; align-items: center;
justify-content: center; align-items: center;
.container { position: absolute; top: 0; right: 0; bottom: 0; left: 0; display: flex; justify-content: center; align-items: center; } .parent { height: 500px; width: 500px; position: relative; } <div class="parent"> <div class="container"> <p>Hello</p> <p>World</p> </div> </div>
You will notice that "Hello" and "World" will be centered vertically and horizontally inside the .container
element.
source share