Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to centralized my text horizontally and vertically. For this purpose I have made the following css class.

CSS
.box
{
    height:30px;
    width:80px;
    background-color:yellow;
    display:block;
    float:left;
    text-align:center;
    vertical-align:middle;
    border:1px solid #000;
}


I have used this class in a span tag
This is a text

using this class my text is centered align horizontally but not vertically. What is the problem?
Posted
Updated 18-May-11 21:04pm
v3
Comments
saxenaabhi6 19-May-11 3:03am    
vertical align dosent worked for me as well some time...
do it in div tag or use margin from top or bottom or use padding

vertical-align:text-bottom;
Try setting vertical-align to middle.
 
Share this answer
 
Comments
rashidfarooq 19-May-11 8:12am    
sorry. I typed wrong by mistake. Now, I have update my question. Please answer keeping in view the updated question.
Sergey Alexandrovich Kryukov 19-May-11 13:29pm    
Abhinav, I think that's not it. It does not apply to span.
Please see my answer.
--SA
Vertical align is a property of the block elements only, mostly used in tables, to arrange text in the table cells in a proper position if the cell is higher (due to data in the same row and difference column(s)) than that is required by the text in a first cell.

[EDIT: answering the follow-up question about table cell]

Test the HTML below:

HTML
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <style type="text/css"><!--
         td, table { border: thin solid; }
         td { padding-left: 1em; padding-right: 2em; padding-top: 1em; padding-bottom: 1em;}
         td.top { vertical-align:top; }
         td.middle { vertical-align:middle; }
         td.bottom { vertical-align:bottom; }
      --></style>
   </head>
<body>

<table><tr>
   <td class="top">top</td>
   <td class="middle">middle</td>
   <td class="bottom">bottom</td>
   <td>1<br/>2<br/>3<br/>4<br/>5<br/>6</td>
</tr></table>

</body>
</html>


Probably you don't see the difference on you example, because the vertical size of all cells in a row is the same. You need to have one cell with higher content to see the difference in vertical align style.

I tested it on Seamonkey (Gecko layout engine, same as the one used by Firefox) and IE8; it should work with any non-nonsense browser.

—SA
 
Share this answer
 
v5
Comments
rashidfarooq 21-May-11 1:26am    
I have applied vertical-align:middle property to table cell, but it does not work there also.
Sergey Alexandrovich Kryukov 22-May-11 1:36am    
It should work -- test my sample I added after [EDIT].
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900