表格制作css
基础表格样式
使用CSS可以轻松地为HTML表格添加样式。以下是一个基础表格的HTML和CSS示例:
<table class="basic-table">
<thead>
<tr>
<th>标题1</th>
<th>标题2</th>
<th>标题3</th>
</tr>
</thead>
<tbody>
<tr>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
</tbody>
</table>
.basic-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}
.basic-table th, .basic-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.basic-table th {
background-color: #f2f2f2;
font-weight: bold;
}
.basic-table tr:nth-child(even) {
background-color: #f9f9f9;
}
.basic-table tr:hover {
background-color: #f1f1f1;
}
响应式表格设计
对于移动设备,表格可能需要特殊处理:
@media screen and (max-width: 600px) {
.responsive-table {
border: 0;
}
.responsive-table thead {
display: none;
}
.responsive-table tr {
border-bottom: 1px solid #ddd;
display: block;
margin-bottom: 10px;
}
.responsive-table td {
border-bottom: 1px solid #eee;
display: block;
text-align: right;
}
.responsive-table td::before {
content: attr(data-label);
float: left;
font-weight: bold;
}
}
斑马条纹效果
为表格添加交替行颜色:
.zebra-table tr:nth-child(odd) {
background-color: #f9f9f9;
}
.zebra-table tr:nth-child(even) {
background-color: #ffffff;
}
表格边框样式
自定义表格边框:
.custom-border-table {
border: 2px solid #333;
}
.custom-border-table th, .custom-border-table td {
border: 1px solid #666;
}
.custom-border-table th {
border-bottom: 2px solid #333;
}
表格悬停效果
增强用户交互体验:
.hover-effect-table tr {
transition: background-color 0.3s ease;
}
.hover-effect-table tr:hover {
background-color: #e6f7ff;
cursor: pointer;
}
固定表头表格
当表格内容较长时固定表头:
.fixed-header-table {
position: relative;
height: 300px;
overflow: auto;
}
.fixed-header-table thead th {
position: sticky;
top: 0;
background-color: #f8f9fa;
z-index: 1;
}
表格单元格对齐
控制单元格内容对齐方式:
.align-center-table td {
text-align: center;
vertical-align: middle;
}
.align-right-table td {
text-align: right;
}
表格间距和填充
调整表格内部间距:
.spacious-table th, .spacious-table td {
padding: 15px 20px;
}
.compact-table th, .compact-table td {
padding: 4px 8px;
}
表格颜色主题
创建彩色表格:
.colorful-table thead {
background-color: #3498db;
color: white;
}
.colorful-table tbody tr:nth-child(odd) {
background-color: #e3f2fd;
}






