如何制作css表格
制作CSS表格的方法
使用HTML表格标签结合CSS样式
HTML提供<table>、<tr>、<th>、<td>等标签构建表格结构,通过CSS美化样式:

<table class="styled-table">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Row 1 Cell 1</td>
<td>Row 1 Cell 2</td>
</tr>
</table>
.styled-table {
width: 100%;
border-collapse: collapse;
}
.styled-table th, .styled-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.styled-table th {
background-color: #f2f2f2;
}
使用CSS Grid布局
通过display: grid属性实现非传统表格布局:

<div class="grid-table">
<div class="header">Header 1</div>
<div class="header">Header 2</div>
<div class="cell">Data 1</div>
<div class="cell">Data 2</div>
</div>
.grid-table {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 1px;
background-color: #ddd;
}
.grid-table > div {
padding: 8px;
background-color: white;
}
.header {
font-weight: bold;
}
使用Flexbox布局
通过display: flex创建灵活的表格结构:
<div class="flex-table">
<div class="row">
<div class="cell header">Header 1</div>
<div class="cell header">Header 2</div>
</div>
<div class="row">
<div class="cell">Data 1</div>
<div class="cell">Data 2</div>
</div>
</div>
.flex-table {
display: flex;
flex-direction: column;
}
.row {
display: flex;
}
.cell {
flex: 1;
padding: 8px;
border: 1px solid #ddd;
}
.header {
font-weight: bold;
background-color: #f2f2f2;
}
响应式表格设计
针对移动设备优化表格显示:
@media screen and (max-width: 600px) {
.styled-table {
display: block;
}
.styled-table thead {
display: none;
}
.styled-table tr {
margin-bottom: 10px;
display: block;
border: 1px solid #ddd;
}
.styled-table td {
display: block;
text-align: right;
border-bottom: 1px solid #eee;
}
.styled-table td::before {
content: attr(data-label);
float: left;
font-weight: bold;
}
}
每种方法适用于不同场景:传统表格标签适合数据密集型展示,CSS Grid/Flexbox适合需要灵活布局的情况,响应式设计确保多设备兼容性。






