(表格单元格)等标签组成。
1.1 基本布局
1.2 CSS结构
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
二、美化表格的基本技巧
2.1 背景与颜色
为表格添加背景和颜色可以提升视觉效果。
table {
background-color: #f9f9f9;
}
th {
background-color: #4CAF50;
color: white;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
2.2 边框与边距
调整边框和边距可以使表格看起来更整洁。
th, td {
border: 2px solid #333;
padding: 12px 15px;
margin: 0;
}
2.3 字体与大小
合适的字体和大小可以让表格内容更易读。
th, td {
font-family: Arial, sans-serif;
font-size: 14px;
}
三、表格交互与动画
3.1 鼠标悬停效果
为表格添加鼠标悬停效果,可以让用户在浏览数据时更具趣味性。
tr:hover {
background-color: #e1e1e1;
}
3.2 动画效果
利用CSS动画,可以为表格添加动态效果。
tr {
transition: background-color 0.3s ease;
}
tr:hover {
background-color: #dcdcdc;
}
四、实战案例
以下是一个完整的CSS表格美颜案例:
| 产品名称 |
价格 |
库存 |
| 产品A |
¥100 |
50 |
| 产品B |
¥200 |
30 |
table {
width: 100%;
border-collapse: collapse;
background-color: #f9f9f9;
font-family: Arial, sans-serif;
font-size: 14px;
}
th, td {
border: 2px solid #333;
padding: 12px 15px;
text-align: left;
transition: background-color 0.3s ease;
}
th {
background-color: #4CAF50;
color: white;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
tr:hover {
background-color: #dcdcdc;
}
通过以上CSS美颜秘籍,相信您已经掌握了打造吸睛数据展示的技巧。赶快动手美化您的表格吧!
|