Hi all
I have a codepen here - https://codepen.io/anon/pen/LdaKEe
I need a table with a sticky header that also scrolls left to right at smaller screen sizes.
I think this is the best way to do it with thead and tbody set the display: block;
and overflow-y: scroll;
on the tbody
The problem with this is I have to set the widths of all the th and td and I also have the set the width of table.
Setting the widths of the th and td is fine but setting a width on the table means I could have space after the table on the right unless I work out the width of the table with the set widths of the columns.
body{
background: grey;
font-family: sans-serif;
}
.page{
background: white;
width: 800px;
margin: 0 auto;
}
.table-con{
overflow: scroll;
width: 800px;
height: 500px;
}
thead th, tbody td{
border: 1px solid red;
}
table{
border-collapse: collapse;
width: 1500px;
}
thead th:first-child{
width: 20px;
padding: 0;
}
th{
text-align: left;
}
thead{
border-bottom: 5px solid red;
tr{
display: block;
position: relative;
}
}
tbody{
display: block;
height: 400px;
overflow-y: scroll;
}
tbody tr:nth-child(even) td:not(:first-child){
background: yellow;
}
tbody td:first-child{
background: none;
border: none;
padding: 0;
}
th, td{
padding: 10px 5px;
}
th:nth-child(1),
td:nth-child(1){
background: none;
border: none;
width: 20px;
}
th:nth-child(2),
td:nth-child(2){
width: 90px;
}
th:nth-child(3),
td:nth-child(3){
width: 200px;
}
th:nth-child(4),
td:nth-child(4){
width: 90px;
}
th:nth-child(5),
td:nth-child(5){
width: 90px;
}
th:nth-child(6),
td:nth-child(6){
width: 90px;
}
th:nth-child(7),
td:nth-child(7){
width: 90px;
}
th:nth-child(8),
td:nth-child(8){
width: 90px;
}
th:nth-child(9),
td:nth-child(9){
width: 100px;
}
th:nth-child(10),
td:nth-child(10){
width: 100px;
}