表格属性用于控制表格的外观,包括边框、间距、对齐和布局。掌握表格属性对于创建美观的表格非常重要。
border-collapse 属性用于设置表格边框的合并方式。
/* 合并边框 */
.collapse {
border-collapse: collapse;
}
/* 分离边框(默认) */
.separate {
border-collapse: separate;
}
/* 合并边框的表格 */
.table-collapse {
border-collapse: collapse;
width: 100%;
}
.table-collapse th,
.table-collapse td {
border: 1px solid #ddd;
padding: 10px;
}
/* 分离边框的表格 */
.table-separate {
border-collapse: separate;
border-spacing: 5px;
}
.table-separate th,
.table-separate td {
border: 1px solid #ddd;
padding: 10px;
}
border-spacing 属性用于设置相邻单元格边框之间的距离。
/* 相同间距 */
.spacing {
border-spacing: 10px;
}
/* 不同间距 */
.different-spacing {
border-spacing: 10px 5px;
}
/* 无间距 */
.no-spacing {
border-spacing: 0;
}
/* 带间距的表格 */
.table-spacing {
border-collapse: separate;
border-spacing: 10px;
width: 100%;
}
.table-spacing th,
.table-spacing td {
border: 1px solid #ddd;
padding: 10px;
background: white;
}
caption-side 属性用于设置表格标题的位置。
/* 标题在表格上方(默认) */
.top {
caption-side: top;
}
/* 标题在表格下方 */
.bottom {
caption-side: bottom;
}
/* 表格标题样式 */
.table {
border-collapse: collapse;
width: 100%;
caption-side: bottom;
}
.table caption {
caption-side: bottom;
padding: 10px;
font-weight: bold;
text-align: center;
}
empty-cells 属性用于设置空单元格是否显示边框和背景。
/* 显示空单元格边框(默认) */
.show {
empty-cells: show;
}
/* 隐藏空单元格边框 */
.hide {
empty-cells: hide;
}
/* 隐藏空单元格边框 */
.table {
border-collapse: separate;
empty-cells: hide;
}
.table td {
border: 1px solid #ddd;
padding: 10px;
}
table-layout 属性用于设置表格布局算法。
/* 自动布局(默认) */
.auto {
table-layout: auto;
}
/* 固定布局 */
.fixed {
table-layout: fixed;
}
/* 固定布局表格 */
.table {
table-layout: fixed;
width: 100%;
}
.table td {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* 表格边框 */
.table {
border: 1px solid #ddd;
}
.table th,
.table td {
border: 1px solid #ddd;
}
/* 去除表格边框 */
.table {
border: none;
}
.table th,
.table td {
border: none;
}
/* 单元格内边距 */
.table th,
.table td {
padding: 10px 15px;
}
/* 表格外边距 */
.table {
margin: 20px 0;
}
/* 左对齐 */
.text-left {
text-align: left;
}
/* 居中对齐 */
.text-center {
text-align: center;
}
/* 右对齐 */
.text-right {
text-align: right;
}
/* 顶部对齐 */
.vertical-top {
vertical-align: top;
}
/* 居中对齐(默认) */
.vertical-middle {
vertical-align: middle;
}
/* 底部对齐 */
.vertical-bottom {
vertical-align: bottom;
}
/* 表格背景 */
.table {
background: #f5f5f5;
}
/* 表头背景 */
.table th {
background: #3498db;
color: white;
}
/* 斑马纹背景 */
.table tr:nth-child(even) {
background: #f9f9f9;
}
/* 基本表格样式 */
.table {
border-collapse: collapse;
width: 100%;
margin: 20px 0;
}
.table th,
.table td {
border: 1px solid #ddd;
padding: 12px 15px;
text-align: left;
}
.table th {
background: #3498db;
color: white;
font-weight: bold;
}
.table tr:nth-child(even) {
background: #f9f9f9;
}
.table tr:hover {
background: #f5f5f5;
}
/* 斑马纹表格 */
.striped-table {
border-collapse: collapse;
width: 100%;
}
.striped-table th,
.striped-table td {
border: 1px solid #ddd;
padding: 12px 15px;
}
.striped-table th {
background: #2c3e50;
color: white;
font-weight: bold;
}
.striped-table tr:nth-child(even) {
background: #ecf0f1;
}
.striped-table tr:hover {
background: #bdc3c7;
}
/* 响应式表格 */
.responsive-table {
border-collapse: collapse;
width: 100%;
overflow-x: auto;
}
.responsive-table th,
.responsive-table td {
border: 1px solid #ddd;
padding: 10px;
min-width: 120px;
}
@media (max-width: 768px) {
.responsive-table {
font-size: 14px;
}
.responsive-table th,
.responsive-table td {
padding: 8px;
}
}
/* 圆角表格 */
.rounded-table {
border-collapse: separate;
border-spacing: 0;
width: 100%;
border-radius: 8px;
overflow: hidden;
}
.rounded-table th,
.rounded-table td {
border: 1px solid #ddd;
padding: 12px 15px;
}
.rounded-table th {
background: #3498db;
color: white;
font-weight: bold;
}
.rounded-table th:first-child {
border-radius: 8px 0 0 0;
}
.rounded-table th:last-child {
border-radius: 0 8px 0 0;
}
.rounded-table tr:last-child td:first-child {
border-radius: 0 0 0 8px;
}
.rounded-table tr:last-child td:last-child {
border-radius: 0 0 8px 0;
}
/* 紧凑表格 */
.compact-table {
border-collapse: collapse;
width: 100%;
}
.compact-table th,
.compact-table td {
border: 1px solid #ddd;
padding: 5px 10px;
font-size: 14px;
}
.compact-table th {
background: #2c3e50;
color: white;
font-weight: bold;
}
/* 推荐 - 合并边框 */
.table {
border-collapse: collapse;
}
/* 不推荐 - 分离边框(除非有特殊需求) */
.table {
border-collapse: separate;
border-spacing: 5px;
}
/* 推荐 - 合理的内边距 */
.table th,
.table td {
padding: 12px 15px;
}
/* 不推荐 - 过小的内边距 */
.table th,
.table td {
padding: 2px 5px;
/* 内容过于拥挤 */
}
/* 推荐 - 使用斑马纹 */
.table tr:nth-child(even) {
background: #f9f9f9;
}
/* 推荐 - 添加悬停效果 */
.table tr:hover {
background: #f5f5f5;
}
表格属性用于控制表格的外观:
记住以下几点: