怎么制作css表格
制作CSS表格的基本方法
使用HTML的<table>标签创建表格结构,配合CSS样式进行美化。以下是一个基础示例:
<table class="styled-table">
<tr>
<th>标题1</th>
<th>标题2</th>
</tr>
<tr>
<td>内容1</td>
<td>内容2</td>
</tr>
</table>
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 25px 0;
font-size: 0.9em;
font-family: sans-serif;
min-width: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
}
.styled-table th,
.styled-table td {
padding: 12px 15px;
}
.styled-table th {
background-color: #009879;
color: #ffffff;
text-align: left;
}
.styled-table tr {
border-bottom: 1px solid #dddddd;
}
.styled-table tr:nth-of-type(even) {
background-color: #f3f3f3;
}
.styled-table tr:last-of-type {
border-bottom: 2px solid #009879;
}
响应式表格设计
对于移动设备,可以添加响应式设计:
@media screen and (max-width: 600px) {
.styled-table {
border: 0;
}
.styled-table thead {
display: none;
}
.styled-table tr {
margin-bottom: 10px;
display: block;
border-bottom: 2px solid #ddd;
}
.styled-table td {
display: block;
text-align: right;
border-bottom: 1px dotted #ccc;
}
.styled-table td:before {
content: attr(data-label);
float: left;
font-weight: bold;
}
}
表格悬停效果
添加悬停效果提升用户体验:
.styled-table tr:hover {
background-color: #e6f7ff;
cursor: pointer;
}
.styled-table tr:hover td {
color: #009879;
font-weight: bold;
}
表格边框样式
自定义表格边框样式:
.styled-table {
border: 1px solid #ddd;
border-radius: 5px;
overflow: hidden;
}
.styled-table th,
.styled-table td {
border-right: 1px solid #ddd;
}
.styled-table th:last-child,
.styled-table td:last-child {
border-right: none;
}
表格斑马条纹
使用CSS实现斑马条纹效果:
.styled-table tr:nth-child(odd) {
background-color: #f9f9f9;
}
.styled-table tr:nth-child(even) {
background-color: #ffffff;
}
以上方法可以单独使用或组合使用,根据具体需求调整CSS属性值。通过灵活运用这些CSS技巧,可以创建出既美观又实用的表格样式。







