.no-select {
-webkit-user-select: none; /* Safari/舊版 Chrome */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* IE10+ / Edge Legacy */
user-select: none; /* 標準語法 */
}
| 瀏覽器 | 版本 | 前綴 | 備註 |
|---|---|---|---|
| Chrome | 6+ | -webkit- | 53+ 無前綴 |
| Firefox | 2+ | -moz- | 69+ 無前綴 |
| Safari | 3.1+ | -webkit- | 13+ 無前綴 |
| Edge | 12+ | -ms- | 79+ (Chromium) 無前綴 |
| IE | 10+ | -ms- | IE9- 不支援 |
| iOS Safari | 3.2+ | -webkit- | |
| Android WebView | 2.1+ | -webkit- |
| 值 | 行為 | 適用場景 |
|---|---|---|
none | 元素及子元素文字不可選。若從外部拖選進入,仍可選取內容。 | UI 控制元、圖標、拖拽手柄 |
auto | 預設。可編輯元素(input/textarea/contenteditable)可選,其餘不可選。 | 重置繼承的 none |
text | 文字可選,不限制選取範圍(可跨元素)。 | 文章區塊、代碼區 |
element | 僅限元素內部發起選取有效,選取範圍被限制在元素邊界內。 | 獨立可選區塊(如代碼卡片) |
all | 單擊即選取整個元素內容(Firefox/Chrome 支援)。 | 代碼片段、Token 複製區 |
IE6-IE9 不支援 user-select,需結合屬性/事件:
<div class="no-select" onselectstart="return false;">不可選內容</div>
.no-select {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* IE6-9 專用 Hack */
* html .no-select { /* IE6 */
/* 利用 expression 動態綁定事件,不推薦生產環境 */
}
*:first-child+html .no-select { /* IE7 */
/* 同上 */
}
現代專案建議**放棄 IE9 以下支援**,若必須支援,請引入 legacy-user-select Polyfill。
body { user-select: none; } /* 全站禁選 */
.article-content { user-select: text; } /* 僅文章區可選 */
.code-block { user-select: all; } /* 代碼區單擊全選 */
.draggable {
user-select: none;
}
.draggable.is-dragging {
/* 拖拽時 body 級禁選,防止選到頁面其他文字 */
}
body.dragging * { user-select: none; }
若需「視覺不可選」但「右鍵複製/Ctrl+C 有效」,可用透明偽元素覆蓋:
.copyable::before {
content: attr(data-text);
position: absolute;
inset: 0;
color: transparent;
user-select: text; /* 偽元素文字可選 */
pointer-events: none;
}
.copyable { position: relative; user-select: none; }
navigator.clipboard.writeText()),優於依賴原生選取。使用標準 user-select: none 加四大前綴覆蓋 99% 現代瀏覽器。放棄 IE9- 支援或用 Polyfill。嚴格限制應用範圍,遵循「預設可選、按需禁選」原則,兼顧體驗與無障礙。