html - CSS 表 : Scrollbar for first column and auto width for remaining column

标签 html css html-table

我是 CSS 的初学者。我试图创建一个表,其中第一列的宽度是固定的,并且必须有一个滚动条。对于剩余的列,我希望它们在整个网页上均匀分布。

我可以为第一列创建滚动条,但它出现在每一行中,这不是我想要的。我仍然希望每个第一列都可以滚动,但滚动条应该只在最后一行下方出现一次。

有人可以帮我解决这个问题吗?

#customers {
  font-size: 10px;
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
  table-layout: fixed;
}

#customers td,
#customers th {
  border: 1px solid #ddd;
  padding: 8px;
  overflow-x: auto;
}

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

#customers tr:hover {
  background-color: #ddd;
}

#customers th {
  padding-top: 6px;
  padding-bottom: 6px;
  text-align: left;
  background-color: #4CAF50;
  color: white;
}

.col {
  width: 3%;
}
<table id="customers">
  <colgroup>
    <col width=500px>
    <col width=5% span="11">
  </colgroup>
</table>

enter image description here

最佳答案

这是可能的,但使用了很多技巧:

  • 必须改变表格的方法,使用 divdisplay: table; display: table-row;显示:表格单元格;

  • 所有单元格的空白必须是nowrap;

  • 在两个宏单元中划分主体;

Look like this demo : 这里是新 html 结构逻辑的摘录。

.div-cell.div-container-cell
{
  border: none;
  display: table-cell;
  padding: 0px;
}

.div-fixed-container-cell
{
  white-space: nowrap;
  max-width:200px;
  overflow-x:scroll;
}

.div-table
{
  display: table;
}

.div-row 
{
  display: table-row;
}

.div-cell
{
  display: table-cell;
  border: 2px solid #ddd;
  padding: 5px;
  white-space: nowrap;
}
<div class="div-table">
  <div class="div-container-cell div-fixed-container-cell">
    <div class="div-row">
      <div class="div-cell">Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</div>
    </div>
    <div class="div-row">
      <div class="div-cell">Lorem ipsum ...</div>
    </div>
    <div class="div-row">
      <div class="div-cell">Lorem ipsum ...</div>
    </div>
  </div>

  <div class="div-cell div-container-cell">
    <div class="div-row">
        <div class="div-cell">A1</div>
        <div class="div-cell">B1</div>
        <div class="div-cell">C1</div>
    </div>

    <div class="div-row">
        <div class="div-cell">A2</div>
        <div class="div-cell">B2</div>
        <div class="div-cell">C2</div>
    </div>  
    <div class="div-row">
       <div class="div-cell">A3</div>
       <div class="div-cell">B3</div>
       <div class="div-cell">C3</div>
    </div>  
  </div>
</div>

关于html - CSS 表 : Scrollbar for first column and auto width for remaining column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48779933/

相关文章:

html - 使 div 小于其内容

javascript - 在简短的内联事件中,是否可以从一个范围输入元素滑动两个范围输入元素?

html - 如何制作容器 'Fluid'

css - 一种在移动设备上隐藏标签但显示包含的 div 的方法

html - 使用 wagtail 图像标签的响应图像

javascript - Chrome 扩展不注入(inject) css

CSS,停止悬停导致锐利的动画

html - CSS 表格中第一行和第一列的每个单元格的不同背景颜色

javascript - 使用 jQuery 从二维数组创建表

PHP 限制表中的行数