HTML table text-alignment(tbody适配thead)
HTML table text-alignment (tbody adapted to thead)
The Table with bad text-alignment
在图片中,您看到 table 和错误的 text-alignment。 tbody 的元素不适合 thead 的元素。重要的是 table 的头部是固定的,这样即使向下滚动也能看到它。
我为此 table 使用的 class 是 table-hover 和我自己的 class "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 并将每个 - 元素直接放在相应的 - 元素下 ?
尝试将 text-align: center;
添加到 .table_Bookingsystem tbody
规则,因为它在 .table_Bookingsystem td
规则中。
我写了一个基本的 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>
The Table with bad text-alignment
在图片中,您看到 table 和错误的 text-alignment。 tbody 的元素不适合 thead 的元素。重要的是 table 的头部是固定的,这样即使向下滚动也能看到它。
我为此 table 使用的 class 是 table-hover 和我自己的 class "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 并将每个 - 元素直接放在相应的 - 元素下 ?
尝试将 text-align: center;
添加到 .table_Bookingsystem tbody
规则,因为它在 .table_Bookingsystem td
规则中。
我写了一个基本的 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;
fromtable_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 applywidth: 5000px;
on eachtd/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>