【问题标题】:CSS Table: Scrollbar for first column and auto width for remaining columnCSS表格:第一列的滚动条和剩余列的自动宽度
【发布时间】:2018-02-23 05:32:07
【问题描述】:

我是 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>

【问题讨论】:

    标签: html css html-table


    【解决方案1】:

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

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

    • 所有单元格的空白必须是 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>

    【讨论】:

    • 谢谢大家,我修改了我的帖子。
    • 非常感谢!它完美地工作。无论如何,我怎么知道我什么时候应该使用 table 或 display 来将它作为 table 来展示?
    猜你喜欢
    • 2011-04-03
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多