I have been working on a piece of code for several hours now, trying to achieve the desired layout for a table. However, every time I make adjustments to the code, it causes the table to become distorted or behave unexpectedly. My goal is to have a scrollbar that appears only for the scrollable content within the table, not for the entire table itself. Additionally, I want the scrollbar to be positioned specifically at the bottom of the scrollable content area.
Kindly help me on this one. Thank youuu!
.table-scroll {
position: relative;
width: 100%;
z-index: 1;
margin: auto;
height: 50vh;
width: 1000px;
}
.table-scroll table {
width: 100%;
margin: auto;
border-collapse: separate;
border-spacing: 0;
}
.table-scroll th,
.table-scroll td {
padding: 5px 10px;
border: 1px solid #000;
background: #fff;
vertical-align: top;
text-align: center;
}
.table-scroll thead th {
background: #333;
color: #fff;
top: 0;
z-index: 1;
}
th.sticky-left,
td.sticky-left {
position: -webkit-sticky;
position: sticky;
left: 0;
z-index: 4;
background: #ccc;
}
th.sticky-left:nth-child(2),
td.sticky-left:nth-child(2) {
position: -webkit-sticky;
position: sticky;
left: 70px;
z-index: 10;
background: #ccc;
}
th.sticky-right,
td.sticky-right {
position: -webkit-sticky;
position: sticky;
right: 0;
z-index: 4;
background: #ccc;
}
<div class="table-scroll">
<table border="1">
<thead>
<tr>
<th class="sticky-left w-32" rowspan="2">DATE</th>
<th class="sticky-left w-32" rowspan="2">DATE2</th>
<th colspan="10">LINE 1</th>
<th colspan="10">LINE 2</th>
<th class="sticky-right" rowspan="2">LINE 3</th>
</tr>
<tr>
<th>SENSOR 1</th>
<th>SENSOR 2</th>
<th>SENSOR 3</th>
<th>SENSOR 4</th>
<th>SENSOR 5</th>
<th>SENSOR 6</th>
<th>SENSOR 7</th>
<th>SENSOR 8</th>
<th>SENSOR 9</th>
<th>SENSOR 10</th>
<th>SENSOR 1</th>
<th>SENSOR 2</th>
<th>SENSOR 3</th>
<th>SENSOR 4</th>
<th>SENSOR 5</th>
<th>SENSOR 6</th>
<th>SENSOR 7</th>
<th>SENSOR 8</th>
<th>SENSOR 9</th>
<th>SENSOR 10</th>
</tr>
</thead>
<tbody>
<tr>
<td class="sticky-left w-32">VALUE 1</td>
<td class="sticky-left w-32">VALUE 1</td>
<td>SENSOR DATA 1</td>
<td>SENSOR DATA 2</td>
<td>SENSOR DATA 3</td>
<td>SENSOR DATA 4</td>
<td>SENSOR DATA 5</td>
<td>SENSOR DATA 6</td>
<td>SENSOR DATA 7</td>
<td>SENSOR DATA 8</td>
<td>SENSOR DATA 9</td>
<td>SENSOR DATA 10</td>
<td>SENSOR DATA 1</td>
<td>SENSOR DATA 2</td>
<td>SENSOR DATA 3</td>
<td>SENSOR DATA 4</td>
<td>SENSOR DATA 5</td>
<td>SENSOR DATA 6</td>
<td>SENSOR DATA 7</td>
<td>SENSOR DATA 8</td>
<td>SENSOR DATA 9</td>
<td>SENSOR DATA 10</td>
<td class="sticky-right">VALUE 1</td>
</tr>
<tr>
<td class="sticky-left w-32">VALUE 2</td>
<td class="sticky-left w-32">VALUE 2</td>
<td>SENSOR DATA 1</td>
<td>SENSOR DATA 2</td>
<td>SENSOR DATA 3</td>
<td>SENSOR DATA 4</td>
<td>SENSOR DATA 5</td>
<td>SENSOR DATA 6</td>
<td>SENSOR DATA 7</td>
<td>SENSOR DATA 8</td>
<td>SENSOR DATA 9</td>
<td>SENSOR DATA 10</td>
<td>SENSOR DATA 1</td>
<td>SENSOR DATA 2</td>
<td>SENSOR DATA 3</td>
<td>SENSOR DATA 4</td>
<td>SENSOR DATA 5</td>
<td>SENSOR DATA 6</td>
<td>SENSOR DATA 7</td>
<td>SENSOR DATA 8</td>
<td>SENSOR DATA 9</td>
<td>SENSOR DATA 10</td>
<td class="sticky-right">VALUE 2</td>
</tr>
<tr>
<td class="sticky-left w-32">VALUE 1</td>
<td class="sticky-left w-32">VALUE 1</td>
<td>SENSOR DATA 1</td>
<td>SENSOR DATA 2</td>
<td>SENSOR DATA 3</td>
<td>SENSOR DATA 4</td>
<td>SENSOR DATA 5</td>
<td>SENSOR DATA 6</td>
<td>SENSOR DATA 7</td>
<td>SENSOR DATA 8</td>
<td>SENSOR DATA 9</td>
<td>SENSOR DATA 10</td>
<td>SENSOR DATA 1</td>
<td>SENSOR DATA 2</td>
<td>SENSOR DATA 3</td>
<td>SENSOR DATA 4</td>
<td>SENSOR DATA 5</td>
<td>SENSOR DATA 6</td>
<td>SENSOR DATA 7</td>
<td>SENSOR DATA 8</td>
<td>SENSOR DATA 9</td>
<td>SENSOR DATA 10</td>
<td class="sticky-right">VALUE 1</td>
</tr>
</tbody>
</table>
</div>
2
Answers
Wrap the
<table>
in a block level element (ex.<div>
,<section>
,<p>
, etc…) and as an option you can assignoverflow-x
:scroll
to the block element which makes the scrollbar always present whether it’s actually needed or not. I preferscroll
because it doesn’t create shifting like the default value ofauto
when it pops in and out. Basically any block element can be scrollable and an inline-block element can be scrollable as well if it is styled withwhite-space
:nowrap
. Read this post for further details concerning what’s scrollable. What’s not scrollable are<table>
s and their associated children elements.The example below consists of 3
<table>
s each wrapped in a block element (<section>
) The first and last<table>
are a single column with a<thead>/<tr>/</th>
and 3<tr><th></th></tr>
in the<tbody>
. They are the "sticky" elements (more like "fixed", see comments in example). The middle<table>
(akatable.content
) contains the scrollable content witch consists of 20 columns and, when including the<thead>
, 4 rows . Note the scrollbar directly undertable.content
but not undertable.lead
ortable.last
👍. This layout is very responsive (not designed for mobile though) view it in Full page mode. Further details are commented in the example.You can achieve the said requirement by using
position: sticky;
.You can refer the following example.
Hope this helps.