Div with horizontal scrolling only

前端 未结 5 1091
天命终不由人
天命终不由人 2020-12-04 07:25

I have a fixed width DIV containing a table with many columns, and need to allow the user to scroll the table horizontally within the DIV.

This needs to work on IE6

5条回答
  •  生来不讨喜
    2020-12-04 07:47

    The solution is fairly straight forward. To ensure that we don't impact the width of the cells in the table, we'll turn off white-space. To ensure we get a horizontal scroll bar, we'll turn on overflow-x. And that's pretty much it:

    .container {
        width: 30em;
        overflow-x: auto;
        white-space: nowrap;
    }
    

    You can see the end-result here, or in the animation below. If the table determines the height of your container, you should not need to explicitly set overflow-y to hidden. But understand that is also an option.

提交回复
热议问题