1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Center table within a table

Discussion in 'CSS' started by anjanesh, Mar 20, 2005.

  1. #1
    Hi
    Moving to CSS as XHTML 1.1 has dropped many of the tag attributes.
    I need a table within a table centered. Before I did align="center".
    text-align:center is working only for text and not for subtables.
    
    <table>
      <tr>
      <td style="text-align:center">
         Test1
         <table>
         <tr><td>Test2</td></tr>
         </table>
      </td>
      </tr>
    </table>
    
    Code (markup):
    Test1 is centered but the Test2 is not. Infact the whole inner-table is aligned left instead of center.
    Any idea how to correct this ?
    Thanks
     
    anjanesh, Mar 20, 2005 IP
  2. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #2
    good question. I played with it a little bit and couldn't get it to work myself.

    Frequently, I've found that finding a good CSS solution isn't worth the time and effort it takes to find it.

    The best way to do things is with a combination of CSS and table's IMO. Tables still have inherent properties that are much more convenient than CSS for now. I think this might be one of those cases. Although it seems that it would be a common enough question that someone else here might answer you and it'll be a lot easier than I thought :D
     
    nevetS, Mar 20, 2005 IP
  3. anjanesh

    anjanesh Well-Known Member

    Messages:
    343
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    165
    #3
    Found this somewhere else. This seems to work. Also for having table in body centered (not the text within the body)
    
    <table>
      <tr>
      <td style="text-align:center">
         Test1
         <table [COLOR=DarkGreen]style="margin-left:auto;margin-right:auto"[/COLOR]>
         <tr><td>Test2</td></tr>
         </table>
      </td>
      </tr>
    </table>
    
    Code (markup):
     
    anjanesh, Mar 20, 2005 IP
  4. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #4
    text-align does not apply to block-level elements; this is why your initial HTML didn't work. This code above will work because you set left and right margins to auto - this is the proper way to align block-level elements. One thing to keep in mind that this solution only works in IE if the document has proper DOCTYPE (see 7061 for more info).

    J.D.
     
    J.D., Mar 20, 2005 IP
  5. anjanesh

    anjanesh Well-Known Member

    Messages:
    343
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    165
    #5
    I viewing this in Mozilla FireFox 1.0.1 - all my development is done in FF. I later check to see what difference is there in IE and make some changes using Javascript.
     
    anjanesh, Mar 20, 2005 IP
  6. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It will work fine in IE as long as you have DOCTYPE defined.

    BTW, JS is disabled more often than you'd think. It's always a good idea to make your website usable in this case.

    J.D.
     
    J.D., Mar 20, 2005 IP
  7. rickbkis

    rickbkis Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Great tip (#4). I think you've saved my sanity - for the moment, at least.

    rickb
     
    rickbkis, Mar 22, 2005 IP