CSS用于设置HTML表格.
border:用于在表格中指定边框。
语法如下:
border: table_width table_color;
示例1:
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: left;
}
h1 {
color: green;
}
table, th, td {
/* Styling the border. */
border: 1.5px solid blue;
}
</style>
</head>
<body>
<h1>lsbin</h1>
<h2>Add border to table:</h2>
<table>
<tr>
<th>Roll No.</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
</body>
</html>
输出如下:

border-collapse:border-collapse属性告诉我们浏览器是否应该控制彼此接触的相邻边框的外观, 或者每个单元格是否应保持其样式。
语法如下:
border-collapse: collapse/separate;
示例2:
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: left;
}
h1 {
color: green;
}
table.one {
/* Styling border collapse for table one. */
border-collapse: collapse;
}
table.two {
/* Styling border separate for table two. */
border-collapse: separate;
}
table, td, th {
border: 1.5px solid blue;
}
</style>
</head>
<body>
<h1>lsbin</h1>
<h2>borders collapsed:</h2>
<table class = "one">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
<br>
<br>
<h2>borders separated:</h2>
<table class = "two">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
</body>
</html>
输出如下:

边框间距:此属性指定相邻单元格的边界之间的间隔。
语法如下:
border-spacing: value;
示例3:
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: left;
}
h1 {
color: green;
}
table.one {
border-collapse: separate;
/* Styling the border-spacing
between adjacent cells. */
border-spacing: 10px;
}
table.two {
border-collapse: separate;
/* Styling the border-spacing
between adjacent cells. */
border-spacing: 10px 30px;
}
table, td, th {
border: 1.5px solid blue;
}
</style>
</head>
<body>
<h1>lsbin</h1>
<h2>border spacing:</h2>
<table class = "one">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
<br>
<br>
<h2>border spacing:</h2>
<table class = "two">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
</body>
</html>
输出如下:

标题侧:Caption side属性用于控制表中标题的位置。默认情况下, 标题位于表格上方。
语法如下:
caption-side:top/bottom;
示例4:
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: left;
}
h1 {
color: green;
}
table.one {
border-collapse: separate;
border-spacing: 10px;
/* Controlling the placement of caption. */
caption-side: top;
}
table.two {
border-collapse: separate;
border-spacing: 10px;
/* Controlling the placement of caption. */
caption-side: bottom;
}
table, td, th {
border: 1.5px solid blue;
}
</style>
</head>
<body>
<h1>lsbin</h1>
<h2>Caption on top:</h2>
<table class = "one">
<caption>Caption at the top of the table.</caption>
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
<br>
<br>
<h2>Caption at bottom:</h2>
<table class = "two">
<caption>
Caption at the bottom of the table
</caption>
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
</body>
</html>
输出如下:

空单元格:此属性指定是否在表格中的空白单元格上显示边框和背景。
语法如下:
empty-cells:show/hide;
示例5:
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: left;
}
h1 {
color: green;
}
table.one {
border-collapse: separate;
border-spacing: 10px;
/* Hiding empty cells border */
empty-cells: hide;
}
table.two {
border-collapse: separate;
border-spacing: 10px;
/* Display empty cells border */
empty-cells: show;
}
table, td, th {
border: 1.5px solid blue;
}
</style>
</head>
<body>
<h1>lsbin</h1>
<h2>empty cells hide:</h2>
<table class = "one">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
<br>
<br>
<h2>empty cells show:</h2>
<table class = "two">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
</body>
</html>
输出如下:

表格布局:表布局属性用于设置表的布局算法。
语法如下:
table-layout:auto/fixed;
示例6:
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: left;
}
h1 {
color: green;
}
table.one {
width: 80px border-collapse: separate;
border-spacing: 10px;
/* Layout of table is auto. */
table-layout: auto;
}
table.two {
width: 80px border-collapse: separate;
border-spacing: 10px;
/* Layout of table is fixed. */
table-layout: fixed;
}
table, td, th {
border: 1.5px solid blue;
width: 80px;
}
</style>
</head>
<body>
<h1>lsbin</h1>
<h2>auto table layout:</h2>
<table class = "one">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C_D_E_F_G_H_I_J_K_L_M_N_O_P</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
<br>
<br>
<h2>fixed table layout:</h2>
<table class = "two">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C_D_E_F_G_H_I_J_K_L_M_N_O_P</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
</body>
</html>
输出如下:

支持的浏览器:支持的浏览器桌子
谷歌浏览器
Edge
火狐浏览器
Opera
苹果浏览器