tags:

views:

119

answers:

3

Dear experts, I was trying to align an paragraph to the middle of an division element via CSS and I somehow can't 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 doesn't work in IE nor firefox,

A: 

http://www.w3.org/TR/CSS21/visudet.html#propdef-vertical-align
"Applies to: inline-level and 'table-cell' elements"

http://phrogz.net/CSS/vertical-align/

reisio
but <p> is not an inline element though
Dennis D
Exactly, it's block, so the *vertical-align* property does not apply to it.
reisio
A: 

It's impossible to vertically align anything without it being a table, or without using Javascript.

DFischer
This is one of those classic areas where teaching someone that is used to using tables for layout to start using CSS hits a giant and horrible train wreck. CSS is powerful, but it's warts are glaring and it's idiosyncrasies are legion.
Serapth
+1 Serapth - I hate this. I **HATE** it!
Andrew Heath
This is incorrect.
reisio
A: 

Here's a nice hackish example, i'd put the IE css in conditionals instead of hiding it with hacks, other that that nice.

  <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>
rebus