Languages
[Edit]
EN

CSS - style selected text

0 points
Created by:
nickkk0
647

In this article, we would like to show you how to style selected text using CSS.

Quick solution:

.class-name::selection {
    background: yellow;
}

 

Practical example

In this example, we present how to use ::selection pseudo element to customize selected text of each class. 

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    .yellow-selection::selection {
        background: yellow;
        color: red;
    }

    .green-selection::selection {
        background: lightgreen;
        color: yellow;
    }

  </style>
</head>
<body>
  <div class="yellow-selection">Select this text to see styled selection.</div>
  <div class="green-selection">Select this text to see styled selection.</div>
</body>
</html>

 

References

  1. ::selection - CSS: Cascading Style Sheets | MDN
  2. Pseudo-elements - CSS: Cascading Style Sheets | MDN
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join