使用Bootstrap4创建可滚动的表格主体

5
使用Bootstrap-4时,我无法为固定表头的表格主体应用滚动条。我使用min-height和overflow来应用表格主体的滚动条。Bootstrap4不支持表格主体上的滚动条吗?下面的代码片段更清楚地解释了这个问题。
我哪里出错了吗?

.tbody {
  min-height:10px;
  overflow-y:scroll;
}
 <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  
  
  <div class="container">
          
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody class="tbody">
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@example.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@example.com</td>
      </tr>
    </tbody>
  </table>
</div>


你想要滚动条水平显示吗? - Sumit Kumar
3个回答

7
在将display:block属性设置为table后,您可以设置高度和宽度。 试一试:
table {
  display:block;
  height : <set your desired height>;
  overflow-y : scroll;
}

1
如果我是 OP,我会将这个标记为答案。 - Maytham Fahmi
1
很不幸,需要使用 display:block,这会使表格在某些情况下看起来很糟糕。我已经尝试过不使用 block 和其他方式,但是这里只有 display: block 是唯一的选择。CSS 真是让人头疼。 - RyanNerd
这将同时滚动标题,所以不行!这不是正确答案。 - undefined

1
我希望这就是你要找的。

.tbody {
  height: 50px !important;
  overflow-y: scroll; 
}
   
.my-tbody {
  height:30px;
  display:block;
  overflow-y:scroll;
  width:100%;
}

tbody {
  width: 100%;
}

tr {
  width: 100%;
}

td {
  width: 33.33%;
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  
  
  <div class="container">
          
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <table class="my-tbody">
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@example.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@example.com</td>
      </tr>
      <div class="cl"></div>
    </table>
  
  </table>
</div>


1
谢谢回复,但如果使用display block,则列会错位。 - coder12349
请看一下这是否有帮助,如果没有,请告诉我需要应用哪些元素的滚动条以及如何应用。谢谢 :) - Sumit Kumar
我已为您添加了另一个答案,请查看。 - Sumit Kumar

-2

或者你可以使用这个。

.tbody {
  height: 50px !important;
  overflow-y: scroll; 
}

.my-tbody {
  height:50px;
  display:block;
  overflow-y:scroll;
  width:100%;
}

tbody {
  width: 100%;
}

tr {
  width: 100%;
}

td {
  width: 33.33%;
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  
  
<div class="container">
          
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <table class="my-tbody">
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@example.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@example.com</td>
      </tr>
      <div class="cl"></div>
    </table>
  
  </table>
</div>


1
列仍然与表头不匹配 - mix3d

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接