html - 如何卡住表格的最后一行?

标签 html css

我能够像 MS Word 中的卡住 Pane 一样卡住 HTML 表格的第一行,但不确定如何对表格中的最后一行执行此操作。

我有这个代码:

.myTable {
  overflow-y: scroll;
  height: 650px;
  display: block;
}

.table-header-col {
  position: sticky;
  top: 0px;
  background-color: #f5f6f8;
}
<table class="myTable">
  <thead>
    <tr style="height: 55px; margin: 0px;">
      <th class="table-header-col"></th>
      <th class="table-header-col">Description </th>
      <th class="table-header-col">Amount</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>&nbsp;</td>
      <td>Bananas</td>
      <td>2.00</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>Apples</td>
      <td>3.00</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>Total:</td>
      <td>5.00</td>
    </tr>
  </tbody>
</table>

我在这里仅包含两行作为示例,但如果我有 50 行,用户将必须滚动才能到达包含总计的最后一行。是否可以像我上面卡住标题行的方式那样卡住该行?

最佳答案

你想要这样的东西吗:)

.myTable {
   overflow-y: scroll;
   height: 650px;
   display: block;
}
table tbody tr:last-child{
   position: sticky;
   top: 0;
   background-color: #f5f6f8;
}
<table class="myTable">
  <thead>
    <tr style="height: 55px; margin: 0px;">
      <th class="table-header-col"></th>
      <th class="table-header-col">Description </th>
      <th class="table-header-col">Amount</th>
    </tr>
 </thead>
 <tbody>
    <tr>
       <td>&nbsp;</td>
       <td>Bananas</td>
       <td>2.00</td>      
   </tr>
   <tr>
      <td>&nbsp;</td>
      <td>Apples</td>
      <td>3.00</td>
   </tr>
   <tr>
      <td>&nbsp;</td>
      <td>Total:</td>
      <td>5.00</td>
  </tr>
     <tr>
      <td>&nbsp;</td>
      <td>Total:</td>
      <td>5.00</td>
  </tr>
     <tr>
      <td>&nbsp;</td>
      <td>Total:</td>
      <td>5.00</td>
  </tr>
     <tr>
      <td>&nbsp;</td>
      <td>Total:</td>
      <td>5.00</td>
  </tr>   
  <tr>
      <td>&nbsp;</td>
      <td>Total:</td>
      <td>5.00</td>
  </tr>   
  <tr>
      <td>&nbsp;</td>
      <td>Total:</td>
      <td>5.00</td>
  </tr>   
  <tr>
      <td>&nbsp;</td>
      <td>Total:</td>
      <td>5.00</td>
  </tr>   
  <tr>
      <td>&nbsp;</td>
      <td>Total:</td>
      <td>5.00</td>
  </tr>   
  <tr>
      <td>&nbsp;</td>
      <td>Total:</td>
      <td>5.00</td>
  </tr>   
  <tr>
      <td>&nbsp;</td>
      <td>Total:</td>
      <td>5.00</td>
  </tr>
  </tbody>
</table>

关于html - 如何卡住表格的最后一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64176382/

相关文章:

javascript - <div> 搜索呈现问题中的数字

javascript - 在用户按下以 Bootstrap 模式提交表单后,如何显示成功消息?

asp.net - 如何创建包含图像和文本的链接?

html - 低于 768px 的视口(viewport)两侧的不同填充

html - 应用于特定 DIV 的 CSS 会遗漏一些细节

html - 如何在::after 上实现全高边框(父元素)

css - 如果屏幕足够宽,我怎样才能将我的 Canvas 自动放在我的 table 旁边,或者在彼此下面?

asp.net - 用户控制与服务器控制

javascript 删除元素

javascript - Bootstrap 4 Safari 漏洞