html,, ,,`
,或者在CSS文件中添加:,“css,table {,
HTML 去除表格外边框的详细方法
在网页开发中,表格(<table>)是一种常用的布局元素,默认情况下,HTML 表格通常会有外边框,这在某些设计场景下可能不符合需求,本文将详细介绍如何在 HTML 中去除表格的外边框,包括多种方法和注意事项。
使用 CSS 去除表格外边框
1 通过 border 属性设置
最直接的方法是使用 CSS 的 border 属性来控制表格的边框,要去除表格的外边框,可以将表格的 border 属性设置为 0 或 none。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">去除表格外边框</title>
<style>
table {
border: none; / 去除表格外边框 /
width: 100%;
border-collapse: collapse; / 合并边框 /
}
th, td {
border: 1px solid #000; / 保留单元格内边框 /
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr>
<td>张三</td>
<td>25</td>
<td>男</td>
</tr>
<tr>
<td>李四</td>
<td>22</td>
<td>女</td>
</tr>
</tbody>
</table>
</body>
</html>
解释:
border: none;:去除表格的外边框。border-collapse: collapse;:合并单元格边框,避免出现双重边框。th, td { border: 1px solid #000; }:保留单元格的内边框,使表格内容更清晰。
2 使用 border-spacing 和 border-collapse
除了直接设置 border,还可以通过 border-spacing 和 border-collapse 属性来控制表格的边框显示。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">去除表格外边框</title>
<style>
table {
width: 100%;
border-spacing: 0; / 去除单元格间距 /
border-collapse: collapse; / 合并边框 /
}
th, td {
border: none; / 去除单元格边框 /
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr>
<td>张三</td>
<td>25</td>
<td>男</td>
</tr>
<tr>
<td>李四</td>
<td>22</td>
<td>女</td>
</tr>
</tbody>
</table>
</body>
</html>
解释:
border-spacing: 0;:去除单元格之间的间距。border-collapse: collapse;:合并单元格边框,使表格看起来更紧凑。th, td { border: none; }:去除单元格的边框。
使用外部 CSS 文件
在实际项目中,通常会将 CSS 样式写在外部文件中,以便更好地管理和维护,以下是一个示例:

1 创建外部 CSS 文件 styles.css
/ styles.css /
table {
border: none; / 去除表格外边框 /
width: 100%;
border-collapse: collapse; / 合并边框 /
}
th, td {
border: 1px solid #000; / 保留单元格内边框 /
padding: 10px;
text-align: center;
}
2 在 HTML 文件中引入外部 CSS 文件
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">去除表格外边框</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr>
<td>张三</td>
<td>25</td>
<td>男</td>
</tr>
<tr>
<td>李四</td>
<td>22</td>
<td>女</td>
</tr>
</tbody>
</table>
</body>
</html>
优点:
- 样式与结构分离,便于维护。
- 多个页面可以共享同一个 CSS 文件,减少代码重复。
使用内联样式(不推荐)
虽然可以使用内联样式直接在 HTML 标签中设置样式,但这种方法不推荐,因为不利于样式的统一管理和维护。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">去除表格外边框</title>
</head>
<body>
<table style="border: none; width: 100%; border-collapse: collapse;">
<thead>
<tr>
<th style="border: 1px solid #000; padding: 10px; text-align: center;">姓名</th>
<th style="border: 1px solid #000; padding: 10px; text-align: center;">年龄</th>
<th style="border: 1px solid #000; padding: 10px; text-align: center;">性别</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid #000; padding: 10px; text-align: center;">张三</td>
<td style="border: 1px solid #000; padding: 10px; text-align: center;">25</td>
<td style="border: 1px solid #000; padding: 10px; text-align: center;">男</td>
</tr>
<tr>
<td style="border: 1px solid #000; padding: 10px; text-align: center;">李四</td>
<td style="border: 1px solid #000; padding: 10px; text-align: center;">22</td>
<td style="border: 1px solid #000; padding: 10px; text-align: center;">女</td>
</tr>
</tbody>
</table>
</body>
</html>
缺点:
- 样式与结构混杂,难以维护。
- 无法复用样式,导致代码冗余。
使用框架或库(如 Bootstrap)
在使用前端框架或库时,去除表格外边框的方法可能会有所不同,以下以 Bootstrap 为例:
1 引入 Bootstrap CSS 文件
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">去除表格外边框 Bootstrap 示例</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
.table-no-border {
border: none; / 去除表格外边框 /
}
.table-no-border th, .table-no-border td {
border: 1px solid #000; / 保留单元格内边框 /
}
</style>
</head>
<body>
<div class="container">
<table class="table table-no-border">
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr>
<td>张三</td>
<td>25</td>
<td>男</td>
</tr>
<tr>
<td>李四</td>
<td>22</td>
<td>女</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
解释:

.table-no-border:自定义类,用于去除表格外边框。.table-no-border th, .table-no-border td:保留单元格内边框。
优点:
- 利用框架的样式,快速实现响应式设计。
- 自定义类名,灵活控制样式。
注意事项
1 兼容性问题
不同浏览器对 CSS 的支持程度可能有所不同,建议使用标准的 CSS 属性,并确保在主流浏览器中进行测试。
2 响应式设计
在去除表格外边框的同时,还需要考虑表格在不同设备上的显示效果,可以使用媒体查询(@media)来调整表格的样式。
/ styles.css /
table {
border: none; / 去除表格外边框 /
width: 100%;
border-collapse: collapse; / 合并边框 /
}
th, td {
border: 1px solid #000; / 保留单元格内边框 /
padding: 10px;
text-align: center;
}
@media (max-width: 768px) {
table {
font-size: 14px; / 调整字体大小 /
}
}
3 语义化 HTML
在去除表格外边框时,应确保表格的使用符合语义化 HTML 的原则,表格应主要用于展示表格数据,而不是用于布局,如果用于布局,建议使用 CSS 的 Flexbox 或 Grid 布局。

去除 HTML 表格的外边框可以通过多种方法实现,包括直接设置 border 属性、使用 border-spacing 和 border-collapse、引入外部 CSS 文件等,在实际项目中,应根据具体需求选择合适的方法,并注意样式的维护和兼容性,通过合理的 CSS 设计,可以实现美观且功能完善的表格布局。
FAQs
Q1: 如何只去除表格的外边框而保留单元格的内边框?
A1: 要只去除表格的外边框而保留单元格的内边框,可以针对表格元素设置 border: none;,然后针对 th 和 td 元素设置内边框。
table {
border: none; / 去除表格外边框 /
width: 100%;
border-collapse: collapse; / 合并边框 /
}
th, td {
border: 1px solid #000; / 保留单元格内边框 /
padding: 10px;
text-align: center;
}
Q2: 使用 Bootstrap 时如何去除表格的外边框?
A2: 在使用 Bootstrap 时,可以通过自定义 CSS 类来去除表格的外边框。
.table-no-border {
border: none; / 去除表格外边框 /
}
.table-no-border th, .table-no-border td {
border: 1px solid #000; / 保留单元格内边框 /
}
然后在 HTML 中应用该类:
<table class="table table-no-border">
<!-表格内容 -->
