
表格对齐问题:如何将表格中的特定列右对齐?
在 html 表格中,您可以使用 css 样式来控制内容对齐方式。在这种情况下,要将最后两列向右对齐,可以使用以下步骤:
- 确保表格为 100% 宽度。这将允许表格占用可用空间的全部宽度。
- 设置需要右对齐的列为固定宽度。这将为列分配一个指定宽度,确保内容始终在此范围内。
- 将剩下的一列(本例中为第 2 列)设置为自动宽度。这将允许列根据其内容自动调整宽度。
下面是实现该效果的 html 和 css 代码:
html:
立即学习“前端免费学习笔记(深入)”;
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<style>
table {
width: 100%;
}
th:nth-child(3), th:nth-child(4) {
width: 150px;
}
th:nth-child(2) {
width: auto;
}
</style>
</head>
<body>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
</body>
</html>











