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

标签 html css laravel css-tables

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/

相关文章:

html - 自适应占位符和 Bootstrap 输入组

php - Laravel与Docker-Compose

html - 如何将 Grep 实现到 CGI 脚本中?

html - 在 ul 中插入虚线边框以打印 css

html - 如何让按钮在不同的 div 中对齐?

javascript - HTML - 如何在 OnClick 事件中更改按钮背景颜色

Laravel 帆 "build path ./vendor/laravel/sail/runtimes/8.0 either does not exist, is not accessible, or is not a valid URL."

php - 如何隐藏配置文件以防止直接访问?

javascript - 如何使用javascript获取具有id的其他div内的div的h2标签的值

javascript - 将此 jQuery 的 "onClick"操作从清除输入文本更改为将输入文本添加到下面的列表中?