【发布时间】:2015-07-04 17:35:55
【问题描述】:
我在 div 中有一个表格。
我的代码:https://jsfiddle.net/2cg29xLc/
HTML
<div>
<table>
<thead>
<!-- Example with only 2 columns -->
<tr>
<th>Col1</th>
<th>Col2</th>
</tr>
</thead>
<tbody>
<tr>
<td>foo</td>
<td>this</td>
</tr>
<tr>
<td>bar</td>
<td>that</td>
</tr>
</tbody>
</table>
CSS
div {
width: 99%;
}
table {
border-top: 2px solid #000;
border-bottom: 2px solid #000;
}
thead tr:nth-child(odd) {
background-color: #FFC0CB;
}
tbody tr:nth-child(even) {
background-color: #FFC0CB;
}
我的情况:
我想扩展表格的背景颜色和边框以匹配 div 的宽度。
看起来像这样:
https://jsfiddle.net/2cg29xLc/7/
我的头疼:
我无法设置表格的宽度:99%
因为设计要求列有自己的宽度,并且所有列都保持在左侧。
我遇到了这个Make background for table rows extend past the bounds of the table
但我无法让它与我的代码一起使用,可能是因为我不使用引导程序。
非常感谢任何帮助。
【问题讨论】:
-
在
table{...}CSS 规则中定义表格的宽度。目前它没有设置,所以默认为100% -
只需将“理想”小提琴示例中的 css 应用到您自己的小提琴中即可。不知道为什么这对您不起作用?
-
为什么不能设置表格宽度?
-
谷歌“CSS Flexbox”作为一种可能的替代方案(尽管这可能需要您在某些维度上设置表格宽度)
标签: css html html-table