Vertical alignment of an element in CSS

Dear experts, I tried to align the paragraph to the middle of the separation element via CSS, and somehow I can not get it to work.

 <style type="text/css">
    .wrap{
background:red;
height: 5em;  
     }
     p{
     background:blue; 
        height: 2em; 
vertical-align:middle; 

      }
  </style>


<div class="wrap">
    <p>
    ALIGN TEXT
    </p>
</div>

It does not work in IE or firefox,

+3
source share
4 answers

Here is a good hacker example , I would put IE css in conditional expressions instead of hiding it with hacks, and the other is good.

  <div style="display: table; height: 400px; #position: relative; overflow: hidden;">
    <div style=" #position: absolute; #top: 50%;display: table-cell; vertical-align: middle;">
      <div class="greenBorder" style=" #position: relative; #top: -50%">
        any text<br>
        any height<br>
        any content, for example generated from DB<br>
        everything is vertically centered
      </div>
    </div>
  </div>
+1
source

vertical-align:middle, display:inline-block 100% :

http://jsfiddle.net/HVtuD/

<div id='container'>
  <div id='text'>vertical-align + inline-block<br>trick<br>in action</div>
</div>

#container{
  height: 300px;border: 1px solid black;text-align:center;
}
#text,#container:before{
  vertical-align: middle;
  display: inline-block;
}
  #container:before{
  content: "";
  height: 100%;
  width: 0%;
}
0

It is not possible to vertically align anything without a table or without using Javascript.

-2
source

Source: https://habr.com/ru/post/1744575/


All Articles