Fixed header and scrollable body

后端 未结 6 656
面向向阳花
面向向阳花 2020-12-14 11:26

I have like 15 columns in the table. I want to scroll both horizontal and vertical and header should be fixed when I scroll vertical. I have tried various examples but no

6条回答
  •  佛祖请我去吃肉
    2020-12-14 12:15

    I have created a pure CSS solution to this problem that may work better for some people than the accepted answer (which I can't seem to get working at all). Unlike most of the other scrolling-body tables I've seen that don't require fixed widths, mine has a minimum width that is determined by its content. It will wrap text if possible to avoid overflowing its container (except in the headers, which don't allow soft-wrapping, unfortunately), but once the wrapping opportunities are used up, it won't get any narrower. That forces the parent element (usually the body tag) to handle the horizontal scrolling, which keeps the headers and columns in sync.

    Here's a fiddle.

    Here's the code:

    HTML

    Top Caption
    Column 4
    Column 4
    Lorem ipsumDolorSitAmet consectetur
    Lorem ipsumDolorSitAmet consectetur
    Lorem ipsumDolorSitAmet consectetur
    Lorem ipsumDolorSitAmet consectetur
    Lorem ipsumDolorSitAmet consectetur
    Lorem ipsumDolorSitAmet consectetur
    Lorem ipsumDolorSitAmet consectetur
    Lorem ipsumDolorSitAmet consectetur
    Lorem ipsumDolorSitAmet consectetur
    Lorem ipsumDolorSitAmet consectetur
    Lorem ipsumDolorSitAmet consectetur
    Lorem ipsumDolorSitAmet consectetur
    Faux bottom caption

    CSS