tags:

views:

52

answers:

2

In the following test case, the alt text is horizontally centered, but it's stuck to the top of the box. How do I get it in the middle?

<html>
  <body>
    <div style="height: 300px; width: 300px; border: solid black 1px;">
      <img src="asdfasdf" alt="foo" style="display: block; text-align: center; vertical-align: middle"/>
    </div>
  </body>
</html>
A: 

Try with display:table-cell instead of block.

gregseth
Nope. That actually breaks the horizontal align, too.
mike
+2  A: 

One advantage of tables is they provide cross-browser vertical centering. DIVs don't. This is once case where I'd bite the bullet and add those ugly TRs and TDs.

<table cellspacing="0" style="width:300px;height:300px;border:1px solid black;">
   <tr>
     <td style="vertical-align:middle;text-align:center;">
       <img src="asdfasdf" alt="foo" />
     </td>
   </tr>
</table>

It's called a single-celled centering table and ugly as it is, it works.

darkporter