2011-04-14 52 views
33

我有一個HTML表格,我希望前幾列很長。我在CSS中這樣做:爲什麼我的HTML表格不尊重我的CSS列寬?

td.longColumn 
{ 
    width: 300px; 
} 

,這裏是我的表

<table> 
    <tr> 
    <td class='longColumn'></td> 
    <td class='longColumn'></td> 
    <td class='longColumn'></td> 
    <td></td> 
    <td></td> 
    <td></td> 
    <td></td> 
    <td></td> 
    [ . . and a bunch more columns . . .] 
    </tr> 
</table> 

的簡化版本,出於某種原因,該表似乎讓這列< 300像素的時候也有很多列。我基本上希望它保持這個寬度,不管怎樣(只需增加水平滾動條)。

表格在裏面的容器沒有任何類型的最大寬度,所以我不知道爲什麼它壓縮這個列而不是尊重這個寬度。

反正有,所以無論如何,這個列會保持一定的寬度?
這裏是外容器div的CSS:

#main 
{ 
    margin: 22px 0 0 0; 
    padding: 30px 30px 15px 30px; 
    border: solid 1px #AAAAAA; 
    background-color: #fff; 
    margin-bottom: 30px; 
    margin-left: 10px; 
    _height: 1px; /* only IE6 applies CSS properties starting with an underscrore */ 
    float: left; 
    /*width: 1020px;*/ 
    min-width:1020px; 
    display: block; 
    overflow: visible; 
    z-index: 0; 
} 

回答

75

您可能會收到鐵道部如果將規則table-layout: fixed應用於表格,則表示運行時爲表格單元格設置寬度 - 在使用表格時,這有助於解決很多cell-sizing問題。如果它符合表格的目的,我不會建議切換到僅使用DIV來安排您的內容 - 以顯示多維數據。

+8

我不得不添加'table-layout:fixed;寬度:100%;'它工作。謝謝! – nrodic 2016-01-18 19:19:15

+0

這個和nrodic的評論幫助了我,謝謝! – 2017-03-10 19:16:36

-1

無法修改<td> width;即,列的寬度是不可設置。您可以添加可能有所幫助的造型white-space:nowrap;。或者,您可以添加&nbsp; s來爲列添加空間。

也許你可以設置山坳寬度的HTML方式:<td width="70%">January>/td>

不幸的是,HTML 4.01,後來,這種方式是無效的。

+0

我看到他們在這裏編輯它: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_td_width – leora 2011-04-14 22:31:23

+0

@ooo - 是的,但他們使用HTML設置寬度的方式,而不是CSS。也許OP可以這樣做。 – 2011-04-14 22:36:21

+0

@ooo - OP ==原始海報,在Usenet中說。哦,現在我看到OP是你:-) – 2011-04-15 00:39:35

-8

如何這樣的事情...

http://jsfiddle.net/qabwb/1/

HTML

<div id="wrapper"> 
    <div class="row"> 
     <div class="column first longColumn">stuff</div> 
     <div class="column longColumn">more stuff</div> 
     <div class="column">foo</div> 
     <div class="column">jsfiddle</div> 
    </div> 
    <div class="row"> 
     <div class="column first longColumn">stuff</div> 
     <div class="column longColumn">more stuff</div> 
     <div class="column">foo</div> 
     <div class="column">jsfiddle</div> 
    </div> 
    <div class="row"> 
     <div class="column first longColumn">stuff</div> 
     <div class="column longColumn">more stuff</div> 
     <div class="column">foo</div> 
     <div class="column">jsfiddle</div> 
    </div> 
</div> 

CSS

#wrapper { 
    min-width: 450px; 
    height: auto; 
    border: 1px solid lime; 
} 

.row { 
    padding: 4px; 
} 
.column { 
    border: 1px solid orange; 
    border-left: none; 
    padding: 4px; 
    display: table-cell; 
} 

.first { 
    border-left: 1px solid orange; 
} 

.longColumn { 
    min-width: 150px; 
} 
+1

這個解決方案當然不是防彈的。 http://jsfiddle.net/qabwb/140/ – tribe84 2013-05-02 03:49:24

+1

有很多情況下表仍然是一個適當的東西使用。例如,表格數據! – Oneezy 2013-05-26 20:09:12

17

我同意Hristo,但有些情況下需要使用表格,並且表格問題的解決方案是在表格下方添加類別,然後根據需要更改任何td寬度。

.tables{ border-collapse:collapse; table-layout:fixed;} 

我希望這有助於尋找表格解決方案的人!

+0

在這裏,我在想,HTML/CSS有一些**錯誤**,因爲它不會爲應用於'table'和'td'標籤或樣式的每個表格指定的寬度渲染表格列和表格相同。相反,只是**我太笨了,不知道我應該做一個CSS聲明,將'table-layout'屬性設置爲'fixed'。我是一個白癡**。感謝您爲我設置直線 - 當沒有其他任何事情時,它會完美運行。它甚至解決了下拉列表控制的問題,迫使列的寬度達到我想要的寬度的2倍。 – 2012-12-08 23:03:09

