CSS-Only Sticky Table Headers In Chrome

后端 未结 4 680
说谎
说谎 2020-11-30 02:40

I have the following Sass snippet in which I want the to float as the table scrolls. This works properly in Safari, but not in Chrome Versio

相关标签:
4条回答
  • 2020-11-30 03:30

    For somebody, who is still looking for a solution and isn't satisfied with the accepted one.

    1) Use sticky-top class on th element.

    2) With own class

    th.sticky-header {
      position: sticky;
      top: 0;
      z-index: 10;
      /*To not have transparent background.
      background-color: white;*/
    }
    <table class="table">
      <thead>
        <tr>
          <th class="sticky-header">1</th>
          <th class="sticky-header">2</th>
          <th class="sticky-header">3</th>
          <th class="sticky-header">4</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>2</td>
          <td>3</td>
          <td>4</td>
        </tr>
        <tr>
          <td>1</td>
          <td>2</td>
          <td>3</td>
          <td>4</td>
        </tr>
        <tr>
          <td>1</td>
          <td>2</td>
          <td>3</td>
          <td>4</td>
        </tr>
      </tbody>
    </table>

    0 讨论(0)
  • 2020-11-30 03:32

    You set the sticky position on the header table-cell instead of table-row.

    .td.header {
      position: sticky;
      top:0px;
    }
    

    Check out this jsfiddle for simple example.

    0 讨论(0)
  • 2020-11-30 03:38

    https://jsfiddle.net/y9cwnb81/4/

    div.table {
      width: 100%;
      display: grid;
      grid-template-columns: 1fr 1fr 1fr;
      grid-template-rows: 1fr auto;
    
      grid-template-areas:
        "header header header"
        "content content content";
    }
    
    .header {
      grid: 1fr/1fr;
    }
    
    .content {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr;
      grid-template-rows: auto;
      grid-area: content;
      height: 200px;
      overflow-y: scroll;
    }
    
    .content div {
      grid: auto-flow 1fr / repeat(auto-fill);
    }
    
    <!-- Here is the table code -->
    
    <div class="table">
      <div class="header">Name</div>
      <div class="header">Color</div>
      <div class="header">Description</div>
      <div class="content">
        <div>Apple</div>
        <div>Red</div>
        <div>These are red asd,mas, da,msnd asndm,asndm,asndbansbdansmbdmnasbd.</div>
    
        <div>Apple</div>
        <div>Red</div>
        <div>These are red asd.</div>
    
        <div>Apple</div>
        <div>Red</div>
        <div>These are red asd.</div>
    
        <div>Apple</div>
        <div>Red</div>
        <div>These are red asd.</div>
    
        <div>Apple</div>
        <div>Red</div>
        <div>These are red asd.</div>
    
        <div>Apple</div>
        <div>Red</div>
        <div>These are red asd.</div>
    
        <div>Apple</div>
        <div>Red</div>
        <div>These are red asd.</div>
    
        <div>Apple</div>
        <div>Red</div>
        <div>These are red asd.</div>
    
        <div>Apple</div>
        <div>Red</div>
        <div>These are red asd.</div>
    
        <div>Apple</div>
        <div>Red</div>
        <div>These are red asd.</div>
    
        <div>Apple</div>
        <div>Red</div>
        <div>These are red asd.</div>
      </div>
    </div>
    

    Here is an example using CSS GRID to have sticky headers only with CSS, no javascript required.

    0 讨论(0)
  • 2020-11-30 03:40

    position: sticky doesn't work with some table elements (thead/tr) in Chrome. You can move sticky to tds/ths of tr you need to be sticky. Like this:

    .table {
      thead tr:nth-child(1) th{
        background: white;
        position: sticky;
        top: 0;
        z-index: 10;
      }
    }
    

    Also this will work.

    .table {
        position: sticky;
        top: 0;
        z-index: 10;
    }
    

    You can move header to separate layout. For example:

    <table class="table">
        <thead>
        <tr>
            <th>1</th>
            <th>2</th>
            <th>3</th>
            <th>4</th>
        </tr>
        </thead>
    </table>
    <table>
        <tbody>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
        </tr>
    </table>
    
    0 讨论(0)
提交回复
热议问题