Mis*_*hko 5 html css html-table alignment flexbox
给定一个包含数字的列的表格,我想将它们放在中心位置.
但是,我也想对齐这些数字!
table {
border: 1px solid black;
}
th {
width: 200px;
}
td {
text-align: center;
}Run Code Online (Sandbox Code Playgroud)
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>45</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>18923538273</td>
</tr>
<tr>
<td>9823</td>
</tr>
</tbody>
</table>Run Code Online (Sandbox Code Playgroud)
输出:
期望的输出:
注意:无论数字如何,表格单元格宽度应保持不变(200px).例如,如果所有数字都为0,则它们都应位于表格的中心:
也:
<td>s 的内容,但每个应该有一个数字<tr>.根据问题的编辑和一些评论进行更新
在评论中,您写道“在期望的结果中,单元格宽度随着数字的变化保持不变(200px)”。
在另一条评论中,您写道“...我的数字是链接,我希望它们占据整个单元格宽度”。
考虑到这些要求,我能找到的唯一基于 CSS 的解决方案是,使用显示CSS Table为 的<table>锚元素来代替元素,使全宽度可单击而无需添加事件处理程序,并且为了居中,使用伪元素来压入数字到中间。atable-row
堆栈片段
.table {
display: table;
border: 1px solid black;
}
.tr {
display: table-row;
text-decoration: none;
color: black;
}
.tr span {
display: table-cell;
width: 200px;
}
a.tr {
text-align: right;
}
.tr::before, .tr::after {
content: '';
display: table-cell;
width: 50%;
}Run Code Online (Sandbox Code Playgroud)
<div class="table">
<div class="thead">
<span class="tr">
<span>Amount</span>
</span>
</div>
<div class="tbody">
<a href="#1" class="tr">
<span>45</span>
</a>
<a href="#2" class="tr">
<span>2</span>
</a>
<a href="#3" class="tr">
<span>18923538273</span>
</a>
<a href="#4" class="tr">
<span>9823</span>
</a>
</div>
</div>
<div class="table">
<div class="thead">
<span class="tr">
<span>Amount</span>
</span>
</div>
<div class="tbody">
<a href="#1" class="tr">
<span>0</span>
</a>
<a href="#2" class="tr">
<span>0</span>
</a>
<a href="#3" class="tr">
<span>0</span>
</a>
<a href="#4" class="tr">
<span>0</span>
</a>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
_____________________________________________________________________________
这是我的第一个答案,我将保留它,因为可能有人可以按原样使用它。
实现此目的的一种简单方法是简单地table为值嵌套 a ,使用自动边距将其居中并右对齐其td内容。
这样,您将获得与原始标记几乎完全相同的行为,但可以更好地控制值对齐。
堆栈片段
table {
border: 1px solid black;
}
th {
width: 200px;
}
table table {
border: none;
margin: 0 auto;
}
table table td {
text-align: right;
}Run Code Online (Sandbox Code Playgroud)
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<table>
<tr>
<td>45</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>18923538273</td>
</tr>
<tr>
<td>9823</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<table>
<tr>
<td>0</td>
</tr>
<tr>
<td>0</td>
</tr>
<tr>
<td>0</td>
</tr>
<tr>
<td>0</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>Run Code Online (Sandbox Code Playgroud)
您当然可以使用div's 代替table,显示为内联块或内联弹性列。
内联块
table {
border: 1px solid black;
}
th {
width: 200px;
}
td {
text-align: center;
}
td > div {
display: inline-block;
}
td > div > div {
text-align: right;
}Run Code Online (Sandbox Code Playgroud)
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
<div>45</div>
<div>2</div>
<div>18923538273</div>
<div>9823</div>
</div>
</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
<div>0</div>
<div>0</div>
<div>0</div>
<div>0</div>
</div>
</td>
</tr>
</tbody>
</table>Run Code Online (Sandbox Code Playgroud)
内联柔性柱
table {
border: 1px solid black;
}
th {
width: 200px;
}
td {
text-align: center;
}
td > div {
display: inline-flex;
flex-direction: column;
}
td > div > div {
text-align: right;
}Run Code Online (Sandbox Code Playgroud)
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
<div>45</div>
<div>2</div>
<div>18923538273</div>
<div>9823</div>
</div>
</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
<div>0</div>
<div>0</div>
<div>0</div>
<div>0</div>
</div>
</td>
</tr>
</tbody>
</table>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2539 次 |
| 最近记录: |