2011-10-24 11 views
8

我有一個100%寬度的表3列。中央欄的寬度必須是600px。如何在使用剩餘空間的同時獲得另外兩個相同的寬度?表3列。固定中心列寬。如何在其他兩列上共享寬度?

<table style="width: 100%"> 
    <tr> 
     <td>left</td> 
     <td style="width: 600px">center</td> 
     <td>right</td> 
    </tr> 
</table> 

目前,根據左列或右列的內容,它們總是不均勻。我試過在左右兩側設置50%的寬度以及其他數字和最小寬度的試驗。

請不要jquery/javascript。謝謝

+0

檢查提到下面COLGROUP標籤 – Nishant

回答

-2

我想「align = center」與center列一起使用可能對你有幫助。

0

使用「colgroup」標籤可能對此有很大的幫助。

<table class="fixed-center-table" border="1"> 
     <thead> 
      <colgroup> 
       <col> 
       <col id="middle-column"> 
       <col> 
      </colgroup> 
      <tr> 
       <th>First Column</th> 
       <th></th> 
       <th>Third Column</th> 
      </tr> 
     </thead> 

     <tbody> 
      <tr> 
       <td>AAAAAA</td> 
       <td>BBBB</td> 
       <td>CCCCC</td> 
      </tr> 
      <tr> 
       <td>AAAAAA</td> 
       <td>BBBB</td> 
       <td>CCCCC</td> 
      </tr> 
      <tr> 
       <td>AAAAAA</td> 
       <td>BBBB</td> 
       <td>CCCCC</td> 
      </tr> 
     </tbody> 
</table> 

.fixed-center-table { 
    table-layout: fixed; 
    width: 100%; 
    border-collapse: collapse; 
} 

.fixed-center-table td{ 
    text-align: center; 
} 

col#middle-column { 
    width: 600px; 
} 
+0

這是更好的解決方案,並應被用於管理表的寬度的溶液。 – Nishant

4

對這個老問題的新答案。

  1. 給中間一列th一個max-widthmin-width,而不是width
  2. 給左,右th50%width
  3. 根本不給table a width

我已經修復了這個例子中列width300px

jsBin Example!

CSS

table { 
    border-collapse: collapse; 
} 
th, td { 
    border: solid 1px #CCC; 
    padding: 10px; 
} 
.fixed { 
    max-width: 300px; 
    min-width: 300px; 
} 
.fluid { 
     width: 50%; 
} 

HTML

<table> 
    <thead> 
     <tr> 
      <th class="fluid">First Column</th> 
      <th class="fixed">Fixed Column</th> 
      <th class="fluid">Third Column</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td></td> 
      <td></td> 
      <td></td> 
     </tr> 
    </tbody> 
</table> 
相關問題