2011-06-17 69 views
35

文本,我有以下的html代碼:如何頂部,左對齊的​​細胞跨越多個行

<table border="1"> 
    <tr> 
    <th>Month</th> 
    <th>Savings</th> 
    </tr> 
    <tr> 
    <td>January</td> 
    <td>$100</td> 
    </tr> 
    <tr> 
    <td>February</td> 
    <td rowspan="2">Save a lot</td> 
    </tr> 
    <tr> 
    <td>March</td> 
    </tr> 
</table> 

使用CSS樣式或另一種方法,我可以使文字「節省了大量」頂,是否有理由?

回答

62
td[rowspan] { 
    vertical-align: top; 
    text-align: left; 
} 

參見:CSS attribute selectors

+2

。對於IE,您需要詳細說明跨行的行,在本例中爲'td [rowspan = 「2」]' - 顯然是DTD **默認**設置''is consi即使未明確指定,也會匹配'td [rowspan]',因此它實際上匹配** ALL **'td' - 不管它是否應該是另一回事,這也適用於'colspan' - [the長故事](http://www.highdots.com/forums/cascading-style-sheets/re-tables-colspan-298379.html) – clairesuzy

+0

這解決了我的問題...... – Ziggler

7
<td rowspan="2" style="text-align:left;vertical-align:top;padding:0">Save a lot</td> 

應該這樣做。

+0

注意這一點,如果你想將無法正常工作,以垂直對齊:底部,因爲它對齊到除了IE Bug(6/7/8)以外,不在兩行單元格的底部 – user1260310

1

試試這個

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<style> 
 
table, th, td { 
 
    border: 1px solid black; 
 
} 
 
</style> 
 
</head> 
 
<body> 
 

 
<table style="width:50%;"> 
 
    <tr> 
 
     <th>Month</th> 
 
     <th>Savings</th> 
 
    </tr> 
 
    <tr style="height:100px"> 
 
     <td valign="top">January</td> 
 
     <td valign="bottom">$100</td> 
 
    </tr> 
 
</table> 
 

 
<p><b>Note:</b> The valign attribute is not supported in HTML5. Use CSS instead.</p> 
 

 
</body> 
 
</html>

利用VALIGN =「頂」的TD風格

+0

「valign」屬性不再支持HTML5。使用CSS「vertical-align」例如vertical-align:top;或垂直對齊:底部; –