Setting a max height on a table

前端 未结 9 1324
猫巷女王i
猫巷女王i 2020-12-29 01:10

I am trying to design a page where there are some tables. It seems that styling tables is much more painful than it ought to be.

The problem is the following: The ta

相关标签:
9条回答
  • 2020-12-29 01:34

    I had a coworker ask how to do this today, and this is what I came up with. I don't love it but it is a way to do it without js and have headers respected. The main drawback however is you lose some semantics due to not having a true table header anymore.

    Basically I wrap a table within a table, and use a div as the scroll container by giving it a max-height. Since I wrap the table in a parent table "colspanning" the fake header rows it appears as if the table respects them, but in reality the child table just has the same number of rows.

    One small issue due to the scroll bar taking up space the child table column widths wont match up exactly.

    Live Demo

    Markup

    <table class="table-container">
        <tbody>
            <tr>
                <td>header col 1</td>
                <td>header col 2</td>
            </tr>
            <tr>
                <td colspan="2">
                    <div class="scroll-container">
                        <table>
                            <tr>
                                <td>entry1</td>
                                <td>entry1</td>
                            </tr>
                           ........ all your entries
                        </table>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
    

    CSS

    .table-container {
        border:1px solid #ccc;
        border-radius: 3px;
        width:50%;
    }
    .table-container table {
        width: 100%;
    }
    .scroll-container{
        max-height: 150px;
        overflow-y: scroll;
    }
    
    0 讨论(0)
  • 2020-12-29 01:35

    Use divs with max height and min height around the content that needs to scroll.

    <tr>
        <td>
            <div>content</div>
        </td>
    </tr>
    
    td div{
        max-height:20px;
    }
    

    https://jsfiddle.net/ethanabrace/4w0ksczr/

    0 讨论(0)
  • 2020-12-29 01:37

    Add display:block; to the table's css. (in other words.. tell the table to act like a block element rather than a table.)

    fiddle here

    0 讨论(0)
  • 2020-12-29 01:37
    • Set display: block; for the table
    • Set position: sticky; top: 0; for the header row
    <table style="display: block; height: 100px; overflow: auto;">
      <thead>
        <tr>
          <td style="position: sticky; top: 0;">Header stays put</td>
          <td style="position: sticky; top: 0;">Layout aligned</td>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>foo1</td>
          <td>Header stays put</td>
        </tr>
        <tr>
          <td>foo2</td>
          <td>Header stays put</td>
        </tr>
      </tbody>
    </table>
    

    https://jsfiddle.net/0zxk18fp/

    Tested on Chrome, Firefox, Safari, Edge

    0 讨论(0)
  • 2020-12-29 01:42

    Seems very similar to this question. From there it seems that this should do the trick:

    table {
      display: block; /* important */
      height: 600px;
      overflow-y: scroll;
    }
    
    0 讨论(0)
  • 2020-12-29 01:44

    You can do this by using the following css.

    .scroll-thead{
        width: 100%;
        display: inline-table;
    }
    
    .scroll-tbody-y
    {
        display: block;
        overflow-y: scroll;
    }
    
    .table-body{
        height: /*fix height here*/;
    }
    

    Following is the HTML.

    <table>
     <thead class="scroll-thead">
              <tr> 
               <th>Key</th>
               <th>Value</th>
              </tr> 
             </thead>
             <tbody class="scroll-tbody-y table-body">
              <tr> 
              <td>Blah</td> 
              <td>Blah</td> 
              </tr> 
    </tbody>
    </table>
    

    JSFiddle

    0 讨论(0)
提交回复
热议问题