
本文首发公众号: 伊洛的小屋,欢迎关注并查看更多内容!!!
HTML表格
在HTML中构建表格数据可以让web显示数据变得更容易,表格在实际的生活中是极其常见的直观的表达方式
创建一个表格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link href="table.css" rel="stylesheet" type="text/css"> <title>TABLE</title> </head> <body> <table> <tr> <td>表格1</td> <td>表格2</td> <td>表格3</td> <td>表格4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> </table> </body> </html>
|
设置CSS表格样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| html { font-family: sans-serif; }
table { border-collapse: collapse; border: 2px solid rgb(200,200,200); letter-spacing: 1px; font-size: 0.8rem; }
td, th { border: 1px solid rgb(190,190,190); padding: 10px 20px; }
th { background-color: rgb(235,235,235); }
td { text-align: center; }
tr:nth-child(even) td { background-color: rgb(250,250,250); }
tr:nth-child(odd) td { background-color: rgb(245,245,245); }
caption { padding: 10px; }
|
打开浏览器

单元格跨越多行和列
需要用到colspan和rowspan两个属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| 公众号:伊洛的小屋 个人主页:https://yiluotalk.com/ 博客园:https://www.cnblogs.com/yiluotalk/ <html> <head> <meta charset="utf-8"> <title>表格TITLE</title> <link href="table.css" rel="stylesheet" type="text/css"> </head> <body> <h1>城市表格</h1> <table> <tr> <th colspan="2">北京</th> </tr> <tr> <th colspan="2">上海</th> </tr> <tr> <th rowspan="2">广东</th> <td>广州</td> </tr> <tr> <td>深圳</td> </tr> <tr> <th colspan="2">天津</th> </tr> <tr> <th rowspan="2">辽宁</th> <td>沈阳</td> </tr> <tr> <td>大连</td> </tr> </table> </body> </html>
|
打开浏览器

共同的样式
HTML有一种方法可以定义整列数据的样式信息
,
为表格增加标题
通过
元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <html> <head> <meta charset="utf-8"> <title>表格TITLE</title> <link href="table.css" rel="stylesheet" type="text/css"> </head> <body> <h1>城市表格</h1> <table> <caption>中国城市</caption> <tr> <th colspan="2">北京</th> </tr> <tr> <th colspan="2">上海</th> </tr> <tr> <th rowspan="2">广东</th> <td>广州</td> </tr> <tr> <td>深圳</td> </tr> <tr> <th colspan="2">天津</th> </tr> <tr> <th rowspan="2">辽宁</th> <td>沈阳</td> </tr> <tr> <td>大连</td> </tr> </table> </body> </html>
|
标题放在