【问题标题】:how do i insert a horizontal line in html table to separate rows如何在 html 表格中插入一条水平线来分隔行
【发布时间】:2021-01-29 20:41:17
【问题描述】:

我的代码如下,我想在表格中添加水平线来分隔每一行的内容:

.Favourites-table {
  position: relative;
  width: 70%;
  text-align: center;
  /*border: 1px solid orangered;*/
  margin: 0 auto;
  top: 130px;
  height: 300px;
}

.Favourites-table th {
  position: relative;
  border: 1px solid orangered;
  color: #fd886b;
  width: 100%;
  height: 300px;
}

.horizontal-line {
  position: relative;
  width: 100%;
  background-color: orangered;
  padding: 1px 0px;
}

.Favourites-table tr {
  position: relative;
  border: 1px solid orangered;
  width: 100%;
  height: 100px;
}
<table class="Favourites-table">
  <tr class="Favourites-titlerow">
    <th>Search name</th>
    <th>Email notifications</th>
    <th>options</th>
  </tr>
  <tr class="Favourites-row">
    <td>
      <image src="../1x/"></image>
      <h4>wetpoo</h4>
    </td>
    <td><span id="emailnotify-on" style="display: none;"><a href="#">On</a></span> <span class="hide-off" id="emailnotify-off" style="display: inline;"><a href="#">Off</a></span></td>
    <td> <a class="delete-row" href="#">Delete <a class="edit-search" href="#">Edit search</a></td>
  </tr>
  <tr class="Favourites-row">
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>

我尝试设置 TH 和 TR 的样式以添加该行,我还尝试使用 &lt;hr&gt; 创建一个行并将其添加到表中,但我无法让它工作。

【问题讨论】:

  • 你有没有尝试过,你能分享那个 CSS 吗?你在哪里卡住了?
  • 是的,我尝试设置 和 的样式以添加水平线,我还尝试使用
    手动将其添加到表格中,但我无法正确处理跨度>
  • 出于好奇,什么是“湿便”?
  • 顺便说一句,这是&lt;img /&gt;,而不是&lt;image&gt;&lt;/image&gt;。
  • wetpoo 只是一些测试数据

标签: html css


【解决方案1】:

您只需要在tr &gt; td 上设置border-bottom。

要跳过表格最后一行的边框,请使用tr:not(:last-of-type) &gt; td。

您可以直接在&lt;tr&gt; 而不是&lt;td&gt; 上设置边框,但您可能会遇到CSS 的表格格式模型的问题(它与CSS 的display: grid 完全无关且独立格式化模型,顺便说一句)。

此外,您应该使用显式的 &lt;thead&gt; 和 &lt;tbody&gt; 元素(它们始终存在于 DOM 中)(作为奖励,this makes it easier to use position: sticky; 意味着您不需要单独的 CSS 类名)。

因此,将您的 CSS 和 HTML 更改为如下所示的 sn-p:

(请注意,表格需要至少两行才能看到border-bottom,否则表格中的单行将与:not(:last-of-type) 选择器不匹配,因此它不会' t 渲染边框。

table.Favourites-table > tbody > tr:not(:last-of-type) > td {
    border-bottom: 3px solid black;
}

/* Additional styles you might want to use too: */
table.Favourites-table {
    border-collapse: collapse;
    width: 100%;
    border: 1px solid #eee;
}
table.Favourites-table > thead > tr > * {
    position: sticky;
    top: 0;
    background-color: orange; /* `position: sticky;` Needs an opaque background */
}
<table class="Favourites-table">
    <thead>
        <tr>
            <th>Search name</th>
            <th>Email notifications</th>
            <th>options</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <img src="../1x/" alt="picture" />
                <h4>wetpoo</h4>
            </td>
            <td>
                <span id="emailnotify-on" style="display: none;"><a href="#">On</a></span>
                <span class="hide-off" id="emailnotify-off" style="display: inline;"><a href="#">Off</a></span>
            </td>
            <td>
                <a class="delete-row" href="#">Delete <a class="edit-search" href="#">Edit search</a>
            </td>
        </tr>
        
        <tr>
            <td>
                <img src="../1x/" alt="picture" />
                <h4>wetpoo</h4>
            </td>
            <td>
                <span id="emailnotify-on" style="display: none;"><a href="#">On</a></span>
                <span class="hide-off" id="emailnotify-off" style="display: inline;"><a href="#">Off</a></span>
            </td>
            <td>
                <a class="delete-row" href="#">Delete <a class="edit-search" href="#">Edit search</a>
            </td>
        </tr>
        
        <tr>
            <td>
                <img src="../1x/" alt="picture" />
                <h4>wetpoo</h4>
            </td>
            <td>
                <span id="emailnotify-on" style="display: none;"><a href="#">On</a></span>
                <span class="hide-off" id="emailnotify-off" style="display: inline;"><a href="#">Off</a></span>
            </td>
            <td>
                <a class="delete-row" href="#">Delete <a class="edit-search" href="#">Edit search</a>
            </td>
        </tr>
    </tbody>
</table>

【讨论】:

  • 谢谢@Dai,这很有效,非常有帮助,愿wetpoo 保佑你在危险的编码冒险之旅中保佑你
猜你喜欢
相关资源
最近更新 更多
热门标签