【发布时间】:2020-01-07 18:40:43
【问题描述】:
我正在尝试右对齐下表最后一列中的文本:
<table class="dataview">
<thead>
<tr>
<th>Customer Name</th>
<th>Email Address</th>
<th><span class="right">Balance</span></th>
</tr>
</thead>
<tbody>
<tr>
<td>Customer 1</td>
<td>testcust1@gmail.com</td>
<td class="right">$3000.00</td>
</tr>
<tr>
<td>Customer 2</td>
<td>testcust2@gmail.com</td>
<td class="right">$4000.00</td>
</tr>
</tbody>
</table>
使用以下 CSS:
.dataview{
width: 830px;
margin: 0 auto;
table-layout: fixed;
border-collapse: collapse;
border: 1px solid black;
}
.dataview th, .dataview td{
padding: 5px;
text-align: left;
}
.dataview th:nth-child(1), .dataview td:nth-child(1){min-width: 350px;}
.dataview th:nth-child(2), .dataview td:nth-child(2){min-width: 350px;}
.dataview th:nth-child(3), .dataview td:nth-child(3){min-width: 100px;}
.dataview thead{
background-color: cornflowerblue;
color: white;
}
.dataview tr{
border-bottom: 1px solid black
}
.dataview tbody tr:nth-last-child(1){
border-bottom: none;
}
.dataview thead tr{
display: block;
position: relative;
}
.dataview tbody{
display: block;
overflow-y: auto;
overflow-x: hidden;
width: 100%;
height: 50px;
}
.dataview td.right{
text-align:right;
background-color: green;
}
.dataview tbody tr:nth-child(even){
background-color: powderblue;
}
最后一列中的文本确实右对齐;但是,如果存在滚动条,则部分文本位于滚动条下方。我尝试了填充、边距、围绕跨度或 div 中的文本,但我无法从滚动条下方获取文本。如何解决此问题以使文本正确对齐? CodePen Example
【问题讨论】: