Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

text-align center is not working inside inline-block html table

I am making an HTML Email and have decided to use tables for each of the nav elements. I need the tables to be displayed inline. I used display:inline-block; on the table creating the desired result, However the TEXT inside the table will not align. I have placed the text-align: center; in the table, td, and a tag but it has not worked.

I know the method works with divs but I can't find a solution for tables.

HTML

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
    <td align="right" valign="top">

        <table class="navInline" width="130" height="35" border="0" cellspacing="0" cellpadding="0" bgcolor="#999966" style="text-align:center;">
            <tr>
                <td align="center"> <a href="#" style="text-decoration: none; color: #005E82;">
     option 1
     </a>

                </td>
            </tr>
        </table>

        <table class="navInline" width="130" height="35" border="0" cellspacing="0" cellpadding="0" bgcolor="#999966">
            <tr>
                <td align="center" style="text-align:center;"> <a href="#" style="text-decoration: none; color: #005E82;">
     option 2
                    </a>

                </td>
            </tr>
        </table>

        <table class="navInline" width="130" height="35" border="0" cellspacing="0" cellpadding="0" bgcolor="#999966">
            <tr>
                <td align="center"> <a href="#" style="text-decoration: none; color: #005E82; text-align:center;">
     option 3
     </a>

                </td>
            </tr>
        </table>
    </td>
</tr>

CSS

.navInline {
display:inline-block;
vertical-align:top;
text-align: center !important; }

here is the not working code

http://jsfiddle.net/nathanaric/pf35h/9/

like image 520
nathanaric Avatar asked Apr 09 '14 17:04

nathanaric


People also ask

How do I center text in an inline block?

Inline Elements To center an inline element like a link or a span or an img, all you need is text-align: center . For multiple inline elements, the process is similar. It's possible by using text-align: center .

Why is text not aligning Center in HTML?

This is because text-align:center only affects inline elements, and <aside> is not an inline element by default. It is a block-level element. To align it, we will have to use something other than text-align , or turn it into an inline element.

Why text-Align Center doesn't work?

Short answer: your text isn't centered because the elements are floated, and floated elements "shrink" to the content, even if it's a block level element.


1 Answers

You need to set a width attribute on your table cells.

Example:

<table class="navInline" width="130" height="35" border="0" cellspacing="0" cellpadding="0" bgcolor="#999966" style="text-align:center;">
  <tr>
    <td align="center" width="130"><a href="#" style="text-decoration: none; color: #005E82;">option 1</a></td>
  </tr>
like image 90
John Avatar answered Nov 15 '22 07:11

John