gpt4 book ai didi

HTML 表格文本对齐(tbody 适配 thead)

转载 作者:太空宇宙 更新时间:2023-11-04 06:17:18 26 4
gpt4 key购买 nike

The Table with bad text-alignment

在图片中,您会看到文本对齐不正确的表格。 tbody 的元素不适合 thead 的元素。表头固定很重要,这样即使向下滚动也能看到。

我为此表使用的类是 table-hover 和我自己的类“table_Bookingsystem”,其属性列在下面

.table_Bookingsystem{
table-layout: fixed;
border-collapse: collapse;
width: 100%;
margin-left: auto;
margin-right: auto;
text-align: left;
padding-top: 16px;
padding-bottom: 16px;
border: 1px solid #ccc !important;
}

.table_Bookingsystem tbody{
display:block;
width: 100%;
overflow: auto;
}

.table_Bookingsystem thead tr {
display: block;
}

.table_Bookingsystem tr{
border-bottom: 1px solid #ddd;
}

.table_Bookingsystem thead {
background: black;
color:#fff;
}

.table_Bookingsystem th, .table_Bookingsystem td {
padding: 5px;
text-align: center;
width: 5000px; //important line
}

.table_Bookingsystem tbody tr:nth-child(even) {
background-color: #e4ebf2;
color: #000;
}
<table class="table_Bookingsystem table-hover ">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Auto</th>
<th scope="col">IMEI</th>
<th scope="col">Heimatstadt</th>
<th scope="col">derzeitige Stadt</th>

</tr>
</thead>
<tbody @if(count($cars) > 9) style="height: 300px;" @endif>
@foreach($cars as $car)
<tr class="table-row clickable-row" data-href='/cars/{{$car->id}}'>
<th>{{$loop->iteration}}</th>
<td>{{$car->name}}</td>
<td>{{$car->IMEI}}</td>
<td>
<?php
$city = Illuminate\Support\Facades\DB::table('cities')->where('id','=', $car->id_homeCity)->get('name');
echo $city[0]->name; ?>
</td>
<td>
<?php
$city = Illuminate\Support\Facades\DB::table('cities')->where('id','=', $car->id_currentCity)->get();
echo $city[0]->name; ?>
</td>

</tr>
@endforeach
</tbody>
</table>

当我将宽度设置为 250px 时,它看起来会好一些,但是当我只有 4 列或更少列时,就会有很大的优势。有什么方法可以保留固定的 header 并将 的每个 - 元素直接放在 的相应 - 元素下?

最佳答案

我写了一个基本的 table 就像你在 screenshot 中分享的那样。您添加了一些额外的 CSS,这就是您的 table ui 干扰的原因。

Step 1 - Remove below CSS, we don't need to block table row.

.table_Bookingsystem thead tr {
display: block;
}

Step 2 - Remove display: block; from table_Bookingsystem tbody

.table_Bookingsystem tbody{
width: 100%;
overflow: auto;
}

Step 3 - I just remove width: 5000px; from .table_Bookingsystem th, .table_Bookingsystem td, if you want apply width: 5000px; on each td/th, revert it.

所有提到的都适用于下面的代码片段。试试看,希望对你有帮助。谢谢

.table_Bookingsystem th, .table_Bookingsystem td {
padding: 5px;
text-align: center;
}

.table_Bookingsystem{
table-layout: fixed;
border-collapse: collapse;
width: 100%;
margin-left: auto;
margin-right: auto;
text-align: left;
padding-top: 16px;
padding-bottom: 16px;
border: 1px solid #ccc !important;
}

.table_Bookingsystem tbody{
width: 100%;
overflow: auto;
}

.table_Bookingsystem tr{
border-bottom: 1px solid #ddd;
}

.table_Bookingsystem thead {
background: black;
color:#fff;
}

.table_Bookingsystem th, .table_Bookingsystem td {
padding: 5px;
text-align: center;
}

.table_Bookingsystem tbody tr:nth-child(even) {
background-color: #e4ebf2;
color: #000;
}
<table class="table_Bookingsystem">
<thead>
<tr>
<th>#</th>
<th>Auto</th>
<th>IMEI</th>
<th>Heimatstadt</th>
<th>derzeitige Stadt</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Auto</td>
<td>0001</td>
<td>Regensburg</td>
<td>Regensburg</td>
</tr>
<tr>
<td>1</td>
<td>Auto</td>
<td>0001</td>
<td>Regensburg</td>
<td>Regensburg</td>
</tr>
</tbody>
</table>

关于HTML 表格文本对齐(tbody 适配 thead),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55823475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com