I'm attempting to center the text on a webpage right in the middle using display: flex while also including a header with a logo inside it. I've successfully achieved this using the code below:
Method 1
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
The result can be seen here: https://codepen.io/pen/QWKrrNq
However, when trying to accomplish this with display: flex using the following code:
Method 2
height: 100%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
The outcome is visible here: https://codepen.io/pen/wvzjjMJ
With method 1, the text is perfectly centered, but with method 2, it's not.
The reason for this discrepancy lies in the height taken up by the header, affecting the space available for centering the text div. I wonder if achieving what was done in method 1 is possible through display flex?
I have experimented with changing the height of the text container div from 100% to 80%, 70%, etc., which initially works, but as the viewport width changes, the text becomes off-centered unlike in method 1. The only solution seems to be using media queries for different heights percentages.
Is there an easier way to accomplish this using display: flex?
Thank you.