【问题标题】:Possible to have overflow: scroll without display: block?可能有溢出:滚动不显示:阻塞?
【发布时间】:2022-01-24 05:48:48
【问题描述】:

我有一张桌子: CSS:

.content-table{
    border-collapse: collapse;
    background-color: var(--secondaryBg);
    font-size: 0.9em;
    border-radius: 20px;
    box-shadow: 0 0 20px rgba(0,0,0,0.15);
    max-height: 55vh;   
}

我想添加一个overflow: scroll 以便能够在表格内滚动。当我申请display: block 时出现问题。如果没有 display 设置,所有列都占据整个宽度,但是使用 display: block 时,左侧列的右侧会有很多空白。

这些是应用于表格的其他设置:

.content-table thead{
    text-align: left;
    font-weight: bold;
    cursor: pointer;
}

.content-table th,
.content-table td {
    padding: 12px 15px;
}

.content-table td{
    width: 18%;
}

.content-table tr:not(:first-child){
    border-bottom: 1px solid;
}

.content-table tr:not(:first-child):hover{
    font-weight: bold;
    cursor: pointer;
}

非常感谢任何帮助!

【问题讨论】:

标签: html css html-table


【解决方案1】:

可能的解决方案

您好,我不知道这是否适合您,但希望对您有所帮助!无论如何,这里是如何做到这一点。制作一个简单 table,包含您想要的任意数量的内容。在这个表格中,我们将有一个固定的顶部/头部,但表格内的内容是可移动的。 抱歉,格式代码错误! :(

HTML

<table class="fixed_header">
      <thead>
        <tr>
          <th>Col 1</th>
          <th>Col 2</th>
          <th>Col 3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
        </tr>
        <tr>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
        </tr>
        <tr>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
        </tr>
        <tr>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
        </tr>
        <tr>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
          <td>Text</td>
        </tr>
      </tbody>
</table>

CSS

.fixed_header {
      width: 400px;
      table-layout: fixed;
      border-collapse: collapse;
}
.fixed_header tbody {
      display: block;
      width: 100%;
      overflow: auto;
      height: 100px;
}
.fixed_header thead tr {
      display: block;
}
.fixed_header thead {
      background: black;
      color: #fff;
}
.fixed_header th,
.fixed_header td {
      padding: 5px;
      text-align: left;
      width: 200px;
}

如果您想在浏览器中查看,click here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-02
    • 2015-10-25
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2021-12-18
    相关资源
    最近更新 更多