views:

45

answers:

2

I have code:

<tr>
    <td align="center" style="height: 50px; text-align: center; vertical-align: middle"   >
           <div class="hoverClass ui-widget-content ui-corner-all" style="text-align: center; vertical-align: middle;width:100%;height:100%">
                 <label style="text-align: center; vertical-align: middle" >Streetlight Repair</label>
           </div>
     </td>
</tr>

I want it to be a cell with a div containing a label. I want the div to take up the whole cell. and the label to be centered within the div. However the label sits at the top left of the div.

Any ideas?

+2  A: 

If the height is definitely 50px, set the line-height CSS of the label to 50px;

To get the label in the middle horizontally, set margin-left:auto; and margin-right:auto;

Oliver
So this is a whole table of these. So some of the labels wrap and take up two lines.
kralco626
And I tried the margin thing and it didn't work.
kralco626
The auto horizontal margin will work only if you define an explicit width I'm pretty sure.
alex
i could define an explication width for the label. It is in a cell with a defined width. I'll try that. But i still have an issue with the vertical alignment.
kralco626
+1  A: 

If you have more than one line, you can not use the line-height property to hack middle alignment.

Easiest way is to play with top and bottom padding.

I blogged about this.

alex
ugh. So basically if I wanted to make the content decided at runtime vis JavaScript i'm screwed... Unless I made JavaScript decide if the text would be wrapped and how many lines it would need and then set the padding accordingly...?
kralco626
@kralco626 Yes, or try playing with `padding`. You can also use the middle align hack, but it is ugly with IE (wrap with 2 divs). Refer to article linked in blog :)
alex