1. 边框
  2. 环偏移宽度

Quick reference

属性
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);

基本用法(Basic usage)

设置环形偏移宽度(Setting the ring offset width)

使用类似 ring-offset-2ring-offset-4 的工具,通过添加纯白色的盒子阴影并增加附加轮廓环的厚度来模拟偏移,从而适应偏移。

🌐 Use utilities like ring-offset-2 and ring-offset-4 to simulate an offset by adding solid white box-shadow and increasing the thickness of the accompanying outline ring to accommodate the offset.

ring-offset-0

ring-offset-2

ring-offset-4

<button class="... ring ring-pink-500 ring-offset-0">Button A</button>
<button class="... ring ring-pink-500 ring-offset-2">Button B</button>
<button class="... ring ring-pink-500 ring-offset-4">Button C</button>

更改偏移颜色(Changing the offset color)

在 CSS 中,你实际上不能偏移 box-shadow,所以我们必须使用与父元素背景颜色相匹配的纯色阴影来模拟。默认情况下我们使用白色,但如果你在不同的背景颜色上添加环形偏移,请使用环形偏移颜色工具,例如 ring-offset-slate-50,以匹配父元素背景颜色:

🌐 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, use the ring offset color utilities, like ring-offset-slate-50, to match the parent background color:

ring-offset-slate-50

<button class="ring ring-pink-500 ring-offset-2 ring-offset-slate-50 dark:ring-offset-slate-900 ...">
  Save Changes
</button>

欲了解更多信息,请参阅 ring offset color 文档。

🌐 For more information, see the ring offset color documentation.


有条件申请

悬停、聚焦及其他状态

Tailwind 允许你使用变体修饰符在不同状态下有条件地应用工具类。例如,使用hover:ring-offset-4 仅在 hover 时应用 ring-offset-4 工具。

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

有关所有可用状态修饰符的完整列表,请查看悬停、聚焦、以及其他状态 文档。

断点和媒体查询

你还可以使用变体修饰符来定位媒体查询,例如响应式断点、暗黑模式、首选减少运动等。例如,使用 md:ring-offset-4 仅在中等屏幕尺寸及以上时应用 ring-offset-4 工具。

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

要了解更多信息,请查看有关 响应式设计暗黑模式、以及 其他媒体查询修饰符 的文档。


使用自定义值(Using custom values)

自定义主题(Customizing your theme)

要自定义生成哪些环偏移宽度工具,请在 tailwind.config.js 文件的 theme 部分的 ringOffsetWidth 键下添加自定义值。

🌐 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
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    extend: {
      ringOffsetWidth: {
        '3': '3px',
        '6': '6px',
        '10': '10px',
      }
    }
  }
}

主题自定义文档中了解有关自定义默认主题的更多信息。

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

任意值(Arbitrary values)

如果你需要使用一次性的 ring-offset 值,而该值没有必要包含在你的主题中,请使用方括号动态生成属性,使用任意值。

<div class="ring-offset-[3px]">
  <!-- ... -->
</div>

任意值 文档中了解有关任意值支持的更多信息。