【发布时间】:2021-01-30 19:59:41
【问题描述】:
我想让我的桌子的所有三行都具有相同的高度。但是带有图标的行总是更高。
是否有一些神奇的 css 设置可以让我自动让表格的所有行都有一个恒定的高度,无论我是否添加了一个图标?
<!DOCTYPE html>
<html lang="en" >
<head>
<title>TEST</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<style>
#my_div {
line-height: 0.3;
}
.my_icon {
width: 1em;
height: 1em;
vertical-align: middle;
}
</style>
<div class="container-fluid bg-info" id="my_div">
<table class="table">
<tbody>
<tr class="table-primary row">
<td class="col-sm-2">No Icon in this row. </td>
</tr>
<tr class="table-primary row">
<td class="col-sm-2">Icon Row:
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="css-class my_icon"><path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4"/><polyline points="10 17 15 12 10 7"/><line x1="15" y1="12" x2="3" y2="12"/></svg>
</td>
</tr>
<tr class="table-primary row">
<td class="col-sm-2">No Icon in this row. </td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
#my_div {
line-height: 0.3;
}
.my_icon {
width: 1em;
height: 1em;
vertical-align: middle;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<div class="container-fluid bg-info" id="my_div">
<table class="table">
<tbody>
<tr class="table-primary row">
<td class="col-sm-2">No Icon in this row. </td>
</tr>
<tr class="table-primary row">
<td class="col-sm-2">Icon Row:
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="css-class my_icon"><path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4"/><polyline points="10 17 15 12 10 7"/><line x1="15" y1="12" x2="3" y2="12"/></svg>
</td>
</tr>
<tr class="table-primary row">
<td class="col-sm-2">No Icon in this row. </td>
</tr>
</tbody>
</table>
</div>
【问题讨论】:
-
不幸的是没有魔法,你可以通过在 tds 上设置一个足够大的高度来保存你的图像,或者通过 js,在窗口加载时检查 td 的高度并设置高度他们都到了最高的数字。顺便说一句,你为什么在桌子上使用 bootstrap css 作为网格?
-
<td>中的文本长度是静态的吗?还是文本长度未知(短行和长行文本)? -
感谢您对“用于表格网格的引导 css”的评论。事实证明,如果我摆脱了表格的东西并使用网格,一切都会很开心。
标签: html css bootstrap-4 icons