Maybe a more modern answer?
- Create div
- Place the text inside the div
Then use the flexbox div style as follows:
display: flex; flex-direction: column; height: 100%; width: 100%; justify-content: center; align-items: center;
ALTERNATIVE for more dynamic positioning
set the background image div to
positiion: relative;
And your text block div to include
position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
- This allows you to move text, for example, 20% from the top instead of 50%.
- translate (-50%, -50%) changes the alignment of the text element div to the center, which will be noticeable if you have longer text.
source share