怎么制作css表格
制作CSS表格的基本方法
使用HTML的<table>标签创建表格结构,结合CSS样式美化表格。以下是一个基础示例:
<table class="styled-table">
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
<tr>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</tbody>
</table>
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 25px 0;
font-size: 0.9em;
font-family: sans-serif;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
}
.styled-table thead tr {
background-color: #009879;
color: #ffffff;
text-align: left;
}
.styled-table th,
.styled-table td {
padding: 12px 15px;
}
.styled-table tbody tr {
border-bottom: 1px solid #dddddd;
}
.styled-table tbody tr:nth-of-type(even) {
background-color: #f3f3f3;
}
.styled-table tbody 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 tbody tr:hover {
background-color: #f5f5f5;
cursor: pointer;
}
边框样式定制
自定义表格边框样式:
.styled-table {
border: 1px solid #ddd;
border-radius: 5px;
overflow: hidden;
}
.styled-table th,
.styled-table td {
border-right: 1px solid #ddd;
border-left: 1px solid #ddd;
}
斑马条纹表格
使用CSS伪类创建交替行颜色:
.styled-table tbody tr:nth-child(odd) {
background-color: #f9f9f9;
}
.styled-table tbody tr:nth-child(even) {
background-color: #ffffff;
}
固定表头表格
对于长表格,可以固定表头:
.styled-table thead {
position: sticky;
top: 0;
z-index: 10;
}
单元格对齐方式
控制单元格内容对齐:
.styled-table td {
text-align: center; /* 或 left/right */
vertical-align: middle; /* 或 top/bottom */
}
这些方法可以根据实际需求组合使用,创建出各种风格的CSS表格。







