css实现表格中最后一列固定

使用position: sticky;

这种方法适用于大多数现代浏览器,特别是当你想要固定列在滚动时。

html 复制代码
<style>
  .table-container {
    overflow-x: auto;
    width: 100%;
  }
  .table-container table {
    width: max-content;
    border-collapse: collapse;
  }
  .table-container th, .table-container td {
    border: 1px solid black;
    padding: 8px;
    text-align: left;
  }
  .table-container th:last-child, .table-container td:last-child {
    position: sticky;
    right: 0;
    background-color: white; /* 确保背景颜色与表格匹配 */
    z-index: 100; /* 确保在最上层 */
  }
</style>

<div class="table-container">
  <table>
    <thead>
      <tr>
        <th>列1</th>
        <th>列2</th>
        <th>列3</th>
        <!-- 更多列 -->
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>数据1</td>
        <td>数据2</td>
        <td>数据3</td>
        <!-- 更多数据 -->
      </tr>
      <!-- 更多行 -->
    </tbody>
  </table>
</div>
相关推荐
o***Z44817 分钟前
前端性能优化案例
前端
张拭心21 分钟前
前端没有实际的必要了?结合今年工作内容,谈谈我的看法
前端·ai编程
姜太小白30 分钟前
【前端】CSS媒体查询响应式设计详解:@media (max-width: 600px) {……}
前端·css·媒体
weixin_4111918437 分钟前
flutter中WebView的使用及JavaScript桥接的问题记录
javascript·flutter
HIT_Weston41 分钟前
39、【Ubuntu】【远程开发】拉出内网 Web 服务:构建静态网页(二)
linux·前端·ubuntu
百***060141 分钟前
SpringMVC 请求参数接收
前端·javascript·算法
天外天-亮1 小时前
Vue + excel下载 + 水印
前端·vue.js·excel
起个名字逛街玩1 小时前
前端正在走向“工程系统化”:从页面开发到复杂产品架构的深度进化
前端·架构
用户47949283569151 小时前
React 渲染两次:是 Bug 还是 Feature?聊聊严格模式的“良苦用心”
前端·react.js·前端框架
用户47949283569151 小时前
Code Review 惊魂:同事的“优雅”重构,差点让管理员全部掉线
javascript