HTML表格是由行(<tr>
)和列(<td>
或<th>
)组成的,用于展示结构化数据。行高指的是表格中每一行的高度。
HTML表格的基本结构包括:
<table>
:定义表格。<tr>
:定义表格的行。<td>
:定义表格的单元格。<th>
:定义表格的表头单元格。HTML表格广泛应用于各种需要展示数据的场景,如:
可以通过CSS来设置HTML表格的行高。以下是几种常见的方法:
在每个<tr>
标签中使用内联样式来设置行高。
<table>
<tr style="height: 50px;">
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr style="height: 70px;">
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
在<head>
部分使用内部样式表来设置行高。
<!DOCTYPE html>
<html>
<head>
<style>
tr.row1 {
height: 50px;
}
tr.row2 {
height: 70px;
}
</style>
</head>
<body>
<table>
<tr class="row1">
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr class="row2">
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
</body>
</html>
将CSS样式放在一个单独的文件中,并通过<link>
标签引入。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<table>
<tr class="row1">
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr class="row2">
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
</body>
</html>
styles.css
文件内容:
tr.row1 {
height: 50px;
}
tr.row2 {
height: 70px;
}
原因:
px
、em
、%
等。解决方法:
<!DOCTYPE html>
<html>
<head>
<style>
tr.row1 {
height: 50px;
}
tr.row2 {
height: 70px;
}
</style>
</head>
<body>
<table border="1">
<tr class="row1">
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr class="row2">
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
</body>
</html>
通过以上方法,你可以轻松地为HTML表格设置不同的行高,以满足不同的展示需求。
腾讯云存储知识小课堂
云+社区沙龙online第5期[架构演进]
企业创新在线学堂
助跑计划之生态伙伴成长营—云上直播
云+社区技术沙龙[第9期]
云+社区技术沙龙[第28期]
腾讯云GAME-TECH游戏开发者技术沙龙
中国数据库前世今生
领取专属 10元无门槛券
手把手带您无忧上云