在线css 表格制作
CSS 表格制作方法
使用纯CSS创建表格可以通过多种方式实现,以下是一些常见的方法和技巧:
使用HTML表格元素结合CSS样式
<table class="custom-table">
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</tbody>
</table>
.custom-table {
width: 100%;
border-collapse: collapse;
}
.custom-table th, .custom-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.custom-table th {
background-color: #f2f2f2;
}
.custom-table tr:nth-child(even) {
background-color: #f9f9f9;
}
使用CSS 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 .header, .grid-table .cell {
background-color: white;
padding: 8px;
}
.grid-table .header {
font-weight: bold;
background-color: #f2f2f2;
}
使用Flexbox布局创建表格
<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;
border: 1px solid #ddd;
}
.flex-table .row {
display: flex;
}
.flex-table .cell {
flex: 1;
padding: 8px;
border: 1px solid #ddd;
}
.flex-table .header {
font-weight: bold;
background-color: #f2f2f2;
}
表格样式增强技巧
添加悬停效果
.custom-table tr:hover {
background-color: #e6e6e6;
}
固定表头
.table-container {
height: 300px;
overflow-y: auto;
}
.custom-table thead th {
position: sticky;
top: 0;
}
响应式表格
@media (max-width: 600px) {
.custom-table {
display: block;
}
.custom-table thead, .custom-table tbody,
.custom-table th, .custom-table td,
.custom-table tr {
display: block;
}
.custom-table thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
.custom-table td {
border: none;
position: relative;
padding-left: 50%;
}
.custom-table td:before {
position: absolute;
left: 6px;
content: attr(data-label);
font-weight: bold;
}
}
在线CSS表格生成工具
-
CSS Table Generator (如TablesGenerator.com)
- 提供可视化界面设计表格
- 可自定义边框、间距、颜色等样式
- 自动生成HTML和CSS代码
-
Div Table (如divtable.com)
- 使用DIV元素创建表格布局
- 适合需要更灵活布局的场景
- 生成响应式表格代码
-
Bootstrap Table (getbootstrap.com/docs)
- 使用Bootstrap框架快速创建美观表格
- 提供多种预定义样式和功能
- 内置响应式设计
以上方法和工具可以帮助快速创建各种CSS表格,根据项目需求选择合适的实现方式。







