Reputation: 2685
I am new to front-end-development. Here I have the following table:
<div className="table-responsive">
<table className="table table-hover" id="job-table">
<thead>
<tr className="text-center">
<th scope="col">Sr.No.</th>
<th scope="col">Company Name</th>
<th scope="col">Technology</th>
<th scope="col">Total Resumes</th>
<th scope="col">Job Title</th>
<th scope="col">Total Score</th>
<th scope="col">Average Score</th>
</tr>
</thead>
<tbody className="text-center">
{this.props.list && this.props.list.content && this.props.list.content.length > 0 && this.props.list.content.map((item, key) => {
return (
<tr key={key}>
<td className="font-weight-bold">1</td>
<td className="font-weight-bold">ABCDED</td>
<td>Software Developer</td>
<td className="font-weight-bold">17</td>
<td>5 years experience</td>
<td className="font-weight-bold">30</td>
<td className="font-weight-bold">30</td>
</tr>
)
})}
</tbody>
</table>
</div>
In this I have used the table-responsive class
. I have tried to make this table scrollable by using this:
tbody { height: 400px; overflow-y: scroll }
But this is not working.
One other thing about the content of the td
, if it is large the other rows gets affected. How can I avoid this scenario?
Upvotes: 13
Views: 77590
Reputation: 1116
use position : sticky at and top : 0
th {
background: white;
position: sticky;
top: 0; /* Don't forget this, required for the stickiness */
}
full example can be found here: https://css-tricks.com/position-sticky-and-table-headers/
Upvotes: 5
Reputation: 2225
Try using flex, it should be compatible with table-responsive
:
table {
display: flex;
flex-flow: column;
width: 100%;
}
thead {
flex: 0 0 auto;
}
tbody {
flex: 1 1 auto;
display: block;
overflow-y: auto;
overflow-x: hidden;
}
tr {
width: 100%;
display: table;
table-layout: fixed;
}
Hope this will help
Upvotes: 20
Reputation: 3387
add display:block
to the table to fix this
tbody{height: 400px; overflow-y: scroll;display:block;}
th { position: sticky; top: 0; }
Upvotes: 7
Reputation: 1129
Can set fixed header by using position: sticky
.
Check the sample code.
Here set the height to the main container.
.table-responsive{height:400px;overflow:scroll;}
Set the postion sticky to the tr-> th.
thead tr:nth-child(1) th{background: white; position: sticky;top: 0;z-index: 10;}
Upvotes: 54