回答
這是我知道的唯一解決方案,而不使用JavaScript。它具有指定TR和TD高度的缺點,因爲TR是絕對定位的。我也不確定它在其他瀏覽器中的表現如何。
<style>
tr {
position: absolute;
background: url(http://www.google.com/intl/en_ALL/images/logo.gif) no-repeat;
height: 200px;
border: 1px solid #00f;
}
td {
width: 200px;
height: 200px;
border: 1px solid #f00;
}
</style>
<table cellpadding="0" cellspacing="0">
<tr>
<td></td>
<td></td>
</tr>
</table>
這對我的作品在所有瀏覽器:
tr {
background: transparent url(/shadow-down.gif) no-repeat 0% 100%;
display: block;
position: relative;
width: 828px;
}
tr td{
background: transparent;
}
當我改變我的tr顯示:block ...它失去了所有的列格式(即行不再與標題對齊)。你如何解決這個問題? – davekaro 2010-12-09 21:42:53
這對我來說不適用於chrome - 它導致整行佔據表格的一列,而其他行佔據表格的右側。 – 2011-10-11 16:10:50
您可能需要每個td的顯式寬度。 – 2011-10-11 17:06:51
瀏覽器(WebKit的)覆蓋的位置:相對的錶行,並計算風格靜態的。錯誤或功能 - 這可以防止位置:IE7中使用的相對修復。
溶液中使用固定的(已知的)小區大小時: TR { 浮子:左; }
並將適當的單元格大小添加到表中的所有單元格(通過類或內聯樣式)。我還添加了行大小,但解決方案可能不需要。
我不需要動態單元大小的解決方案,所以我一直沒有去探索這個用例。
我建議直接設置表格單元格的背景圖像,將所有後續單元格的背景位置向左移動,使其看起來應該如此。這樣你就不必切分你的背景圖像。
我最終什麼事做的是:
table.money-list tr {
background: url(images/status-2.gif) no-repeat;
}
@media screen and (-webkit-min-device-pixel-ratio:0) {
/*Chrome CSS here*/
table tbody tr td:first-child {
background-image: none;
background-color: transparent;
}
table tr td{
background-color: #FFFFFF;
}
}
因此,大家可以看到,只是設置背景顏色到每一個TD,但第一位的,而目標的Webkit。
這是我的jQuery解決方案。這是一個thead tr,但也應該爲tbody tr工作。
$(document).ready(function() {
// Apply <thead><tr> background row fix for Chrome/Safari (webkit)
if ($.browser.webkit) {
$('thead tr').each(function (i) {
var theadWidth = $(this).width()-1;
$(this).append('<div style="position:absolute;top:'+$(this).position().top+'px;left:'+$(this).position().left+'px;z-index:0;width:'+theadWidth+'px;height:'+($(this).height()+8)+'px;background:#2e4b83 url(th_background.jpg) no-repeat 0 0;background-size:'+theadWidth+'px 100%;"></div>');
});
}
});
您可以在Chrome/Firefox/IE9中查看。它應該看起來一樣。
沒有提到的解決方法對我來說很合適。這是我結束了:
TABLE TBODY.highlight {
background-image: url(path/image.png);
}
TABLE>* {
float: left;
clear: both;
}
僅適用於Chrome和Firefox測試,所以不知道它是如何工作的IE瀏覽器(對我來說並不重要),但對我來說這完美的作品!
不是一個可行的解決方案,因爲浮動左擊桌子,但是將背景應用到tbody而不是表格行的想法是天才。加上「
WebKit的做法與在Quirks模式下的Opera相同。 IE修復也需要'td {background-image:none; }或IE會在每個單元格內容區域再次渲染圖像。真的,這從來沒有可靠的跨瀏覽器,我不確定像相對定位這樣的黑客的副作用是你應該依賴的。找到另一種方法? – bobince 2010-04-07 17:59:35
我的答案正在被mods刪除,但我會在這裏發表評論,請使用:background-attachment:fixed; – thouliha 2015-08-15 14:32:18