表格大于带水平滚动的容器

时间:2016-09-08 16:52:41

标签: html css

如何创建一个比其父容器大的表来获得水平滚动

<div style="background: pink; width: 200px">
  <table style="background: rgba(2,2,2, 0.2); width: 500px">
    <thead>
      <th>th</th>
      <th>th</th>
    </thead>
    <tbody>
      <td>cell</td>
      <td>cell</td>
    </tbody>
  </table>
</div>

我需要带有水平滚动的表格。

3 个答案:

答案 0 :(得分:1)

尝试将overflow-x: autooverflow-x: scroll添加到父级。

&#13;
&#13;
<div style="background: pink; width: 200px; overflow-x: auto;">
  <table style="background: rgba(2,2,2, 0.2); width: 500px">
    <thead>
      <th>th</th>
      <th>th</th>
    </thead>
    <tbody>
      <td>cell</td>
      <td>cell</td>
    </tbody>
  </table>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

&#13;
&#13;
<div style="background: pink; width: 200px; overflow: scroll">
  <table style="background: rgba(2,2,2, 0.2); width: 500px">
    <thead>
      <th>th</th>
      <th>th</th>
    </thead>
    <tbody>
      <td>cell</td>
      <td>cell</td>
    </tbody>
  </table>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

在父元素上使用overflow: scroll;

&#13;
&#13;
.table-container {
  background: pink; 
  width: 200px;
  overflow-x: scroll;
}
.table {
  background: rgba(2,2,2, 0.2); 
  width: 500px;
}
&#13;
<div class="table-container">
  <table class="table">
    <thead>
      <th>th</th>
      <th>th</th>
    </thead>
    <tbody>
      <td>cell</td>
      <td>cell</td>
    </tbody>
  </table>
</div>
&#13;
&#13;
&#13;