你正在瀏覽的是 Tailwind CSS v2. 的技術文件
Tailwind CSS 的 GitHub

Background Clip (背景裁切)

控制元素背景定界框的功能

Default class reference

Class
屬性
bg-clip-borderbackground-clip: border-box;
bg-clip-paddingbackground-clip: padding-box;
bg-clip-contentbackground-clip: content-box;
bg-clip-textbackground-clip: text;

用法

使用 bg-clip-{keyword} 功能來控制元素背景的定界框。

.bg-clip-border
.bg-clip-padding
.bg-clip-content
<div class="bg-clip-border p-6 bg-indigo-600 border-4 border-indigo-300 border-dashed"></div>
<div class="bg-clip-padding p-6 bg-indigo-600 border-4 border-indigo-300 border-dashed"></div>
<div class="bg-clip-content p-6 bg-indigo-600 border-4 border-indigo-300 border-dashed"></div>

Cropping to text

Use bg-clip-text to crop an element’s background to match the shape of the text. Useful for effects where you want a background image to be visible through the text. 使用 bg-clip-text 將元素背景根據文字的形狀進行裁切。當你想要在文字上顯示背景圖片的效果時很有用。

Hello world
<div class="text-5xl font-extrabold ...">
  <span class="bg-clip-text text-transparent bg-gradient-to-r from-green-400 to-blue-500">
    Hello world
  </span>
</div>

響應式

如要讓元素的背景裁切有響應式效果,可在現有背景裁切功能前加上前綴詞 {screen}: 。例如:中型或以上的螢幕尺寸使用 md:bg-clip-padding 以套用 bg-clip-padding 功能。

<div class="bg-clip-border md:bg-clip-padding">
  <!-- ... -->
</div>

更多有關 Tailwind 響應式設計功能的資訊,可參考 響應式設計 文件。

客製化

變化模式

默認情況下, 只有 響應式 的 background clip 變化模式 (variants) 會產生。

tailwind.config.jsvariants 區塊中變更 backgroundClip 屬性來決定有哪些變化模式會生成。

舉個例子來說,這個設定將會生成 響應式 和 hover 的變化模式。

  // tailwind.config.js
  module.exports = {
    variants: {
      extend: {
        // ...
+       backgroundClip: ['responsive', 'hover'],
      }
    }
  }

停用

如果你不打算在專案中使用 background clip 功能,可以在你的設定檔裡的 corePlugins 屬性中將 backgroundClip 設定為 false 來停用功能:

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