为table表格设置合并边框

border-collapse: collapse;


例1:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        table {
            margin: 0 auto;
            width: 300px;
            border:1px solid red;
        }
        th,td {
            text-align: center;
            border: 1px solid green;
        }
    </style>
</head>
<body>
    <table>
        <caption>学生成绩表</caption>
        <thead>
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>成绩</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>张三</td>
                <td>88</td>
            </tr>            <tr>
                <td>1</td>
                <td>李四</td>
                <td>98</td>
            </tr>            <tr>
                <td>3</td>
                <td>王五</td>
                <td>68</td>
            </tr>
        </tbody>
        <tfoot>

        </tfoot>
    </table>
</body>
</html>

效果预览:
这里写图片描述


修改后:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        table {
            margin: 0 auto;
            width: 300px;
            border:1px solid red;
            border-collapse: collapse;/*关键代码*/
        }
        th,td {
            text-align: center;
            border: 1px solid green;
        }
    </style>
</head>
<body>
    <table>
        <caption>学生成绩表</caption>
        <thead>
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>成绩</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>张三</td>
                <td>88</td>
            </tr>            <tr>
                <td>1</td>
                <td>李四</td>
                <td>98</td>
            </tr>            <tr>
                <td>3</td>
                <td>王五</td>
                <td>68</td>
            </tr>
        </tbody>
        <tfoot>

        </tfoot>
    </table>
</body>
</html>

效果预览:
这里写图片描述

CSS中,你可以通过结合`border`属性的不同部分来设置table元素的不同边框样式,创造出独特的视觉效果。以下是一些基本的示例: 1. **内外边框样式**: ```css table { border-collapse: collapse; /* 合并相邻单元格的边框 */ } th, td { border: 1px solid black; /* 设置所有单元格的默认边框 */ } th { border-bottom-width: 2px; /* 表头的下边框更粗 */ } tr:nth-child(even) { /* 奇数行设置不同的颜色 */ background-color: #f2f2f2; } tr:hover { /* 鼠标悬停时的效果 */ background-color: #ccc; } ``` 2. **边框圆角**: ```css table.rounded { border-radius: 5px; } th, td { border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; } ``` 3. **不同列的分隔线**: ```css table td.col1 { border-right: none; } table td.col2 { border-left: none; } ``` 4. **渐变边框** (需引入伪元素如`:before`和`:after`): ```css table { position: relative; } table:before, table:after { content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: -1; box-sizing: border-box; } table:before { background: linear-gradient(to right, transparent 50%, black 50%); transform-origin: bottom left; } table:after { background: linear-gradient(to left, transparent 50%, black 50%); transform-origin: top right; } ``` 以上只是基本示例,实际应用中可以组合更多的样式和技巧来创建更多样化的边框效果。记得在`<table>`标签上加上适当的class或id以便精确选择和控制。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值