阅读(4948) (0)

Tailwind CSS 表格布局

2022-07-25 17:31:37 更新

表格布局

用于控制表格布局算法的功能类。

Class
Properties
table-auto table-layout: auto;
table-fixed table-layout: fixed;

Auto

使用 ​table-auto​ 允许表格自动调整列的大小以适应单元格的内容。


<table class="table-auto">
  <thead>
    <tr>
      <th>Title</th>
      <th>Author</th>
      <th>Views</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Intro to CSS</td>
      <td>Adam</td>
      <td>858</td>
    </tr>
    <tr class="bg-emerald-200">
      <td>A Long and Winding Tour of the History of UI Frameworks and Tools and the Impact on Design</td>
      <td>Adam</td>
      <td>112</td>
    </tr>
    <tr>
      <td>Intro to JavaScript</td>
      <td>Chris</td>
      <td>1,280</td>
    </tr>
  </tbody>
</table>

Fixed

使用 ​table-fixed​ 允许表格忽略内容,使用固定的列宽。第一行的宽度将设置整个表格的列宽。

您可以手动设置一些列的宽度,其余的可用宽度将被平均分配给没有明确宽度的列。


<table class="table-fixed">
  <thead>
    <tr>
      <th class="w-1/2 ...">Title</th>
      <th class="w-1/4 ...">Author</th>
      <th class="w-1/4 ...">Views</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Intro to CSS</td>
      <td>Adam</td>
      <td>858</td>
    </tr>
    <tr class="bg-blue-200">
      <td>A Long and Winding Tour of the History of UI Frameworks and Tools and the Impact on Design</td>
      <td>Adam</td>
      <td>112</td>
    </tr>
    <tr>
      <td>Intro to JavaScript</td>
      <td>Chris</td>
      <td>1,280</td>
    </tr>
  </tbody>
</table>

自定义

变体

默认情况下, 针对 table layout 功能类,只生成 responsive 变体。

您可以通过修改您的 ​tailwind.config.js​ 文件中的 ​variants ​部分中的 ​tableLayout ​属性来控制为 table layout 功能生成哪些变体。

例如,这个配置也将生成 hover and focus 变体:

  // tailwind.config.js
  module.exports = {
    variants: {
      extend: {
        // ...
       tableLayout: ['hover', 'focus'],
      }
    }
  }

禁用

如果您不打算在您的项目中使用 table layout 功能,您可以通过在配置文件的 ​corePlugins ​部分将 ​tableLayout ​属性设置为 ​false ​来完全禁用它们:

  // tailwind.config.js
  module.exports = {
    corePlugins: {
      // ...
     tableLayout: false,
    }
  }