【问题标题】:HTML table text-alignment (tbody adapted to thead)HTML表格文本对齐(tbody适应thead)
【发布时间】:2019-04-24 06:15:16
【问题描述】:

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>

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

【问题讨论】:

  • 请在上面的代码 sn-p 中添加 HTML。谢谢
  • 我现在做了,问题是我使用 laravel 和刀片模板,无法在此处执行

标签: html css laravel css-tables


【解决方案1】:

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

第 1 步 - 删除 CSS 下方,我们不需要阻止表格行。

.table_Bookingsystem thead tr {
  display: block;
}

第 2 步 - 从 table_Bookingsystem tbody 中删除 display: block;

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

第 3 步 - 我只是从 .table_Bookingsystem th, .table_Bookingsystem td 中删除 width: 5000px;,如果您想在每个 td/th 上应用 width: 5000px;,请还原它。

所有提到的都适用于下面的代码 sn-p。试试看,希望能帮到你。谢谢

.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>

【讨论】:

  • @lxg95 如果对您有帮助,请标记为正确。谢谢
【解决方案2】:

尝试将text-align: center; 添加到.table_Bookingsystem tbody 规则中,因为它位于.table_Bookingsystem td 规则中。

【讨论】: