2014-08-27 74 views
1

我正在使用CSS display propertytable,table-rowtable-cell值來以表格的形式佈局我的文檔。使第二個表格中的單元格的寬度等於第一個表格中的單元格的寬度?

通常情況下(你可以在代碼中看到)的細胞的表的大小有關,包含(直到我們提供硬編碼維度值的內容。

但我想table-cell小號在第二個(依此類推)table-row與第一個table-row對應的table-cell具有相同的寬度。因此,第一行中第一個table-cell的寬度與第一個table-row中第一個table-cell的寬度相同。是否有可能?

我想要這樣做,而不必在第二個和後續的table-row s中添加pixelmarginpadding等屬性。我只是想讓它上面的單元格的width

所以最後,列的寬度將等於列中最寬的單元格的寬度。

我該怎麼做?

Here is the fiddle.在代碼中,三行有不同的顏色。

CODE:

<div style="display:table;"> 
    <div style="background-color:#d9a4f4; display:table-row;"> 
     <label style="display:table-cell; padding-left:10px; padding-right:10px;">Inferrable</label> 
     <label style="display:table-cell; padding-left:10px; padding-right:10px;">Not inferrable</label> 
    </div> 
    <div style="background-color:#7cbfdd; display:table-row;"> 
     <input type="radio" name="radio_group1" value="0" style="display:table-cell;"> 
     <input type="radio" name="radio_group1" value="0" style="display:table-cell;"> 
     <label style="display:table-cell;">I am the label for the first group of radio buttons. I am the label for the first group of radio buttons. I am the label for the first group of radio buttons. I am the label for the first group of radio buttons. I am the label for the first group of radio buttons. </label> 
    </div> 
    <div style="background-color:#e8c277; display:table-row;"> 
     <input type="radio" name="radio_group2" value="0" style="display:table-cell;"> 
     <input type="radio" name="radio_group2" value="1" style="display:table-cell;"> 
     <label style="display:table-cell;">I am the label for the second group of radio buttons. I am the label for the second group of radio buttons. I am the label for the second group of radio buttons. </label> 
    </div> 
</div> 
+1

哈啊!不要這樣做。如果你想讓它們完全匹配或使用表格佈局(*咳嗽*) – 2014-08-27 13:28:41

+0

不要使用內聯樣式,請在單獨的CSS文件中定義樣式。 – Dan 2014-08-27 13:30:18

+1

您每行的單元格數量不正確 - 如果您希望它們相等,則它們應匹配,因爲display-table沒有像colspan這樣的內容。因爲你的數據看起來好像它可以被分類爲表格,所以你應該使用一個適當的表格,並在第一行設置'th'和'scope'。 – Pete 2014-08-27 13:33:33

回答

1

如果一個單元格添加到您的第一行,然後在跨度換你輸入:

<div style="display:table;"> 
    <div style="background-color:#d9a4f4; display:table-row;"> 
     <label style="display:table-cell; padding-left:10px; padding-right:10px;">Inferrable</label> 
     <label style="display:table-cell; padding-left:10px; padding-right:10px;">Not inferrable</label> 
     <span style="display:table-cell;"></span> 
    </div> 
    <div style="background-color:#7cbfdd; display:table-row;"> 
     <span style="display:table-cell; text-align:center;"><input type="radio" name="radio_group1" value="0" style="display:table-cell;"></span> 
     <span style="display:table-cell; text-align:center;"><input type="radio" name="radio_group1" value="0" style="display:table-cell;"></span> 
     <label style="display:table-cell;">I am the label for the first group of radio buttons. I am the label for the first group of radio buttons. I am the label for the first group of radio buttons. I am the label for the first group of radio buttons. I am the label for the first group of radio buttons.</label> 
    </div> 
    <div style="background-color:#e8c277; display:table-row;"> 
     <span style="display:table-cell; text-align:center;"><input type="radio" name="radio_group2" value="0" style="display:table-cell;"></span> 
     <span style="display:table-cell; text-align:center;"><input type="radio" name="radio_group2" value="1" style="display:table-cell;"></span> 
     <label style="display:table-cell;">I am the label for the second group of radio buttons. I am the label for the second group of radio buttons. I am the label for the second group of radio buttons.</label> 
    </div> 
</div> 

您應該能夠達到你想要的東西 - example

Titles on one line instead of wrapping

1

所有DIVlabelinput元素是完全獨立的,並且即使將它們設置display: table-cell他們對彼此的尺寸沒有內部關係,沒有知識。

您必須明確定義寬度以使其寬度匹配。 (那麼你基本上不需要爲他們提供display-屬性)。

這裏有一個的jsfiddle演示與修改您的代碼

http://jsfiddle.net/erlingormar/ga1hzrLd/

這對於修改的結構,你可以用它來調整你的元素,而在表樣佈局考慮他們的建議:

+0

不,不,我正在尋找的是,單選按鈕應該在第一行的前兩個單元格下('可推理'和'不可推薦'標籤)。 – Solace 2014-08-27 13:51:59

+0

顯式寬度的問題在於,在第一行中,有時候它會包含與'標籤'一樣小的'Yes'和'No'的單詞,有時還會有像'inferrable'和'Not inferrable'這樣的長單詞。所以例如如果我考慮'Not Inferrable'使它成爲'width:150px',那麼對於'No'這個詞來說就太寬了 – Solace 2014-08-27 13:52:26