原生html+css 实现table表格超出宽度后,table的第一列和第二列固定左侧,其余列左右滚动

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>products compare</title>
<style>
.product-compare-detail-specs {
    max-width: 1500px;
/* dynamic allocation*/
margin: 50px auto 50px auto;
}
.product-compare-detail-specs .section-header__title {
    color: #2f2725;
    font-family: MetropolisBold;
    font-size: 36px;
}
.product-compare-detail-specs .section-header_sub {
    color: #464a4b;
    font-size: 18px;
    line-height: 1.5; /* 150% */
}

.table-container {
    overflow-x: auto;
}

.product-compare-detail-specs table {
    border-collapse: collapse;
    width: 100%;
    font-size: 16px;
}

.product-compare-detail-specs th,
.product-compare-detail-specs td {
    padding: 19px 8px;
}
.detail-compare-table tr {
    border-bottom: 1px solid #e0e0e0;
}

.product-compare-detail-specs th:nth-child(1),
.product-compare-detail-specs td:nth-child(1),
.product-compare-detail-specs th:nth-child(2),
.product-compare-detail-specs td:nth-child(2) {
    position: sticky;
    z-index: 1;
}

.product-compare-detail-specs th:nth-child(1),
.product-compare-detail-specs td:nth-child(1),
.product-compare-detail-specs th:nth-child(2) {
    background-color: #ffffff;
}
.product-compare-detail-specs td:nth-child(2) {
    background-color: #edf9ff;
}

.product-compare-detail-specs th:nth-child(1),
.product-compare-detail-specs td:nth-child(1) {
    left: 0;
    width: 230px;
    min-width: 230px;
    font-family: MetropolisBold;
}

.product-compare-detail-specs th:nth-child(2),
.product-compare-detail-specs td:nth-child(2) {
    left: 230px;
    width: 230px;
    min-width: 230px;
}

.product-compare-detail-specs th:nth-child(n + 3),
.product-compare-detail-specs td:nth-child(n + 3) {
    width: 230px;
    min-width: 230px;
}

.compare-detail-container {
    margin-top: 40px;
}
.compare-detail-container .table-header {
    display: flex;
    flex-direction: column;
    justify-content: center;
    border-bottom: 0!important;
}
.table-header .product-image {
    width: 130px;
    aspect-ratio: 1 / 1;
    object-fit: cover;
}
.meta-field-item {
    width: 100%;
    word-break: break-word;
    color: #2F2725;
}
.product-compare-detail-specs .product-title {
    text-align: left;
    font-family: MetropolisBold;
    color: #2f2725;
    transition: all 0.4s;
}
.table-header:hover .product-title {
    color: #158cc5;
}


</style>
</head>
<body>
<div class="product-compare-detail-specs test revamp-section">
    <div class="table-container compare-detail-container">
        <table id="myTable" class="detail-compare-table">
            <thead>
            <tr>
                <th></th>
                <th class="main-product-header">
                    <a href="" class="table-header">
                        <img
                            src="../../images/product-compare-1.png"
                            alt=""
                            class="product-image"
                        >
                        <span class="product-title"> product.title </span>
                    </a>
                </th>
                <!-- 对比产品头部 最多支持4个 -->
                <th>
                    <a href="" class="table-header">
                        <img src="../../images/product-compare-1.png" alt="" class="product-image">
                        <span class="product-title">compareItem.title</span>
                    </a>
                </th>
                <th>
                    <a href="" class="table-header">
                        <img src="../../images/product-compare-1.png" alt="" class="product-image">
                        <span class="product-title">compareItem.title </span>
                    </a>
                </th>
                <th>
                    <a href="" class="table-header">
                        <img src="../../images/product-compare-1.png" alt="" class="product-image">
                        <span class="product-title">compareItem.title </span>
                    </a>
                </th>
                <th>
                    <a href="" class="table-header">
                        <img src="../../images/product-compare-1.png" alt="" class="product-image">
                        <span class="product-title">compareItem.title</span>
                    </a>
                </th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td><div class="meta-field-item">modell</div></td>
                <td>
                    <span class="meta-field-item">modell</span>
                </td>
                
                <td>
                    <span class="meta-field-item">modell</span>
                </td>
                <td>
                    <span class="meta-field-item">modell</span>
                </td>
                <td>
                    <span class="meta-field-item">modell</span>
                </td>
                <td>
                    <span class="meta-field-item">modell</span>
                </td>
            </tr>
            <tr>
                <td><div class="meta-field-item">Working Area</div></td>
                <td>
                    <span class="meta-field-item">510mm x 300 mm</span>
                </td>
    
                <td>
                    <span class="meta-field-item">510mm x 300 mm</span>
                </td>
                <td>
                    <span class="meta-field-item">510mm x 300 mm</span>
                </td>
                <td>
                    <span class="meta-field-item">510mm x 300 mm</span>
                </td>
                <td>
                    <span class="meta-field-item">510mm x 300 mm</span>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现使用原生table 固定第一列,并且 table 超出可以滚动: 1. 在 table 外层包裹一个 div,设置 div 的宽度和高度,并且添加 overflow 属性为 auto。 2. 将 table 分成两个部分,第一部分只包含第一列,第二部分包含剩余的。 3. 使用 CSS 设置第一部分的宽度和样式,使其能够固定左侧;设置第二部分的样式,使其能够与第一部分对齐。 下面是一个简单的例子代码,可以实现 table 固定第一列,并且 table 超出可以滚动: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>固定第一列表格</title> <style> .table-wrapper { width: 300px; height: 200px; overflow: auto; } .table-wrapper table { border-collapse: collapse; width: 100%; } .table-wrapper td, .table-wrapper th { padding: 10px; border: 1px solid #ccc; } .table-wrapper td:first-child, .table-wrapper th:first-child { position: sticky; left: 0; z-index: 1; background-color: #fff; } .table-wrapper td:not(:first-child), .table-wrapper th:not(:first-child) { background-color: #f5f5f5; } </style> </head> <body> <div class="table-wrapper"> <table> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>地址</th> </tr> </thead> <tbody> <tr> <td>张三</td> <td>20</td> <td>男</td> <td>北京</td> </tr> <tr> <td>李四</td> <td>25</td> <td>女</td> <td>上海</td> </tr> <tr> <td>王五</td> <td>30</td> <td>男</td> <td>深圳</td> </tr> <tr> <td>赵六</td> <td>35</td> <td>女</td> <td>广州</td> </tr> </tbody> </table> </div> </body> </html> ``` 这段代码会生成一个包含固定第一列table,当 table 内容超出 div 的宽度和高度时,div 会出现滚动条。你可以根据自己的需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值