在 HTML5 頁面中,若需防止使用者選取、複製特定文字(如遊戲介面、驗證碼提示、版權聲明),可使用 CSS user-select 屬性。本文提供標準語法、廠商前綴回溯相容寫法、適用場景與無障礙考量,助你精準控制文字選取行為。
| 瀏覽器/引擎 | 屬性寫法 | 支援版本 |
|---|---|---|
| 標準(現代瀏覽器) | user-select: none; | Chrome 54+, Firefox 69+, Edge 79+, Safari 13+ |
| Firefox (舊版) | -moz-user-select: none; | Firefox 1+ |
| WebKit/Blink (舊版) | -webkit-user-select: none; | Chrome 1+, Safari 3+, Android Browser |
| IE 10+ / Edge (舊版) | -ms-user-select: none; | IE 10+, Edge 12-18 |
| KHTML/Konqueror | -khtml-user-select: none; | 極舊版本,現已罕見 |
.unselectable {
-webkit-user-select: none; /* Chrome/Safari/舊版 Edge */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* IE 10/11 */
user-select: none; /* 標準語法,最後寫入確保優先級 */
}
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>CSS user-select Demo</title>
<style>
.box { margin-bottom:20px; padding:10px; background:rgba(0,0,0,.05); border-radius:8px; }
.unselectable {
-webkit-user-select:none; -moz-user-select:none; -ms-user-select:none; user-select:none;
font-style:italic; color:#666;
}
</style>
</head>
<body>
<div class="box">【可選取】這段文字可以正常滑鼠選取、複製、貼上。</div>
<div class="box unselectable">【不可選取】這段文字套用 user-select:none,嘗試選取看看。</div>
<div class="box">【可選取】另一段可選取文字,確認隔離效果。</div>
</body>
</html>
<input>、<textarea>、contenteditable 元素不受 user-select:none 影響,仍可正常編輯。none,子層設 text 或 all 可局部恢復選取能力。auto(預設):由瀏覽器依元素類型決定(文字可選、輸入框可編輯)。text:僅允許選取文字內容,常用於覆蓋父層 none。all:單擊即選取整個元素內容(如程式碼區塊一鍵全選)。contain:選取範圍限制在元素內部(實驗階段,支援度低)。掌握 user-select 讓你在保護介面完整性與尊重使用者操作自由之間找到平衡,是前端細節打磨的必備技能。