【问题标题】:Setting Internal Borders within table using css使用css在表格内设置内部边框
【发布时间】:2015-01-04 03:27:42
【问题描述】:

阅读this Stack Overflow question - (不要误会我的意思)有效。但我似乎找不到根据我的要求编辑它的方法。

目前,JSFiddle 给出:

 Heading | Heading
---------+---------
 Row     | Row
---------+---------
 Row     | Row
---------+---------
 Row     | Row

如何将其编辑为:

 Heading | Heading
---------+---------
 Row     | Row
 Row     | Row
 Row     | Row

相反?

我尝试了很多组合,但似乎永远无法让它发挥作用! :(


我还注意到接受的答案是 +5 岁。 它仍然是“更好”/“最佳”的解决方案吗?


顺便说一句,我不擅长 css(实际上,我非常糟糕),因此非常感谢您的帮助(毕竟,我最终得到了 this - 完全 错了!)。

【问题讨论】:

  • 我可能是错的,但你追求的是this?

标签: html css


【解决方案1】:

删除您的table tr:last-child td 组(因为它是多余的)并添加样式以从您的<td>s 中删除任何顶部或底部边框。

table {
    border-collapse: collapse;
}
table td, table th {
    border: 1px solid black;
}
table tr:first-child th {
    border-top: 0;
}

/* ---- This change --------- */
table tr td {
    border-bottom: 0;
    border-top:0;
}
/* ------------- */

table tr td:first-child,
table tr th:first-child {
    border-left: 0;
}
table tr td:last-child,
table tr th:last-child {
    border-right: 0;
}
<table>
    <tr>
        <th>Heading 1</th>
        <th>Heading 2</th>
    </tr>
    <tr>
        <td>Cell (1,1)</td>
        <td>Cell (1,2)</td>
    </tr>
    <tr>
        <td>Cell (2,1)</td>
        <td>Cell (2,2)</td>
    </tr>
    <tr>
        <td>Cell (3,1)</td>
        <td>Cell (3,2)</td>
    </tr>
</table>

【讨论】:

  • 是否有办法将其设置为仅此特定表(其他表可能具有不同的样式),即使其具有唯一的类?
  • 当然,如果您的表具有可定位属性,例如id:#myTable tr td{ }。
  • 这样就删除了 table 这个词并用 #myTablesID 替换它?
  • 或者table#MyTable,但由于ids 需要在文档范围内是唯一的,所以没有必要。
【解决方案2】:

检查这个小提琴:fiddle

<table>
    <tr>
        <th>Heading 1</th>
        <th>Heading 2</th>
    </tr>
    <tr>
        <td class="inner">Cell (1,1)</td>
        <td class="inner">Cell (1,2)</td>
    </tr>
    <tr>
        <td class="inner">Cell (2,1)</td>
        <td class="inner">Cell (2,2)</td>
    </tr>
    <tr>
        <td class="inner">Cell (3,1)</td>
        <td class="inner">Cell (3,2)</td>
    </tr>
</table>

table {
    border-collapse: collapse;
}
table td, table th {
    border: 1px solid black;
}
table tr:first-child th {
    border-top: 0;
}
table tr:last-child td {
    border-bottom: 0;
}
table tr td:first-child,
table tr th:first-child {
    border-left: 0;
}
table tr td:last-child,
table tr th:last-child {
    border-right: 0;
}

.inner {
    border-bottom:1px white solid;
}

【讨论】:

    【解决方案3】:

    只需在您的 css 中使用您的 th 执行此操作,它就会为您的 th 添加底部边框。

    table tr th{
        border:0px solid transparent;
        border-bottom-width:1px;
        border-bottom-color:black;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-07
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 2014-03-25
      • 2015-07-28
      • 2012-02-21
      相关资源
      最近更新 更多