如何使用 CSS 停用文字選擇高亮?


要使用 CSS 停用文字選擇高亮,這有助於防止內容被複制。我們可以使用 CSS 的 user-select 屬性來停用文字選擇高亮。在本文中,我們將瞭解如何使用 CSS user-select 屬性來停用文字選擇高亮。

我們有一個包含一些文字內容的 div 元素。我們的任務是使用 CSS 停用文字選擇高亮。

使用 CSS 停用文字選擇高亮

  • 我們使用 div 標籤在其中編寫文字內容。
  • 在編寫文字內容後,我們使用了 邊框 來區分不可選擇的文字。
  • 我們使用了 "user-select: none;",它停用了文字選擇。

示例

以下是實現上述步驟以使用 CSS 停用文字選擇高亮的完整示例程式碼。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        To disable text selection highlighting using CSS
    </title>
    <style>
        .no-select {
            user-select: none;
            border: 2px solid green;
        }
    </style>
</head>
<body>
    <h3>
        To Disable Text Selection Highlighting Using CSS
    </h3>
    <p>
        In this example we have used <strong>user-select:
        none;</strong> property to disable text selection
        highlighting using CSS.
    </p>
    <div class="no-select">
        This text cannot be selected by the user.
    </div>
</body>
</html>

結論

在本文中,我們瞭解瞭如何使用 CSS 停用文字選擇高亮。我們對 div 元素使用了 CSS 的 user-select 屬性來停用文字選擇。

更新於: 2024年8月19日

8K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.