【问题标题】:Scrollable table body using bootstrap4使用 bootstrap4 的可滚动表体
【发布时间】:2019-04-16 04:13:40
【问题描述】:

使用 Bootstrap-4 我无法为带有固定标题的表格正文应用滚动。 我正在使用最小高度和溢出来将滚动应用于表格主体。 bootstrap4 不支持在表体上滚动吗? 下面 sn -p 更清楚地解释了这个问题。

我是不是哪里出错了?

.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>

【问题讨论】:

  • 你想要水平滚动吗?

标签: html css bootstrap-4


【解决方案1】:

display:block属性设置为表格后,您可以设置高度和宽度。

试试这个:

table {
  display:block;
  height : <set your desired height>;
  overflow-y : scroll;
}

【讨论】:

  • 如果我是 OP,我会检查这个作为答案?
  • 不幸的是需要display:block,因为这会使表格在某些情况下看起来很糟糕。我已经尝试过没有阻塞和其他人,但display: block 是这里唯一的选择。 CSS 太痛苦了。
【解决方案2】:

我希望这就是你要找的东西

.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>

【讨论】:

  • 感谢您的回复,但是如果使用了显示块,则该列会错位
  • 请看看这是否有帮助,如果没有帮助,请告诉我你想应用什么样的滚动和哪些元素谢谢:)
  • 我已经为你添加了另一个答案,请看一下
【解决方案3】:

或者你可以使用这个。

.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>

【讨论】:

  • 列仍然不匹配表头
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2020-05-16
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
  • 1970-01-01
  • 2020-05-21
相关资源
最近更新 更多