html - 滚动容器内具有固定布局的表格不占用定义的宽度

标签 html css

我需要将具有固定布局和固定列宽的表格放入可滚动容器中。该表及其单个列应占用 colgroup 部分中定义的空间。

这是 html:

<div class="scroll-container">

  <table>
    <colgroup>
      <col width="100px">
      <col width="100px">
      <col width="50px">
    </colgroup>

    <tr>
      <td>width &lt; 100px</td>    
      <td>width &lt; 1 100px</td>   
      <td>width &lt; 150px</td>   
    </tr>
  </table>

</div>

这是 css:

div.scroll-container{
  width : 200px;
  height : 90px;
  background-color : #ddd;
  overflow-x: scroll;
}

table{
  table-layout: fixed;
}

td{
   border : 1px solid black;

}

列的宽度正在缩小,div 容器没有滚动,而我想实现相反的效果。

Js fiddle :https://jsfiddle.net/n337p1ck/1/

最佳答案

你只需要添加:

width:100%;

到你的 table

div.container{
  width : 260px;
  height : 70px;
  background-color : #ccc;
}

div.scroll-container{
  width : 200px;
  height : 90px;
  background-color : #ddd;
  overflow-x: scroll;
}

table{
  table-layout: fixed;
  width:100%;
}

td{
   border : 1px solid black;
   
}
<div class="container">

  <table>
    <colgroup>
      <col width="100px">
      <col width="100px">
      <col width="50px">
    </colgroup>

    <tr>
      <td>width = 100px</td>    
      <td>width = 100px</td>   
      <td>width = 50px</td>   
    </tr>
  </table>

</div>

<hr/>

<div class="scroll-container">

  <table>
    <colgroup>
      <col width="100px">
      <col width="100px">
      <col width="50px">
    </colgroup>

    <tr>
      <td>width &lt; 100px</td>    
      <td>width &lt; 1 100px</td>   
      <td>width &lt; 150px</td>   
    </tr>
  </table>

</div>

关于html - 滚动容器内具有固定布局的表格不占用定义的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45532134/

相关文章:

html - 将一些 CSS 从一个元素交换到另一个元素

html - 是否有一个 JavaScript 函数可以确定您使用的是哪种类型的设备?

jquery - 如何使用 jQuery 在输入的 keyup 上从特定的 div 添加/删除类?

html - 如何将整个网页旋转 90、180 或 270 度?

PHP Imagepng 输出不显示在任何浏览器中

javascript - 如何通过滚动捕捉使拖动滚动工作顺利进行

javascript - 将 nicedit textarea 内容保存到 mysql 数据库中

css - Twitter-Bootstrap 中的响应井?

javascript - 咕噜声 : Adding an alias to a non-project folder

html - 如何更改:before on hover state