0

Open below link seems that TH and TD are not in same line. The reason might be because of the scroll bar. How to resolve this issue so that even with scroll TD and TH will in same line?

table {
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    height: 500px; /* this can vary */
}

https://plnkr.co/edit/fcdXKE?p=preview

enter image description here

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3249448
  • 1,369
  • 2
  • 14
  • 34

2 Answers2

1

The scrollbar in the tbody is reducing his width, while theaddoesn't have one.

That's why 20% width in the thead doesn't match the 20% width in the tbody.

You could set a padding on the tr in the thead but i'm not sure it will be cross-browser...

You could also use this for the browsers supporting webkit (CanIUse) :

::-webkit-scrollbar { 
    display: none; 
}

For Firefox, there is no way to custom it with CSS, you will have to deal with jQuery (see thirtydot's answer for more informations). There are librarie making good looking scrollbar : Custom Content Scroller

Community
  • 1
  • 1
Cladiuss
  • 535
  • 10
  • 20
0

Getting the offset of the th, comparing it to the offset of the td, and then setting the margin-left property of the th as the difference of those two should work.

Tested Demo:

// set id on table
var dataTable = document.getElementById("dataTable");
var tableHead = dataTable.getElementsByTagName("thead")[0];
var headRow = tableHead.getElementsByTagName("tr")[0];
var tds = document.querySelectorAll('#dataTable tbody td');
var ths = document.querySelectorAll('#dataTable thead th');
for(i=0;i<tds.length;i++){
    var offset=tds[i].offsetLeft- ths[i].offsetLeft;
    var thToSet = headRow.getElementsByTagName("th")[i];
    thToSet.style.marginLeft = offset + "px";
}
Steven B.
  • 8,962
  • 3
  • 24
  • 45