【问题标题】:Bootstrap 4 tables with transparent border带透明边框的 Bootstrap 4 表
【发布时间】:2019-01-26 05:42:53
【问题描述】:

我正在尝试使行之间的边框透明,以便页面背景的颜色显示出来。

边框来自td,背景色来自tr,所以如果我让td边框透明,tr的背景就会透出来。我尝试删除 th td 边框并仅使用 tr 边框,但我似乎无法让边框出现。

考虑下表,使用标准引导程序 4.2 css:

<div style="background-color:white">
    <table class="table table-gap table-striped table-striped-alt">
        <tbody>
            <tr>
                <td>A</td><td>B</td>
            </tr>
            <tr>
                <td>C</td><td>D</td>
            </tr>
            <tr>
                <td>E</td><td>F</td>
            </tr>
        </tbody>
    </table>
</div>

我添加了自己的 CSS。这会为奇数行着色,而标准的 boostrap 表条带颜色为偶数行。它还尝试在 tr 和 th/td 上都显示透明边框,这不起作用:

<style type="text/css">   
    /*Color the alternating rows that table-striped does not
    .table-striped-alt tr:not(:nth-of-type(odd))
    {
        background-color: #007bff !important;
    } 

    .table-gap tr > *
    {
        border-top: 3px solid transparent !important;
    }
    .table-gap tr
    {
        border-top: 3px solid transparent !important;
    }
</style>

所以我尝试将 tr 边框更改为黄色,将 td/th 更改为红色,看看是否有任何影响。 td/th显示了边框,但是tr没有显示边框,td做到了:

<style type="text/css">    
    .table-striped-alt tr:not(:nth-of-type(odd))
    {
        background-color: #007bff !important;
    }
    .table-gap tr > *
    {
        border-top: 3px solid red !important;
    }
    .table-gap tr
    {
        border-top:3px solid yellow !important
    }
</style>

所以我玩了border-collapse,并将其设置为'separate',仍然没有在tr上显示边框。它实际上在所有单元格周围都有一个透明的间距。这是不可取的,因为我只想要单元格上方和下方的透明间距。

<style type="text/css">    
    .table-striped-alt tr:not(:nth-of-type(odd))
    {
        background-color: #007bff !important;
    }
    .table-gap tr > *
    {
        border-top: 3px solid red !important;
    }
    .table-gap tr
    {
        border-top:3px solid yellow !important
    }

    table
    {
     border-collapse:separate;
    }
</style>

【问题讨论】:

标签: twitter-bootstrap bootstrap-4


【解决方案1】:

表格边框可能很棘手。相反,只需间隔单元格。

body {
  background-color: lightgreen;
}

.table {
  width: 100%;
  border-collapse: separate; /* boom */
  border-spacing: 0 5px; /* bam */
}

.table td {
  background-color: pink;
  border: 5px solid transparent;
}

.table-striped-alt tr:not(:nth-of-type(odd)) {
  background-color: #007bff !important;
}

.table-gap tr>* {
  border-top: 3px solid red !important;
}

.table-gap tr {
  border-top: 3px solid yellow !important
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" />

<div>
  <table class="table">
    <tbody>
      <tr>
        <td>A</td>
        <td>B</td>
      </tr>
      <tr>
        <td>C</td>
        <td>D</td>
      </tr>
      <tr>
        <td>E</td>
        <td>F</td>
      </tr>
    </tbody>
  </table>
</div>

【讨论】:

  • 哦,边框间距消除了同一行单元格之间的空间,太棒了!这适用于我的大多数情况。是否可以关闭某些行的边框间距?我有一些实例,我有多行行,所以我有多个 tbody 元素充当可视行,每个 tbody 元素可能有多个 tr ,所以我可以获得多行“行”。在这种情况下,我想要 tbody 元素之间的空间。
  • 您可以通过索引选择行,或者通过向间隔行添加一个类,以及其他技术。您应该为此提出一个新问题。
猜你喜欢
  • 2013-01-13
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
相关资源
最近更新 更多