Tailwind CSS Flex

2022-08-08 10:08 更新

Flex

用于控制 flex 項(xiàng)目放大和縮小的功能類。

Class
Properties
flex-1 flex: 1 1 0%;
flex-auto flex: 1 1 auto;
flex-initial flex: 0 1 auto;
flex-none flex: none;

Initial

使用 ?flex-initial? 允許 flex 項(xiàng)目在考慮到其初始尺寸的情況下縮小但不放大:


<div class="flex">
  <div class="flex-initial ...">
    <!-- Won't grow, but will shrink if needed -->
  </div>
  <div class="flex-initial ...">
    <!-- Won't grow, but will shrink if needed -->
  </div>
  <div class="flex-initial ...">
    <!-- Won't grow, but will shrink if needed -->
  </div>
</div>

Flex 1

使用 ?flex-1? 允許 flex 項(xiàng)目根據(jù)需要放大和縮小,忽略其初始尺寸。


<div class="flex">
  <div class="flex-1 ...">
    <!-- Will grow and shrink as needed without taking initial size into account -->
  </div>
  <div class="flex-1 ...">
    <!-- Will grow and shrink as needed without taking initial size into account -->
  </div>
  <div class="flex-1 ...">
    <!-- Will grow and shrink as needed without taking initial size into account -->
  </div>
</div>

Auto

使用 ?flex-auto? 允許一個(gè) flex 項(xiàng)目在考慮到其初始大小的情況下放大和縮小:


<div class="flex ...">
  <div class="flex-auto ...">
    <!-- Will grow and shrink as needed taking initial size into account -->
  </div>
  <div class="flex-auto ...">
    <!-- Will grow and shrink as needed taking initial size into account -->
  </div>
  <div class="flex-auto ...">
    <!-- Will grow and shrink as needed taking initial size into account -->
  </div>
</div>

None

使用 ?flex-none? 來阻止一個(gè) flex 項(xiàng)目的放大和縮?。?/p>


<div class="flex ...">
  <div class="flex-1 ...">
    <!-- Will grow and shrink as needed -->
  </div>
  <div class="flex-none ...">
    <!-- Will not grow or shrink -->
  </div>
  <div class="flex-1 ...">
    <!-- Will grow and shrink as needed -->
  </div>
</div>

響應(yīng)式

要控制 flex 項(xiàng)目在特定斷點(diǎn)處的放大和縮小方式,可在任何現(xiàn)有的功能類產(chǎn)添加 ?{screen}:? 前綴。例如,使用 ?md:flex-1? 僅在中等尺寸及以上的屏幕應(yīng)用 ?flex-1? 功能。

<div class="flex ...">
  <!-- ... -->
  <div class="flex-none md:flex-1 ...">
    Responsive flex item
  </div>
  <!-- ... -->
</div>

關(guān)于 Tailwind 的響應(yīng)式設(shè)計(jì)功能的更多信息,請(qǐng)查看 響應(yīng)式設(shè)計(jì) 文檔。

自定義

Flex Values

默認(rèn)情況下,Tailwind 提供了四個(gè) ?flex ?實(shí)用程序。您可以通過編輯 Tailwind 配置的 ?theme.flex? 部分來更改、添加或刪除這些內(nèi)容。

  // tailwind.config.js
  module.exports = {
    theme: {
      flex: {
        '1': '1 1 0%',
        auto: '1 1 auto',
       initial: '0 1 auto',
       inherit: 'inherit',
        none: 'none',
       '2': '2 2 0%',
      }
    }
  }

變體

默認(rèn)情況下, 針對(duì) flex 功能類,只生成 responsive 變體。

您可以通過修改您的 ?tailwind.config.js? 文件中的 ?variants ?部分中的 ?flex ?屬性來控制為 flex 功能生成哪些變體。

例如,這個(gè)配置也將生成 hover and focus 變體:

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

禁用

如果您不打算在您的項(xiàng)目中使用 flex 功能,您可以通過在配置文件的 ?corePlugins ?部分將 ?flex ?屬性設(shè)置為 ?false ?來完全禁用它們:

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


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)