<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> table{ width: 300px; margin: 5px auto; /*table 和td之间默认会有一个距离,通过border-spacing设置为零*/ border-spacing: 0px; /*用于设置边框合并成一条线,则border-spacing自动失效*/ border-collapse: collapse; border: 1px solid black; } /*隔行变色*/ tr:nth-child(even){ background-color: dodgerblue; } /*选中变色*/ tr:hover{ background-color: red; } td , th{ border: 1px solid black; text-align: center; } </style> </head> <body> <!-- 长表格:方便翻页,表头和表尾可以不变,表头永远在顶部,表尾永远在底部,与写的顺序无关--> <table> <thead> <th></th> </thead> <tbody> <tr></tr> </tbody> <tfoot> <tr></tr> </tfoot> </table> <!--短表格: 没有写tbody但是浏览器会自动添加tbody table和tr是爷孙更关系,中间隔了一个tbody --> <table> <!-- <tbody>系统会自动添加--> <tr> <th>1</th> <th>2</th> <th>3</th> <th>4</th> </tr> <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> <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> <!-- </tbody>--> </table> </body> </html>