21

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,427
  • 2
  • 25
  • 40
EricC
  • 5,460
  • 10
  • 48
  • 70
  • 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

41

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,427
  • 2
  • 25
  • 40
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;
  height:100%;
}

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

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

</div>
rgmt
  • 14,120
  • 12
  • 47
  • 66
Santosh Khalse
  • 10,808
  • 3
  • 34
  • 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.

rgmt
  • 14,120
  • 12
  • 47
  • 66
-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>
rgmt
  • 14,120
  • 12
  • 47
  • 66
Rohit Yadav
  • 47
  • 1
  • 6