22

The following is not vertically aligning the div with text in browser tab (it is horizontally aligning correctly):

<div style="height: 100%; display: flex; align-items: center; justify-content: center;">

  <div>
    Some vertical and horizontal aligned text
  </div>

</div>

What is wrong?

Syam Pillai
  • 4,967
  • 2
  • 26
  • 42
EricC
  • 5,720
  • 13
  • 52
  • 71
  • 1
    Set a height on the parent. In your case both `body` and `html`. When you use 100% -- think 100% of what? – AA2992 Sep 26 '16 at 08:05

4 Answers4

42

The problem is with the height you given to the parent container. The height :100% does not take whole height. change that to 100vh or like that

Here is the updated Demo

.container{
  height: 100vh; 
  display: flex; 
  align-items: center; 
  justify-content: center;
}
<div class="container">
  <div>
    Some vertical and horizontal aligned text
  </div>
</div>
Syam Pillai
  • 4,967
  • 2
  • 26
  • 42
3

Please try bellow following code

body, html{
  height:100%;
  width:100%;
  padding:0px;
  margin:0px;
}

.demo-wp{
  height: 100%; 
  display: flex; 
  align-items: center; 
  justify-content: center;
  background:green;
}

.inner-div{
  background:gold;
  padding:20px;
}
<div class="demo-wp">

  <div class="inner-div">
    Some vertical and horizontal aligned text
  </div>

</div>
Sébastien Gicquel
  • 4,227
  • 7
  • 54
  • 84
Santosh Khalse
  • 12,002
  • 3
  • 36
  • 36
0

Try this:

<div style="height: 100vh; display: flex; align-items: center; justify-content: center;">
  <div>
    Some vertical and horizontal aligned text
  </div>
</div>

It's because your parent divdoesn't take the full height.

rocambille
  • 15,398
  • 12
  • 50
  • 68
-2

try this

<div style="height: 100%; display: -webkit-flex; display: flex; align-items: center; justify-content: center; background-color: lightgrey;">

  <div>
    Some vertical and horizontal aligned text
  </div>

</div>
rocambille
  • 15,398
  • 12
  • 50
  • 68
Rohit Yadav
  • 47
  • 1
  • 6