I would like to position a <h1>
in the middle of any user's page. I have been searching the internet and they all position it in the incorrect place. Thank you!
UPDATE: I mean vertical and horizontal! In the exact middle!
ALSO: There is nothing else on the page.
Answer: Use the CSS line-height property Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px .
Try this CSS:
h1 { left: 0; line-height: 200px; margin-top: -100px; position: absolute; text-align: center; top: 50%; width: 100%; }
jsFiddle: http://jsfiddle.net/wprw3/
Here's a method using display:flex
:
.container { height: 100%; width: 100%; display: flex; position: fixed; align-items: center; justify-content: center; }
<div class="container"> <div>centered text!</div> </div>
View on Codepen
Check Browser Compatability
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With