样式数据表
你将构建什么
HTML page 使用数据表 showing 5 行的 student 数据, 使用样式请求头, zebra-striped 行, and hover 副作用.
学习目标
- Use 表, thead, tbody, tr, th, and td 元素正确地
- Apply zebra striping 使用 nth-child pseudo-类
- 添加 hover highlighting for 行
- 样式表 for readability 使用边框 and padding
前置要求
- HTML document 结构
- 基础 CSS selectors and 属性
步骤
- 1
创建表 HTML
创建表.HTML 使用表 元素 using thead and tbody. 添加 5 行的 student 数据使用 Name, Age, and Grade 列.
代码<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Student Table</title> <link rel="stylesheet" href="table.css"> </head> <body> <table> <thead> <tr> <th>Name</th> <th>Age</th> <th>Grade</th> </tr> </thead> <tbody> <tr><td>Ada Lovelace</td><td>20</td><td>A</td></tr> <tr><td>Grace Hopper</td><td>21</td><td>A+</td></tr> <tr><td>Alan Turing</td><td>22</td><td>B+</td></tr> <tr><td>Linus Torvalds</td><td>19</td><td>A</td></tr> <tr><td>Margaret Hamilton</td><td>23</td><td>A+</td></tr> </tbody> </table> </body> </html>预期结果
基础表 使用请求头 and 5 行的 student 数据.
- 2
样式表 结构
创建表.CSS. 添加边框-collapse, padding, and 边框到 制作表 clean.
代码table { border-collapse: collapse; width: 100%; max-width: 40rem; margin: 2rem auto; font-family: system-ui, sans-serif; } th, td { padding: 0.75rem 1rem; text-align: left; border-bottom: 1px solid #e2e8f0; }预期结果
clean 表使用 consistent padding and 底部边框.
- 3
样式请求头 and 添加 zebra striping
样式请求头行 使用 background 颜色. Use nth-child(偶数) for zebra striping on 请求体行.
代码th { background: #1e293b; color: #fff; font-weight: 600; } tbody tr:nth-child(even) { background: #f1f5f9; }预期结果
Dark 请求头行 and alternating 行 backgrounds for readability.
- 4
添加 hover highlighting
添加 hover 副作用 on tbody 行到 highlight 行 cursor 是 over.
代码tbody tr:hover { background: #e0e7ff; cursor: pointer; }预期结果
行 highlight in light indigo on hover.
提示
nth-child(偶数) 选择每个偶数-numbered 行 for zebra striping.
- 5
添加响应式样式
使用媒体查询为移动端调整布局:在小屏幕上将多列改为单列、缩小字号、调整间距。移动优先意味着小屏幕体验同样重要。
代码@media (max-width: 640px) { .container { flex-direction: column; padding: 1rem; } h1 { font-size: 1.5rem; } .card { width: 100%; margin-bottom: 1rem; } }预期结果
在 640px 以下视口,布局变为单列,字号和间距缩小,内容不溢出。
自己动手试试
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Table</title>
<link rel="stylesheet" href="table.css">
</head>
<body>
<table>
<!-- TODO: thead + tbody with 5 rows -->
</table>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Table</title>
<link rel="stylesheet" href="table.css">
</head>
<body>
<table>
<thead>
<tr><th>Name</th><th>Age</th><th>Grade</th></tr>
</thead>
<tbody>
<tr><td>Ada Lovelace</td><td>20</td><td>A</td></tr>
<tr><td>Grace Hopper</td><td>21</td><td>A+</td></tr>
<tr><td>Alan Turing</td><td>22</td><td>B+</td></tr>
<tr><td>Linus Torvalds</td><td>19</td><td>A</td></tr>
<tr><td>Margaret Hamilton</td><td>23</td><td>A+</td></tr>
</tbody>
</table>
</body>
</html>
/* table.css */
table { border-collapse: collapse; width: 100%; max-width: 40rem; margin: 2rem auto; font-family: system-ui, sans-serif; }
th, td { padding: 0.75rem 1rem; text-align: left; border-bottom: 1px solid #e2e8f0; }
th { background: #1e293b; color: #fff; font-weight: 600; }
tbody tr:nth-child(even) { background: #f1f5f9; }
tbody tr:hover { background: #e0e7ff; cursor: pointer; }你学到了什么
你构建 readable 数据表 使用语义 HTML, zebra striping 使用 nth-child, and hover 副作用 — 标准模式 for displaying tabular 数据 on web.
相关资源
相关课程
- HTML
表元素 and 结构.
- CSS Selectors
nth-child pseudo-类.
相关练习
- Pseudo-类测验
练习使用 CSS pseudo-类.
相关速查表
- HTML Cheatsheet
表元素引用.
相关术语
- HTML
HyperText Markup Language.
相关术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
- HTML
HyperText Markup Language.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。