Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Position text in the middle of the page

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.

like image 263
snarkyazoid Avatar asked Oct 30 '11 22:10

snarkyazoid


People also ask

How do I vertically align text in the middle of a div?

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 .


2 Answers

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/

like image 77
Ry- Avatar answered Sep 25 '22 07:09

Ry-


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

like image 24
Claytronicon Avatar answered Sep 23 '22 07:09

Claytronicon