css制作表
使用HTML和CSS创建表格
HTML的<table>元素结合CSS可以创建结构清晰、样式美观的表格。以下是一个完整示例:
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
font-family: Arial, sans-serif;
}
th, td {
border: 1px solid #ddd;
padding: 12px;
text-align: left;
}
th {
background-color: #4CAF50;
color: white;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
tr:hover {
background-color: #ddd;
}
caption {
font-weight: bold;
font-size: 1.2em;
margin-bottom: 10px;
}
</style>
</head>
<body>
<table>
<caption>员工信息表</caption>
<tr>
<th>姓名</th>
<th>职位</th>
<th>部门</th>
</tr>
<tr>
<td>张三</td>
<td>前端开发</td>
<td>技术部</td>
</tr>
<tr>
<td>李四</td>
<td>UI设计师</td>
<td>设计部</td>
</tr>
</table>
</body>
</html>
响应式表格设计
对于移动设备友好的响应式表格:
@media screen and (max-width: 600px) {
table {
border: 0;
}
table caption {
font-size: 1.3em;
}
table thead {
display: none;
}
table tr {
border-bottom: 2px solid #ddd;
display: block;
margin-bottom: 10px;
}
table td {
border-bottom: 1px dotted #ccc;
display: block;
text-align: right;
}
table td:before {
content: attr(data-label);
float: left;
font-weight: bold;
}
}
表格美化技巧
添加圆角边框和阴影效果:
table {
border-radius: 10px;
overflow: hidden;
box-shadow: 0 0 20px rgba(0,0,0,0.15);
}
th:first-child {
border-top-left-radius: 10px;
}
th:last-child {
border-top-right-radius: 10px;
}
高级表格样式
斑马条纹和悬停效果增强:
tr:nth-child(odd) {
background-color: #ffffff;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
tr:hover {
transform: scale(1.01);
box-shadow: 0 0 10px rgba(0,0,0,0.1);
transition: all 0.3s ease;
}
表格边框样式
自定义边框样式:
table {
border: 2px solid #4CAF50;
}
th, td {
border-left: 1px dashed #ccc;
border-right: 1px dashed #ccc;
}
tr:last-child td {
border-bottom: none;
}
这些代码示例展示了如何创建基础表格并逐步添加样式增强效果,可根据实际需求组合使用或调整具体参数。