+2

應該提到我使用table-layout:使用width:auto固定以實現我想要的結果。 – 2012-12-08 23:09:47

+0

完美地工作。謝謝。 – 2014-04-23 05:50:21

3

我有一堆列我想要間隔柱的問題。 我以前做的:

<td style='width: 10px;'>&nbsp;</td> 

但是當表比窗口寬,間隔是不是真的10px的,但也許爲5px。 在我的情況下,僅使用沒有TABLE的DIV不是一種選擇。 所以,我想:

<td><div style='width: 10px;'></div></td> 

而且效果很不錯! :)

+1

例如:http:// jsfiddle。net/allicarn/2RSHe/1/ – allicarn 2014-02-10 18:54:02

+0

使用內聯樣式工作。我也必須調整列標題,並給出'width:auto;' – thekodester 2017-01-23 17:41:46

2

設置列寬(td's)的最佳方法是使用表頭(th's)。表格標題會自動設置您的td的寬度。你只需要確保你的帖子裏的你的列與你的tbody中的列數相同。

看看這裏: http://jsfiddle.net/tKAj8/

HTML

<table> 
    <thead> 
     <tr> 
      <th class="short-column">Short Column</th> <!-- th sets the width --> 
      <th class="short-column">Short Column</th> <!-- th sets the width --> 
      <th class="long-column">Long Column</th> <!-- th sets the width --> 
     </tr> 
    </thead> 

    <tbody> 
     <tr> 
      <td class="lite-gray">Short Column</td> <!-- td inherits th width --> 
      <td class="lite-gray">Short Column</td> <!-- td inherits th width --> 
      <td class="gray">Long Column</td> <!-- td inherits th width --> 
     </tr> 
    </tbody> 
</table> 

CSS

table { table-layout: fixed; border-collapse: collapse; border-spacing: 0; width: 100%; } 

.short-column { background: yellow; width: 15%; } 
.long-column { background: lime; width: 70%; } 

.lite-gray { background: #f2f2f2; } 
.gray { background: #cccccc; } 
7

給予它既max-widthmin-width屬性應該工作。

+0

的其他標題樣式,這對我很有用。 – Snowman 2016-06-25 23:48:06

+1

這是這裏最好的答案。 – 2016-08-09 19:00:14

+0

GOLD!非常感謝。這花了幾個小時。只有'最小寬度'對我來說是必要的。 – poshest 2016-09-03 19:15:59

0

在表格中使用表格佈局屬性和「固定」值。

table { 
    table-layout: fixed; 
    width: 300px; /* your desired width */ 
} 

後在TD的百分比設置表的整個寬度, 你現在可以設置寬度。

td:nth-child(1), td:nth-child(2) { 
    width: 15%; 
} 

您可以瞭解更多關於此鏈接:http://www.w3schools.com/cssref/pr_tab_table-layout.asp

0

我的問題與不能夠在table-layout: fixed表大小的列指使用合併單元格。對於任何人遇到問題,即建議上述方法無效的變體的利益,COLGROUP工作對我來說(對OP的代碼變化):

div { 
 
    margin: 22px 0 0 0; 
 
    padding: 30px 30px 15px 30px; 
 
    border: solid 1px #AAAAAA; 
 
    background-color: #fff; 
 
    margin-bottom: 30px; 
 
    margin-left: 10px; 
 
    _height: 1px; /* only IE6 applies CSS properties starting with an underscrore */ 
 
    float: left; 
 
    /*width: 1020px;*/ 
 
    min-width:1020px; 
 
    display: block; 
 
    overflow: visible; 
 
    z-index: 0; 
 
} 
 
td.longColumn { 
 
    width: 300px; 
 
} 
 
table { 
 
    border: 1px solid; 
 
    text-align: center; 
 
    width: 100%; 
 
} 
 
td, tr { 
 
    border: 1px solid; 
 
}
<div> 
 
    <table> 
 
     <colgroup> 
 
      <col class='longColumn' /> 
 
      <col class='longColumn' /> 
 
      <col class='longColumn' /> 
 
      <col/> 
 
      <col/> 
 
      <col/> 
 
      <col/> 
 
     </colgroup> 
 
     <tbody> 
 
     <tr> 
 
      <td colspan="7">Stuff</td> 
 
     </tr> 
 
     <tr> 
 
      <td>Long Column</td> 
 
      <td>Long Column</td> 
 
      <td>Long Column</td> 
 
      <td>Short</td> 
 
      <td>Short</td> 
 
      <td>Short</td> 
 
      <td>Short</td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 
</div>

相關問題