1

I am trying to create a table. I want its first column to be fixed. I have done it using the following

tr > th:first-child,
tr > td:first-child {
  position: sticky;
  left: 0;
}

This works fine. I want to make the all headings also fixed. The above code only makes the first th fixed. I want to make all th and first td in a row fixed. and all td in a tr except the first one, scrollable.

This is my table and CSS I have given till now.

.container {
  margin: 0px;
 padding : 0px;
  overflow-x: auto;
}

table {
  font-family: "Open Sans", sans-serif;
  position: relative;
  border-collapse: collapse;
  border-spacing: 0;
  table-layout: auto;
  width: 100%;
  border: none;
  border-radius: 0.5rem;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
    0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

thead tr {
  color: #ececec;
  font-size: 1rem;
  font-weight: 500;
  text-align: left;
}

th {
  background: #eefecd;
  padding: 0.75rem 1.5rem;
  vertical-align: middle;
}

tbody tr:nth-child(odd) td {
  background: #fff;
}

tbody tr:nth-child(even) td {
  background: #ececec;
}

td {
  text-align: left;
  padding: 1.5rem 1.5rem;
  vertical-align: middle;
  font-size: 1.125rem;
  font-weight: normal;
}

tr:last-child td:first-child {
  border-bottom-left-radius: 0.5rem;
}

th:first-child {
  border-top-left-radius: 0.5rem;
}

tr:last-child td:last-child {
  border-bottom-right-radius: 0.5rem;
}

th:last-child {
  border-top-right-radius: 0.5rem;
}

tr > th:first-child,
tr > td:first-child {
  position: sticky;
  left: 0;
}
<div class="container p-0 m-0">
  <table>
    <thead>
      <tr>
        <th>DATA1</th>
        <th>DATA2</th>
        <th>DATA3</th>
        <th>DATA4</th>
        <th>DATA1</th>
        <th>DATA2</th>
        <th>DATA3</th>
        <th>DATA4</th>
        <th>DATA1</th>
        <th>DATA2</th>
        <th>DATA3</th>
        <th>DATA4</th>
        <th>DATA1</th>
        <th>DATA2</th>
        <th>DATA3</th>
        <th>DATA4</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
      </tr>
      <tr>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
      </tr>
      
    </tbody>
  </table>
</div>

1 Answer 1

0

I am able to do it by using position: fixed on the tr row of the table:

.container {
  position: relative;
  margin: 0px;
  padding: 0px;
  overflow-x: auto;
}

table {
  font-family: "Open Sans", sans-serif;
  border-collapse: collapse;
  border-spacing: 0;
  table-layout: auto;
  width: 100%;
  border: none;
  border-radius: 0.5rem;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

thead tr {
  color: #ececec;
  font-size: 1rem;
  font-weight: 500;
  text-align: left;
}

th {
  background: black;
  padding: 0.75rem 1.5rem;
  vertical-align: middle;
}

tbody tr:nth-child(odd) td {
  background: #fff;
}

tbody tr:nth-child(even) td {
  background: #ececec;
}

td {
  text-align: left;
  padding: 1.5rem 1.5rem;
  vertical-align: middle;
  font-size: 1.125rem;
  font-weight: normal;
}

tr:last-child td:first-child {
  border-bottom-left-radius: 0.5rem;
}

th:first-child {
  border-top-left-radius: 0.5rem;
}

tr:last-child td:last-child {
  border-bottom-right-radius: 0.5rem;
}

th:last-child {
  border-top-right-radius: 0.5rem;
}

tr>td:first-child {
  position: sticky;
  left: 0;
}

thead>tr:first-child {
position: fixed;
left: 0px;
}

thead {
  height: 42px;
}
<div class="container p-0 m-0">
  <table>
    <thead>
      <tr>
        <th>DATA1</th>
        <th>DATA2</th>
        <th>DATA3</th>
        <th>DATA4</th>
        <th>DATA1</th>
        <th>DATA2</th>
        <th>DATA3</th>
        <th>DATA4</th>
        <th>DATA1</th>
        <th>DATA2</th>
        <th>DATA3</th>
        <th>DATA4</th>
        <th>DATA1</th>
        <th>DATA2</th>
        <th>DATA3</th>
        <th>DATA4</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
      </tr>
      <tr>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
        <td>Some values</td>
      </tr>

    </tbody>
  </table>
</div>

2
  • Hi Dear @NarenMurali, This styling hide the td visiblity. I am implementing this in a laravel blade page. And i have another contents above this table.
    – Vismaya
    Commented Jan 22 at 8:48
  • ^ If you have content above a sticky container be sure to place it within a relative container so the sticky container wont go out of it Commented Jan 22 at 13:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.