Stick-n-Slide

Stick-n-Slide is designed to be a drop-in solution for creating tables with cells that remain fixed in place as the user scrolls.

Unlike other solutions, it does not require complicated modifications to your HTML, or support for CSS "position: sticky".

How to use

In your HTML/CSS:

  1. Make a table.

  2. Ensure that the the table cells that you want to stick in place have backgrounds and borders defined directly on them. Not on the rows, but on the cells themselves.

    Example:

    // Correct
    td {
      background: gray;
    }
    
    // Incorrect
    tr {
      background: gray;
    }
  3. Wrap the table in some containing element like a <div>, and make sure the <div> has height, width, and overflow specified correctly so the table can be scrolled if it is too large for its container.

  4. Add classes to the cells that you want to stick in place.

    .sns---is-stuck
    Ensures the cell does not move at all when scrolled, in either vertical or horizontal directions
    .sns--is-stuck-x
    Cell can be scrolled vertically, but not horizontally
    .sns--is-stuck-y
    Cell can be scrolled horizontally, but not vertically

In your javascript:

  1. Install Stick-n-Slide npm install stick-n-slide.

  2. Import Stick-n-Slide into your codebase import stickNSlide from 'stick-n-slide'.

  3. Get a reference to your table(s), either via native JS or jQuery.

    Example:

    // Native JS
    const tableElements = document.querySelectorAll('.my-tables');
    
    // jQuery
    const tableElements = $('.my-tables');
    
  4. Call stickNSlide(tableElements) on the tables whose cells you want to stick in place.

Demo

A1A2A3A4A5A6A7A8A9A10A11A12A13A14A15A16A17A18A19A20
B1B2B3B4B5B6B7B8B9B10B11B12B13B14B15B16B17B18B19B20
C1C2C3C4C5C6C7C8C9C10C11C12C13C14C15C16C17C18C19C20
D1D2D3D4D5D6D7D8D9D10D11D12D13D14D15D16D17D18D19D20
E1E2E3E4E5E6E7E8E9E10E11E12E13E14E15E16E17E18E19E20
F1F2F3F4F5F6F7F8F9F10F11F12F13F14F15F16F17F18F19F20
G1G2G3G4G5G6G7G8G9G10G11G12G13G14G15G16G17G18G19G20
H1H2H3H4H5H6H7H8H9H10H11H12H13H14H15H16H17H18H19H20
I1I2I3I4I5I6I7I8I9I10I11I12I13I14I15I16I17I18I19I20
J1J2J3J4J5J6J7J8J9J10J11J12J13J14J15J16J17J18J19J20
K1K2K3K4K5K6K7K8K9K10K11K12K13K14K15K16K17K18K19K20
L1L2L3L4L5L6L7L8L9L10L11L12L13L14L15L16L17L18L19L20
M1M2M3M4M5M6M7M8M9M10M11M12M13M14M15M16M17M18M19M20
N1N2N3N4N5N6N7N8N9N10N11N12N13N14N15N16N17N18N19N20
O1O2O3O4O5O6O7O8O9O10O11O12O13O14O15O16O17O18O19O20

Click on one of the cells in the above table, and then click one of the buttons below. You'll see that almost all native table functionality works automatically with Stick-n-Slide, including the ability to have colspan/rowspan on cells, as well as having cells grow and shrink automatically with their contents.