【问题标题】:Horizontally and vertically responsive table with sticky/fixed headers?带有粘性/固定标题的水平和垂直响应表?
【发布时间】:2015-11-06 18:49:01
【问题描述】:

在我正在构建的网站的页面上,只会显示一个表格。无论屏幕的宽度/高度如何,我都希望这张桌子贴在屏幕的边缘。

此表还需要有粘性/固定标题(因此当它们向下滚动时,标题仍然可见),我发现这会使事情变得复杂,因为似乎需要手动设置宽度和高度。

此外,它需要能够水平滚动,以防有大量列。我不希望它们都是 10px 宽,但是随着尺寸的变化,表格本身仍然需要贴在屏幕边缘。

它可以在用户滚动时保持标题的粘性,但是元素的宽度和高度都必须手动设置,这意味着它没有任何响应。

这可能吗?我不反对 JavaScript,但稍微偏爱纯 CSS 解决方案。

(JSFiddle)

body {
  margin: 0;
  padding: 0;
}
table {
  table-layout: fixed;
  border-collapse: collapse;
  width: 375px;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch;
  -webkit-border-horizontal-spacing: 0;
}
th,
td {
  text-align: left;
  padding: 10px 10px;
  width: 105px;
  word-wrap: break-word;
  word-break: break-all;
  -webkit-hyphens: auto;
  hyphens: auto;
}
thead {
  background-color: black;
  color: #fff;
}
thead tr {
  display: block;
  position: relative;
}
tbody {
  display: block;
  overflow: auto;
  height: 300px;
}
<table>
  <thead>
    <tr>
      <th>Longer title for testing</th>
      <th>Color</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Apple ioeras eionars ienraist eioarn stio enar sotinera oietnar i i i i i i i i i i i i i i i i i i i i i to</td>
      <td>Red aeinrs tienras tienr eiostnnearhstniehrastneihrsaetinh iaroes nte narse itnar einaer ns eanr enrsena ernes netnea rnst sr</td>
      <td>These are red.</td>
    </tr>
    <tr>
      <td>Pear</td>
      <td>Green</td>
      <td>These are green.</td>
    </tr>
    <tr>
      <td>Grape</td>
      <td>Purple / Green</td>
      <td>These are purple and green.</td>
    </tr>
    <tr>
      <td>Orange</td>
      <td>Orange</td>
      <td>These are orange.</td>
    </tr>
    <tr>
      <td>Banana</td>
      <td>Yellow</td>
      <td>These are yellow.</td>
    </tr>
    <tr>
      <td>Kiwi</td>
      <td>Green</td>
      <td>These are green.</td>
    </tr>
    <tr>
      <td>Plum</td>
      <td>Purple</td>
      <td>These are Purple</td>
    </tr>
    <tr>
      <td>Watermelon</td>
      <td>Red</td>
      <td>These are red.</td>
    </tr>
    <tr>
      <td>Tomato</td>
      <td>Red</td>
      <td>These are red.</td>
    </tr>
    <tr>
      <td>Cherry</td>
      <td>Red</td>
      <td>These are red.</td>
    </tr>
    <tr>
      <td>Cantelope</td>
      <td>Orange</td>
      <td>These are orange inside.</td>
    </tr>
    <tr>
      <td>Honeydew</td>
      <td>Green</td>
      <td>These are green inside.</td>
    </tr>
    <tr>
      <td>Papaya</td>
      <td>Green</td>
      <td>These are green.</td>
    </tr>
    <tr>
      <td>Raspberry</td>
      <td>Red</td>
      <td>These are red.</td>
    </tr>
    <tr>
      <td>Blueberry</td>
      <td>Blue</td>
      <td>These are blue.</td>
    </tr>
    <tr>
      <td>Mango</td>
      <td>Orange</td>
      <td>These are orange.</td>
    </tr>
    <tr>
      <td>Passion Fruit</td>
      <td>Green</td>
      <td>These are green.</td>
    </tr>
  </tbody>
</table>

