Reputation: 757
I'm using react-table along with styled components and trying to make a tbody scrollable with sticky thead. I've tried to set a height to a parent container and then set tbody overflow-y to auto, however this approach doesn't work. What am I doing wrong?
const TableContainerOuter = styled.div`
width: 100%;
height: 100%;
overflow-y: hidden;
padding-bottom: 21px;
`;
const TableContainer = styled.div`
width: 100%;
height: 300px;
min-width: 900px;
`;
const StyledTable = styled.table`
width: 100%;
position: relative;
height: 30px;
border-spacing: 0px;
th {
position: sticky;
top: 0;
left: 0;
background: #f3f4f5;
font-size: 9px;
font-weight: 500;
line-height: 230%;
letter-spacing: 0.05em;
color: ${(props) => props.theme.pbDarker};
text-transform: uppercase;
text-align: left;
padding: 6px 0px;
user-select: none;
&:first-child {
border-top-left-radius: 6px;
border-bottom-left-radius: 6px;
padding-left: 22px;
}
&:last-child {
border-top-right-radius: 6px;
border-bottom-right-radius: 6px;
}
}
tbody {
overflow-y: scroll;
height: 100px;
}
td {
padding: 16px 0px;
border-style: solid;
border-width: 0px;
font-size: 12px;
font-weight: 400;
border-bottom-width: 1px;
border-color: ${(props) => props.theme.pbFaded1};
min-width: 95px;
:first-of-type {
padding-left: 22px;
}
}
`;
Upvotes: 2
Views: 10492
Reputation: 1
You can achieve this with such solution
tbody {
height: calc(100vh - 310px); // Your prefered height here
table-layout: fixed;
display: block;
overflow-y: auto;
overflow-x: hidden;
}
thead, tbody tr {
display: table;
width: 100%;
table-layout: fixed;
}
Upvotes: 0
Reputation: 19
Create 2 tables. One with the column headings one without any. Set the second table to scroll
Upvotes: 1
Reputation: 259
You're not applying overflow. You can see if you inspect your table. Add overflow to your TableContainer and it will work. See down below
const TableContainer = styled.div`
width: 100%;
height: 100px;
min-width: 900px;
overflow-y: scroll;
`;
Upvotes: 1