How to specify table's height such that a vertical scroll bar appears?

后端 未结 3 569
栀梦
栀梦 2020-12-04 15:54

I have a table with many rows on my page. I would like to set table\'s height, say for 500px, such that if the height of the table is bigger than that, a vertical scroll bar

相关标签:
3条回答
  • 2020-12-04 16:47

    You need to wrap the table inside another element and set the height of that element. Example with inline css:

    <div style="height: 500px; overflow: auto;">
     <table>
     </table>
    </div>
    
    0 讨论(0)
  • 2020-12-04 16:49

    Try using the overflow CSS property. There are also separate properties to define the behaviour of just horizontal overflow (overflow-x) and vertical overflow (overflow-y).

    Since you only want the vertical scroll, try this:

    table {
      height: 500px;
      overflow-y: scroll;
    }
    

    EDIT:

    Apparently <table> elements don't respect the overflow property. This appears to be because <table> elements are not rendered as display: block by default (they actually have their own display type). You can force the overflow property to work by setting the <table> element to be a block type:

    table {
      display: block;
      height: 500px;
      overflow-y: scroll;
    }
    

    Note that this will cause the element to have 100% width, so if you don't want it to take up the entire horizontal width of the page, you need to specify an explicit width for the element as well.

    0 讨论(0)
  • 2020-12-04 16:59

    to set the height of table, you need to first set css property "display: block" then you can add "width/height" properties. I find this Mozilla Article a very good resource to learn how to style tables : Link

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