【问题讨论】:

    标签: javascript html css responsive-design html-table


    【解决方案1】:

    您可以使用 position: fixed in thead tag(in css) 将表格标题固定在顶部 对于全宽设置宽度:100%

    【讨论】:

      【解决方案2】:

      这是你要找的吗?

      https://jsfiddle.net/6o1gbq6x/

      <style>
      body {
        margin: 0;
        padding: 0;
      }
      .table_container { /*responsive container*/
        width: 100%;
        margin: 0 auto;
        height: 100%;
        max-height: 300px;
      }
      
      table {
        border-collapse: collapse;
        width: auto;
        -webkit-overflow-scrolling: touch;
        -webkit-border-horizontal-spacing: 0;
      }
      th,
      td {
        text-align: left;
        padding: 10px 10px;
        width: 33%;
        min-width: 100px;
        word-wrap: break-word;
        word-break: break-all;
        -webkit-hyphens: auto;
        hyphens: auto;
      }
      thead {
        background-color: black;
        color: #fff;
      }
      thead tr {
        display: block;
        position: relative;
      }
      tbody {
        display: block;
        overflow: auto;
        height: 300px;
      }
      </style>
      
      <div class="table_container">
      <table>
        <thead>
          <tr>
            <th>Longer title for testing</th>
            <th>Color</th>
            <th>Description</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Apple ioeras eionars ienraist eioarn stio enar sotinera oietnar i i i i i i i i i i i i i i i i i i i i i to</td>
            <td>Red aeinrs tienras tienr eiostnnearhstniehrastneihrsaetinh iaroes nte narse itnar einaer ns eanr enrsena ernes netnea rnst sr</td>
            <td>These are red.</td>
          </tr>
          <tr>
            <td>Pear</td>
            <td>Green</td>
            <td>These are green.</td>
          </tr>
          <tr>
            <td>Grape</td>
            <td>Purple / Green</td>
            <td>These are purple and green.</td>
          </tr>
          <tr>
            <td>Orange</td>
            <td>Orange</td>
            <td>These are orange.</td>
          </tr>
          <tr>
            <td>Banana</td>
            <td>Yellow</td>
            <td>These are yellow.</td>
          </tr>
          <tr>
            <td>Kiwi</td>
            <td>Green</td>
            <td>These are green.</td>
          </tr>
          <tr>
            <td>Plum</td>
            <td>Purple</td>
            <td>These are Purple</td>
          </tr>
          <tr>
            <td>Watermelon</td>
            <td>Red</td>
            <td>These are red.</td>
          </tr>
          <tr>
            <td>Tomato</td>
            <td>Red</td>
            <td>These are red.</td>
          </tr>
          <tr>
            <td>Cherry</td>
            <td>Red</td>
            <td>These are red.</td>
          </tr>
          <tr>
            <td>Cantelope</td>
            <td>Orange</td>
            <td>These are orange inside.</td>
          </tr>
          <tr>
            <td>Honeydew</td>
            <td>Green</td>
            <td>These are green inside.</td>
          </tr>
          <tr>
            <td>Papaya</td>
            <td>Green</td>
            <td>These are green.</td>
          </tr>
          <tr>
            <td>Raspberry</td>
            <td>Red</td>
            <td>These are red.</td>
          </tr>
          <tr>
            <td>Blueberry</td>
            <td>Blue</td>
            <td>These are blue.</td>
          </tr>
          <tr>
            <td>Mango</td>
            <td>Orange</td>
            <td>These are orange.</td>
          </tr>
          <tr>
            <td>Passion Fruit</td>
            <td>Green</td>
            <td>These are green.</td>
          </tr>
        </tbody>
      </table>
      </div>
      

      如果没有,请告诉我,我会调整。

      【讨论】:

      • 您的表格标题是垂直固定的,但不是相对水平的。当表格获得较小的宽度并显示水平滚动时,仅显示内容正在滚动并且标题被冻结。
      【解决方案3】:

      如果正文高度和宽度设置为 100%,它将允许子(表格)占据整个页面。

      body{
          height: 100%;
          width: 100%;
      }
      
      table{
          height: 100%;
          width: 100%;
      }
      

      【讨论】:

      • edit 提供更多信息。纯代码答案是discouraged,因为它们不包含可搜索的内容,也没有解释为什么有人应该“试试这个”。我们在这里努力成为知识的资源。
      • 我认为让表格具有响应性并不是这里的重点。这很简单。但是使标题水平相对和垂直固定是困难的。
      猜你喜欢
      • 2019-09-26
      • 2020-09-28
      • 2016-10-11
      • 1970-01-01
      • 2021-01-25
      • 2011-02-07
      • 1970-01-01
      • 2016-08-29
      • 1970-01-01
      相关资源
      最近更新 更多