0

This might sound odd, but doing this:

<table id="MyTableID">
     <tr>
         <td align="center">
                <div id="1">
                   Content
               </div>
        </td>
     </tr>
 </table>

Aligns "Content" to the center, but:

<table id="MyTableID">
     <tr>
         <td style="text-align: center;">
                <div id="1">
                   Content
               </div>
        </td>
     </tr>
 </table>

Aligns it to the left.

I've read many posts about using margin-left: auto and margin-right: auto, and also extracted the style to a css file. Nothing of this seems to work for me.

Does anyone know why? what am I doing wrong? I'm using Firefox 19.

7
  • 1
    Both center for me: jsfiddle.net/j08691/ttfSk. Note that align="center" is deprecated. Commented Apr 3, 2013 at 21:14
  • Same for me. Which browser do you use? Commented Apr 3, 2013 at 21:15
  • Are you trying to center the content in the td or center the td itself ? Commented Apr 3, 2013 at 21:16
  • Never start ID with a number, try using some string. Commented Apr 3, 2013 at 21:17
  • Aaah, sorry. Works for me in ff19. Commented Apr 3, 2013 at 21:17

1 Answer 1

2

I think that the misunderstanding here is that in using traditional html alignment you declare the style of alignment in the parent element.

<td align="center">
    <!-- Centered stuff in here -->
</td>

You shouldn't use text-align for aligning block-level elements. Also, when using block elements you typically want to apply that style to that specific node and then align using margins.

<td>
    <div style="width:100px;margin:0 auto"></div>
</td>

That being said throwing block elements in tables is never really a good idea. Tables are typically reserved for HTML emails and tabular data. Most modern web products are structured with divs/spans using CSS.

Sign up to request clarification or add additional context in comments.

2 Comments

This worked! It is needed to use the the <div> inside the <td>, or change the div css. Without the <div> it doesn't work. Thanks!
@ChristianVielma my pleasure, please mark as answered so this question doesn't appear as unresolved. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.