表头未占据页面的全部宽度

时间:2019-06-04 10:56:21

标签: javascript reactjs

您好,您可以在下面看到我的表格,但是它不会占用页面的整个宽度,我相信它与我的

有关
 <div style={{ overflow: 'auto', height: '250px' }}> 

但是,我需要将表标题保留在页面中,并且不允许其与表主体一起向下滚动

1 个答案:

答案 0 :(得分:1)

此解决方案在chrome中完美运行。这在IE中不起作用。Refer here for more solutions that work with Javascript and jQuery that works for IE

    table thead tr th {
      position: -webkit-sticky; 
      position: sticky;
      background-color: white;
      z-index: 100;
      top:-1px;
    }

table {
  border-collapse: collapse;
  width: 100%;
}

td,
th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}

table thead tr th {
  background-color: white;
  position: sticky;
  z-index: 100;
  top: -1px;
}
<table>
  <thead>
    <tr>
      <th>Company</th>
      <th>Contact</th>
      <th>Country</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Alfreds Futterkiste</td>
      <td>Maria Anders</td>
      <td>Germany</td>
    </tr>
    <tr>
      <td>Centro comercial Moctezuma</td>
      <td>Francisco Chang</td>
      <td>Mexico</td>
    </tr>
    <tr>
      <td>Ernst Handel</td>
      <td>Roland Mendel</td>
      <td>Austria</td>
    </tr>
    <tr>
      <td>Island Trading</td>
      <td>Helen Bennett</td>
      <td>UK</td>
    </tr>
    <tr>
      <td>Laughing Bacchus Winecellars</td>
      <td>Yoshi Tannamuri</td>
      <td>Canada</td>
    </tr>
    <tr>
      <td>Magazzini Alimentari Riuniti</td>
      <td>Giovanni Rovelli</td>
      <td>Italy</td>
    </tr>
    <tr>
      <td>Magazzini Alimentari Riuniti</td>
      <td>Giovanni Rovelli</td>
      <td>Italy</td>
    </tr>
    <tr>
      <td>Magazzini Alimentari Riuniti</td>
      <td>Giovanni Rovelli</td>
      <td>Italy</td>
    </tr>
    <tr>
      <td>Magazzini Alimentari Riuniti</td>
      <td>Giovanni Rovelli</td>
      <td>Italy</td>
    </tr>
  </tbody>
</table>