2012-12-28 16 views
13

Mam bardzo prosty problem: muszę wyśrodkować tabelę wewnątrz elementu TD. Gdybym użyciu HTML 4 zrobiłbym to tak:Wyśrodkuj tabelę wewnątrz TD

​<table style="border:solid;width: 100%"> 
    <tr> 
     <td align="center"> 
      <table style="border:solid; width:50%"> 
       <tr> 
        <td >I must be in the center</td>      
       </tr> 
      </table> 
     </td>      
    </tr> 
</table>​ 

Ale staram się nie używać nieaktualnych atrybutów i zrobić to tak, CSS. Ja już próbowałem:

<td style="text-align:center"> 

I tak:

<td style="margin: 0 auto"> 

i stoły trzyma w lewej strony komórki. Jakieś sugestie?

+1

Układ oparty na tabeli, nooooooooo! –

+0

Wiem, ale nie mam wyboru :( –

+0

Jest praktycznie zawsze wybór lol –

Odpowiedz

19

Miałeś dobry pomysł z margin:auto 0; tylko zdjąć przykład 0.

robocza: http://jsfiddle.net/cxnR8/

<table style="border:solid;width: 100%"> 
    <tr> 
     <td> 
      <table style="margin:auto;border:solid; width:50%"> 
       <tr> 
        <td >I must be in the center</td>      
       </tr> 
      </table> 
     </td>      
    </tr> 
</table>​ 

Ale, co ważniejsze, czy naprawdę trzeba używać tabel i in-line stylizacja?

+0

To wszystko, ale atrybut wyrównania TD nie jest w ogóle potrzebny, prawda? –

+0

Tak, masz rację, nawet jeśli przeglądarki mogą wybrać aby obsługiwać go w trybie starszego typu/quirks, atrybut 'align' nie jest częścią specyfikacji HTML5 i nie powinieneś go używać. –

1

Twoja druga sugestia jest poprawna. See this working example.

HTML:

<table class="outer"> 
    <tr> 
     <td> 
      <table class="inner"> 
       <tr> 
        <td>in the middle</td>      
       </tr> 
      </table> 
     </td>      
    </tr> 
</table>​ 

CSS:

.outer 
{ 
    width: 100%; 
    border: solid 1px red; 
} 
.inner 
{ 
    width: 25%; 
    margin: auto; 
    border: solid 1px blue; 
} 
​ 
1

Centrum tabela użyciu przestarzałej align="" atrybut.

<table> 
    <tr> 
     <td> 
      <table align="center"> 
       <tr> 
        <td>in the middle</td>      
       </tr> 
      </table> 
     </td>      
    </tr> 
</table>​ 
Powiązane problemy