1. 边框
  2. 环宽

Quick reference

Class
Properties
ring-0box-shadow: var(--tw-ring-inset) 0 0 0 calc(0px + var(--tw-ring-offset-width)) var(--tw-ring-color);
ring-1box-shadow: var(--tw-ring-inset) 0 0 0 calc(1px + var(--tw-ring-offset-width)) var(--tw-ring-color);
ring-2box-shadow: var(--tw-ring-inset) 0 0 0 calc(2px + var(--tw-ring-offset-width)) var(--tw-ring-color);
ringbox-shadow: var(--tw-ring-inset) 0 0 0 calc(3px + var(--tw-ring-offset-width)) var(--tw-ring-color);
ring-4box-shadow: var(--tw-ring-inset) 0 0 0 calc(4px + var(--tw-ring-offset-width)) var(--tw-ring-color);
ring-8box-shadow: var(--tw-ring-inset) 0 0 0 calc(8px + var(--tw-ring-offset-width)) var(--tw-ring-color);
ring-inset--tw-ring-inset: inset;

基本用法

¥Basic usage

添加戒指

¥Adding a ring

使用 ring-* 工具将特定厚度的实心框阴影应用于元素。默认情况下,环是半透明的蓝色,类似于许多系统中的默认聚焦环样式。

¥Use the ring-* utilities to apply solid box-shadow of a specific thickness to an element. Rings are a semi-transparent blue color by default, similar to the default focus ring style in many systems.

ring-2

ring

ring-4

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

环形工具与常规 shadow-* 工具优雅地组合在一起,并且可以在同一元素上组合。

¥Ring utilities compose gracefully with regular shadow-* utilities and can be combined on the same element.

你还可以使用 ringColorringOpacityringOffsetWidth 工具控制环的颜色、透明度和偏移量。

¥You can also control the color, opacity, and offset of rings using the ringColor, ringOpacity, and ringOffsetWidth utilities.

对焦环

¥Focus rings

环宽工具通过将 focus: 添加到任何 ring-* 工具的开头,可以轻松使用自定义聚焦环。

¥The ring width utilities make it easy to use custom focus rings by adding focus: to the beginning of any ring-* utility.

聚焦此按钮即可看到环出现

<button class="... focus:ring-2">Save Changes</button>

focus 变体默认情况下也为 ringColorringOpacityringOffsetWidthringOffsetColor 工具启用。

¥The focus variant is enabled by default for the ringColor, ringOpacity, ringOffsetWidth, and ringOffsetColor utilities as well.

嵌环

¥Inset rings

使用 ring-inset 工具强制在元素内部而不是外部渲染环。这对于屏幕边缘的元素很有用,因为在这些元素中环的一部分是不可见的。

¥Use the ring-inset utility to force a ring to render on the inside of an element instead of the outside. This can be useful for elements at the edge of the screen where part of the ring wouldn’t be visible.

<button class="... ring-2 ring-pink-300 ring-inset">
  Save Changes
</button>

有条件地应用

悬停、聚焦和其他状态

Tailwind lets you conditionally apply utility classes in different states using variant modifiers. For example, use hover:ring-4 to only apply the ring-4 utility on hover.

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

For a complete list of all available state modifiers, check out the Hover, Focus, & Other States documentation.

断点和媒体查询

You can also use variant modifiers to target media queries like responsive breakpoints, dark mode, prefers-reduced-motion, and more. For example, use md:ring-4 to apply the ring-4 utility at only medium screen sizes and above.

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

To learn more, check out the documentation on Responsive Design, Dark Mode and other media query modifiers.


使用自定义值

¥Using custom values

自定义主题

¥Customizing your theme

默认情况下,Tailwind 包含一些通用 ring-width 工具。你可以通过编辑 tailwind.config.js 文件中的 theme.ringWidththeme.extend.ringWidth 来自定义这些值。

¥By default, Tailwind includes a handful of general purpose ring-width utilities. You can customize these values by editing theme.ringWidth or theme.extend.ringWidth in your tailwind.config.js file.

tailwind.config.js
module.exports = {
  theme: {
    extend: {
      ringWidth: {
        '10': '10px',
      }
    }
  }
}

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

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

任意值

¥Arbitrary values

If you need to use a one-off ring value that doesn’t make sense to include in your theme, use square brackets to generate a property on the fly using any arbitrary value.

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

Learn more about arbitrary value support in the arbitrary values documentation.