【发布时间】:2018-05-04 03:41:27
【问题描述】:
我需要将表格中特定 TD 元素的宽度设置为其内容宽度,但由于从数据库加载的动态数据,此内容是可变的。
这就是我所拥有的:
<html>
<head>
<style>
div.block {
display: inline-block;
font-family: monospace;
}
.debug1 {
border: solid blue 1px;
}
.debug2 {
border: solid red 1px;
}
</style>
</head>
<body>
<table style="width: 600px; border: solid black 1px;">
<tbody>
<tr>
<td class=debug1 style="text-align: right;">
◄
</td>
<td style="text-align: center;">
<div class="block debug2">700000</div>
<div class="block debug2">700001</div>
<div class="block debug2">700002</div>
</td>
<td class=debug1>
►
</td>
</tr>
</tbody>
</table>
</body>
</html>
小提琴:https://jsfiddle.net/wm592qj2/
这就是我想要的:
<html>
<head>
<style>
div.block {
display: inline-block;
font-family: monospace;
}
.debug1 {
border: solid blue 1px;
}
.debug2 {
border: solid red 1px;
}
</style>
</head>
<body>
<table style="width: 600px; border: solid black 1px; table-layout: fixed;">
<tbody>
<tr>
<td class=debug1 style="width: 200px; text-align: right;">
◄
</td>
<td style="width: 160px; border: solid cyan 1px; text-align: center;">
<div class="block debug2">700000</div>
<div class="block debug2">700001</div>
<div class="block debug2">700002</div>
</td>
<td class=debug1 style="width: 200px;">
►
</td>
</tr>
</tbody>
</table>
</body>
</html>
小提琴https://jsfiddle.net/yj9oy9gs/
回顾:
- 我希望记录列表(
block类)显示在中心,因此 TD 元素的宽度必须设置为其内容的宽度,但内容始终是可变的(它可能有3 个 div,或者它可能有 10 个 div) - 我希望 Next 和 Previous 按钮与最后一个和第一个 div 相邻(记录,即类
block)。我正在实现一个记录浏览器,所以它必须看起来不错。
请注意,第二个小提琴具有固定宽度,这就是它起作用的原因,但我对第二个 TD 元素(表格的中心部分)具有动态宽度。
那么,我该怎么做呢?
【问题讨论】: