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

Ring Offset Width

Utilities for simulating an offset when adding outline rings.

Default class reference

Class
屬性
ring-offset-0--tw-ring-offset-width: 0px; box-shadow: 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color), var(--tw-ring-shadow);
ring-offset-1--tw-ring-offset-width: 1px; box-shadow: 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color), var(--tw-ring-shadow);
ring-offset-2--tw-ring-offset-width: 2px; box-shadow: 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color), var(--tw-ring-shadow);
ring-offset-4--tw-ring-offset-width: 4px; box-shadow: 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color), var(--tw-ring-shadow);
ring-offset-8--tw-ring-offset-width: 8px; box-shadow: 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color), var(--tw-ring-shadow);

Usage

Use the ring-offset-{width} utilities to simulate an offset by adding solid white box-shadow and increasing the thickness of the accompanying outline ring to accommodate the offset.

<button class="... ring ring-pink-600 ring-offset-0">ring-0</button>
<button class="... ring ring-pink-600 ring-offset-2">ring-2</button>
<button class="... ring ring-pink-600 ring-offset-4">ring-4</button>

Changing the offset color

You can’t actually offset a box-shadow in CSS, so we have to fake it using a solid color shadow that matches the parent background color. We use white by default, but if you are adding a ring offset over a different background color, you should use the ring-offset-{color} utilities to match the parent background color:

bg-green-100

<div class="... bg-green-100">
  <button class="... ring ring-green-600 ring-offset-4 ring-offset-green-100">
    ring-offset-green-100
  </button>
</div>

For more information, see the ringOffsetColor documentation.


Responsive

To control the ring offset width at a specific breakpoint, add a {screen}: prefix to any existing ring offset width utility. For example, use md:ring-offset-4 to apply the ring-offset-4 utility at only medium screen sizes and above.

<button class="ring-2 ring-offset-2 md:ring-offset-4">
  <!-- ... -->
</button>

For more information about Tailwind’s responsive design features, check out the Responsive Design documentation.


Customizing

To customize which ring offset width utilities are generated, add your custom values under ringOffsetWidth key in the theme section of your tailwind.config.js file.

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      ringOffsetWidth: {
        '3': '3px',
        '6': '6px',
        '10': '10px',
      }
    }
  }
}

Learn more about customizing the default theme in the theme customization documentation.

Variants

默認情況下, 只有 響應式、focus-within 和 focus 的 ring offset width 變化模式 (variants) 會產生。

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

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

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

Disabling

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

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