表格标题位置:粘滞和边框问题

时间:2019-07-23 14:15:06

标签: html css twitter-bootstrap

this article之后,我有一个表试图获取粘性标题。除了我的表格样式,标题的顶部和底部都有边框。

我不了解的部分是,应用于th的顶部/底部边框与表格的其余部分一起滚动,而不是与“卡住的”标题单元格保持一致。有什么可以做的吗?

可以在这里找到工作示例:https://codepen.io/smlombardi/pen/MNKqQY?editors=1100#0

.table-sticky-container {
  height: 200px;
  overflow-y: scroll;
  border-top: 1px solid green;
  border-bottom: 1px solid green;
}

.table-sticky {

  th {
    position: sticky;
    top: 0;
    z-index: 2;
    border-top: 1px solid red !important;
    border-bottom: 2px solid red !important;
  }
}
<div class="table-sticky-container">
  <table class="table table-sticky">
    <thead class="thead-light">
      <tr>
        <th scope="col">Name</th>
        <th scope="col">Title</th>
        <th scope="col">ID</th>
        <th scope="col">Username</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Malcolm Reynolds</td>
        <td>Captain</td>
        <td>9035749867</td>
        <td>mreynolds</td>
      </tr>
     ...
    </tbody>
  </table>
</div>

5 个答案:

答案 0 :(得分:4)

您可以添加

.table {
  border-collapse: separate;
  border-spacing: 0;
}

答案 1 :(得分:2)

您可以添加

.table {
  border-collapse: separate;
}

但不幸的是,它看起来很糟糕,一种更好的解决方案是使用伪元素添加解决方法。

th:after,
th:before {
  content: '';
  position: absolute;
  left: 0;
  width: 100%;
}

th:before {
  top: -1px;
  border-top: 1px solid red;
}

th:after {
  bottom: -1px;
  border-bottom: 2px solid red;
}

.table-sticky-container {
  height: 200px;
  overflow-y: scroll;
  border-top: 1px solid green;
  border-bottom: 1px solid green;
}

.table-sticky th {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  z-index: 2;
}

th:after,
th:before {
  content: '';
  position: absolute;
  left: 0;
  width: 100%;
}

th:before {
  top: -1px;
  border-top: 1px solid red;
}

th:after {
  bottom: -1px;
  border-bottom: 2px solid red;
}
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css'>

<div class="table-sticky-container">
    <table class="table table-sticky">
      <thead class="thead-light">
        <tr>
          <th scope="col">Name</th>
          <th scope="col">Title</th>
          <th scope="col">ID</th>
          <th scope="col">Username</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Malcolm Reynolds</td>
          <td>Captain</td>
          <td>9035749867</td>
          <td>mreynolds</td>
        </tr>
        <tr>
          <td>Zoe Washburne</td>
          <td>First Officer</td>
          <td>8908980980</td>
          <td>zwashburne</td>
        </tr>
        <tr>
          <td>Kaylee Frye</td>
          <td>Engineer</td>
          <td>6678687678</td>
          <td>kfrye</td>
        </tr>
        <tr>
          <td>Malcolm Reynolds</td>
          <td>Captain</td>
          <td>9035749867</td>
          <td>mreynolds</td>
        </tr>
        <tr>
          <td>Zoe Washburne</td>
          <td>First Officer</td>
          <td>8908980980</td>
          <td>zwashburne</td>
        </tr>
        <tr>
          <td>Kaylee Frye</td>
          <td>Engineer</td>
          <td>6678687678</td>
          <td>kfrye</td>
        </tr>
      </tbody>
    </table>
  </div>

第二种解决方法

.table {
  border-collapse: collapse;
}

.table-sticky thead th {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  z-index: 2;
  box-shadow: inset 0 1px 0 red, inset 0 -2px 0 red;
 }

答案 2 :(得分:1)

请勿使用border-collapse并按如下所示绘制线条:

td, th {
    border-bottom: 1px solid grey;
    border-right: 1px solid grey;
}

table {
    border-spacing: 0px;
    border-left: 1px solid grey
}

th {
    background-color:#c5e8ec;
    position: sticky;
    position: -webkit-sticky;
    border-top: 1px solid grey;
    top:0;
}

答案 3 :(得分:0)

Another working solution(已在最新的Chrome和FF上进行了测试)-将div内容包装在div中,并使用这些div边框代替单元格的边框。

ActiveDocument.FollowHyperlink _ 
 Address:="https://www.Microsoft.com", _ 
 NewWindow:=True, AddHistory:=True

答案 4 :(得分:0)

另一个解决方案可以是:

table {
box-shadow:  1px 1px 1px 1px black; 
}

...种类