Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

CSS sticky columns

richarddunnebsc

Active Coder
I have a table with 10 columns. I want the header row, first three columns and the last column sticky.
I have managed to get the header row and first column sticky with the code below.
CSS:
table{
    table-layout: fixed;
    width: 100%;
    height: 100%;
    
}

table thead tr th{
    width: 150px;
}

table tbody tr td{
    text-align: center;
}

thead tr > th {
    background:#ccc;
    position: sticky;
    padding:.5rem;
    z-index:1;
    top: 0;
  }
 
  thead tr > :first-child,
  thead tr > :last-child {
    z-index:2;
    left:0;
    top:0;
  }
 
  td {
    text-align:center;
  }
 
  tbody tr > :first-child,
  tbody tr > :last-child {
    background:#ddd;
    position: sticky;
    z-index:3;
    left:0;
  }
I have tried using nth-child(), but to no avail.
Any help greatly appreciated
 
Almost there, got the row header and last row sticking, but can't get the first 3 columns to stick
CSS:
thead tr > th {
    background:#ccc;
    position: sticky;
    padding:.5rem;
    z-index: 3;
    top: 0;
  }
 
  thead tr > th:nth-child(-n+3),
  tbody tr > td:nth-child(-n+3)
   {
    position: sticky;
    right: 150px;
    background:#ddd;
  }
 
  td {
    text-align:center;
  }
 
  thead tr > :last-child,
  tbody tr > td:last-child {
    background:#ddd;
    position: sticky;
    right: 0;
  }

What am I missing?
 
Almost there, got the row header and last row sticking, but can't get the first 3 columns to stick
CSS:
  thead tr > th:nth-child(-n+3),
  tbody tr > td:nth-child(-n+3)
   {
    position: sticky;
    right: 150px;
    background:#ddd;
  }

  }

I believe this will place ALL 3 at right: 150px. You might need to do each one separately with different right values. Maybe?
 
I tried that
CSS:
thead tr > th:nth-child(-n+1),
  tbody tr > td:nth-child(-n+1)
   {
    position: sticky;
    right: 0;
    background:#ddd;
  }

  thead tr > th:nth-child(-n+2),
  tbody tr > td:nth-child(-n+2)
   {
    position: sticky;
    right: 150px;
    background:#ddd;
  }

  thead tr > th:nth-child(-n+3),
  tbody tr > td:nth-child(-n+3)
   {
    position: sticky;
    right: 300px;
    background:#ddd;
  }

Still not sticking.
 
I messed with that pen in dev tools and adding the code seen in screenshot,, I can get what you are looking for, I think:

1746131664742.webp

I added the class of sticky as well to the td s you see with inline styles
 
Last edited:
Back
Top Bottom