【问题标题】:Header alignment gets disturbed when scrollbar gets added in tbody在 tbody 中添加滚动条时,标题对齐会受到干扰
【发布时间】:2018-03-07 07:28:00
【问题描述】:

我已经为 header 和 body 定义了两个表。当表格大小超过 6 时,我想要正文的滚动条。但是当滚动条出现时,alignmnet 受到干扰,因为滚动条占用了 16px。其他列被调整大小。

<table id="myProjects" class="table table-hover table-bordered table-striped table-condensed table-scrollable">
      <thead>
        <tr>
            <th>Head 1</th>
            <th>Head 2</th>
            <th>Head 3</th>
            <th>Head 4</th>
            <th>Head 5</th>
        </tr>
      </thead>
    </table>
    <table id="myProjects" class="table table-hover table-bordered table-striped table-condensed table-scrollable">
      <tbody>
        <tr>
            <td>Content 1</td>
            <td>Content 2</td>
            <td>Content 3</td>
            <td>Content 4</td>
            <td>Content 5</td>
        </tr>
      </tbody>
    </table>

have tried all these css classes, but its not working

#myProjects thead th:last-child {
    width: 161.133px; 
}
*
#myProjects tbody tr:last-child td {
    width: 145.133px; 
}
#myProjects{
    width:924.999px;
}

【问题讨论】:

  • 真正的问题是......为什么要定义两个表而不是一个??
  • 你需要定义100%的宽度。
  • Benjoe,我也尝试将宽度设为 100%,但它不起作用。
  • @Facundo 我只想在正文部分显示滚动条。

标签: css


【解决方案1】:

您不需要使用 2 个不同的表格,只需在 tbody 上设置滚动条即可。

tbody {
  display:block; /*so you can give it a height*/
  overflow-y:auto; /*to show scrollbar*/
  height:100px;
}
thead, tr {
   display:table; /*to restore the table behaviour*/
   table-layout:fixed; /*fix width of table, even columns width*/
   width:100%;
}

thead {white-space: nowrap;}

thead tr:after{
/*creates a "cell" at the end of the header to compensate for the scrollbar width*/
  content:""; width:17px; display:table-cell;
}

td, th{ border:1px solid;}
table{border-collapse:collapse; }
*, *::after{box-sizing:border-box;}
<table>
  <thead>
    <tr>
        <th>Head 1</th>
        <th>Head 2</th>
        <th>Head 3</th>
        <th>Head 4</th>
        <th>Head 5</th>
    </tr>
  </thead>
  <tbody>
    <tr><td>1</td> <td>2</td> <td>3</td><td>4</td><td>5</td></tr>
    <tr><td>1</td> <td>2</td> <td>3</td><td>4</td><td>5</td></tr>
    <tr><td>1</td> <td>2</td> <td>3</td><td>4</td><td>5</td></tr>
    <tr><td>1</td> <td>2</td> <td>3</td><td>4</td><td>5</td></tr>
    <tr><td>1</td> <td>2</td> <td>3</td><td>4</td><td>5</td></tr>
    <tr><td>1</td> <td>2</td> <td>3</td><td>4</td><td>5</td></tr>
    <tr><td>1</td> <td>2</td> <td>3</td><td>4</td><td>5</td></tr>
    <tr><td>1</td> <td>2</td> <td>3</td><td>4</td><td>5</td></tr>
  </tbody>
</table>

【讨论】:

  • 那么当行数较少且滚动条不存在时,您再次遇到对齐问题。
猜你喜欢
  • 2014-03-08
  • 2015-09-07
  • 2014-07-29
  • 2015-12-16
  • 2020-08-24
  • 1970-01-01
  • 2020-04-16
  • 1970-01-01
  • 2013-10-03
相关资源
最近更新 更多