网页表格制作css代码
基础表格样式
以下是一个基础的HTML表格结构,配合CSS样式使其更美观:
<table class="basic-table">
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>职业</th>
</tr>
</thead>
<tbody>
<tr>
<td>张三</td>
<td>28</td>
<td>设计师</td>
</tr>
<tr>
<td>李四</td>
<td>32</td>
<td>工程师</td>
</tr>
</tbody>
</table>
.basic-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
font-family: Arial, sans-serif;
}
.basic-table th,
.basic-table td {
padding: 12px 15px;
text-align: left;
border-bottom: 1px solid #ddd;
}
.basic-table th {
background-color: #f2f2f2;
font-weight: bold;
}
.basic-table tr:hover {
background-color: #f5f5f5;
}
斑马条纹表格
添加交替行颜色提高可读性:
.striped-table tr:nth-child(even) {
background-color: #f9f9f9;
}
响应式表格
在小屏幕上显示水平滚动条:
.responsive-table {
overflow-x: auto;
}
圆角边框表格
为表格添加现代感的圆角:

.rounded-table {
border-radius: 8px;
overflow: hidden;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.rounded-table th:first-child {
border-top-left-radius: 8px;
}
.rounded-table th:last-child {
border-top-right-radius: 8px;
}
悬停高亮效果
增强交互体验:
.highlight-table tr:hover td {
background-color: #e6f7ff;
color: #1890ff;
cursor: pointer;
}
固定表头表格
长表格固定表头:

.fixed-header {
position: relative;
max-height: 400px;
overflow-y: auto;
}
.fixed-header thead th {
position: sticky;
top: 0;
z-index: 10;
background-color: #f8f9fa;
}
单元格对齐方式
控制不同列的对齐方式:
.align-right {
text-align: right;
}
.align-center {
text-align: center;
}
.align-left {
text-align: left;
}
边框样式自定义
完全控制表格边框:
.custom-border {
border: 2px solid #333;
}
.custom-border th,
.custom-border td {
border: 1px solid #ccc;
}
紧凑型表格
减少内边距创建紧凑布局:
.compact-table th,
.compact-table td {
padding: 6px 8px;
}
这些CSS代码片段可以根据实际需求组合使用或单独应用,通过调整颜色、间距和边框等属性,可以创建各种风格的网页表格。






