css - 如何在 Firefox 中使用 <col> 和 CSS 将填充应用于列?

标签 css html-table padding

例子:

<style type="text/css">
    table {
        border: 1px solid red;
        border-collapse: collapse;
        text-align: left;
    }
    #col-1 {
        padding-left: 20px;
        background-color: tan;
    }
    #specific-cell {
        padding-left: 20px;
    }
</style>
<table>
    <col id="col-1">
    <col id="col-2">
    <tr>
        <th>foo</th>
        <th>bar</th>
    </tr>
    <tr>
        <td>data1</th>
        <td>data2</th>
    </tr>
    <tr>
        <td id="specific-cell">data1</th>
        <td>data2</th>
    </tr>
    <tr>
        <td>data1</th>
        <td>data2</th>
    </tr>
</table>

颜色应用于列而不是填充。如果我直接在单元格上使用类/id,它就可以工作。

我是被迫使用它们,还是有办法利用 <col>标签?

最佳答案

它不应该工作,至少在 CSS 2.1 中是这样。你可以看看 CSS 2.1 table columns specification .

您可以使用 :first-child+ 来规避此问题:

/* first column */
td:first-child {
    padding-left: 20px;  
}

/* second column */ 
td:first-child + td {
    padding-left: 10px;  
}

/* third columns */ {
td:first-child + td + td {
    padding-left: 0;  
}

关于css - 如何在 Firefox 中使用 <col> 和 CSS 将填充应用于列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/868009/

相关文章:

javascript - 如何从最左边的点开始获取谷歌图表的X轴点

css - 将子元素背景颜色应用于父元素的填充

jquery - 修复了使用 css transition 后标题跳转的问题

javascript - 当以特定方式单击图标时,为什么我的网页的一部分会消失?

javascript - 在 Javascript 中删除带有复选框的表格行

css - 当 td 内容太宽时表格溢出父 div

html - Css 悬停不起作用

css - 如何避免带有内联关联输入的 'bottom-aligned' 标签?

html - Bootstrap 绝对定位的导航栏不会位于相对定位的横幅图像之上

css - 当标题在不太宽的容器中中断时向右填充