html - 如何在第一行下方绘制水平分隔线?

标签 html css html-table

我有一个这样的表:

+------+------+
| col1 | col2 | // row1
| col1 | col2 | // row2
| col1 | col2 | // row3
+------+------+

现在我想在 row1 下画一条线。我可以用这种方式选择它:

tr:nth-child(1){ // selecting first row }

或者这样:

tr:first-child { // selecting first row }

但我不知道为什么 border-bottom 在这种情况下不起作用。其实我想要这样的东西:

+------+------+
| col1 | col2 | // row1
+------+------+
| col1 | col2 | // row2
| col1 | col2 | // row3
+------+------+ 

需要注意的是,其他属性也可以,像这样:

tr:first-child {
    color: red;                  // the color will be red
    border-bottom: 1px solid;    // it does not work
}

那么,我该怎么做呢?

最佳答案

将以下内容添加到您的 CSS:

table  {
    border-collapse: collapse;
}

此外,您还必须为边框设置颜色:border-bottom:1px solid red;

完整的例子是这样的:

table  {
  border-collapse:collapse;
}
tr:first-child {
  color:red;                
  border-bottom:1px solid red;  
}
<table>
    <tr>
        <td>col1</td>
        <td>col2</td>
        <td>col3</td>
    </tr>
    <tr>
        <td>col1</td>
        <td>col2</td>
        <td>col3</td>
    </tr>
    <tr>
        <td>col1</td>
        <td>col2</td>
        <td>col3</td>
    </tr>
</table>

关于html - 如何在第一行下方绘制水平分隔线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33352556/

相关文章:

javascript - 悬停时用两个按钮覆盖表格单元格

javascript - 如何从单选按钮中的数据库加载值?

html - 照片浏览器中的framework7 rtl上一个/下一个按钮

html - 2 列,一侧流体,一侧固定

html - 带双边框的 CSS 表格

特定表格行的CSS

html - 如何在没有包装元素的情况下设置大于元素本身的背景大小

javascript - 如何使用 HTML 和 CSS 创建可切换按钮

javascript - Bootstrap Navbar 居中但 DIV 高于内容

css - 在 Chrome 的用户代理样式表中,为什么使用 "-webkit-margin-before"等而不是 "margin-top"等?