If you know the height of the div you want to center, you can position it absolutely within its parent and then set the top
value to 50%
. That will put the top of the child div 50% of the way down its parent, i.e. too low. Pull it back up by setting its margin-top
to half its height. So now you have the vertical midpoint of the child div sitting at the vertical midpoint of the parent - vertically centered!
Example:
.black {_x000D_
position:absolute;_x000D_
top:0;_x000D_
bottom:0;_x000D_
left:0;_x000D_
right:0;_x000D_
background:rgba(0,0,0,.5);_x000D_
}_x000D_
.message {_x000D_
background:yellow;_x000D_
width:200px;_x000D_
margin:auto auto;_x000D_
padding:10px;_x000D_
position: absolute;_x000D_
top: 50%;_x000D_
margin-top: -25px;_x000D_
height: 50px;_x000D_
}
_x000D_
<div class="black">_x000D_
<div class="message">_x000D_
This is a popup message._x000D_
</div>_x000D_
</div>
_x000D_