1. 交互
  2. 用户选择

Quick reference

Class
Properties
select-noneuser-select: none;
select-textuser-select: text;
select-alluser-select: all;
select-autouser-select: auto;

基本用法

¥Basic usage

禁用文本选择

¥Disabling text selection

使用 select-none 可防止选择元素及其子元素中的文本。

¥Use select-none to prevent selecting text in an element and its children.

尝试选择文本以查看预期的行为

The quick brown fox jumps over the lazy dog.
<div class="select-none ...">
  The quick brown fox jumps over the lazy dog.
</div>

允许选择文本

¥Allowing text selection

使用 select-text 允许选择元素及其子元素中的文本。

¥Use select-text to allow selecting text in an element and its children.

尝试选择文本以查看预期的行为

The quick brown fox jumps over the lazy dog.
<div class="select-text ...">
  The quick brown fox jumps over the lazy dog.
</div>

一键选择所有文本

¥Selecting all text in one click

使用 select-all 在用户单击时自动选择元素中的所有文本。

¥Use select-all to automatically select all the text in an element when a user clicks.

尝试选择文本以查看预期的行为

The quick brown fox jumps over the lazy dog.
<div class="select-all ...">
  The quick brown fox jumps over the lazy dog.
</div>

使用自动选择行为

¥Using auto select behaviour

使用 select-auto 以使用默认的浏览器行为来选择文本。对于在不同的断点处撤消其他类(如 select-none)很有用。

¥Use select-auto to use the default browser behavior for selecting text. Useful for undoing other classes like select-none at different breakpoints.

尝试选择文本以查看预期的行为

The quick brown fox jumps over the lazy dog.
<div class="select-auto ...">
  The quick brown fox jumps over the lazy dog.
</div>

有条件地应用

悬停、聚焦和其他状态

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

<div class="hover:select-all">
  <!-- ... -->
</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:select-all to apply the select-all utility at only medium screen sizes and above.

<div class="md:select-all">
  <!-- ... -->
</div>

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