Add custom ::selection
css pseudo-element to website using data attributes all so you can improve your websites look and feel
⚠️ Custom CSS Selection is a controversial practice that can cause usability, accessibility, and performance issues. Don't override selected text styles for purely aesthetic reasons — users can customize them to suit their needs. For people experiencing cognitive concerns or who are less technologically literate, unexpected changes to selection styles may hurt their understanding of the functionality. If overridden, it is important to ensure that the contrast ratio between the text and background colors of the selection is high enough that people experiencing low vision conditions can read it.
Inside <body>
closing tag
<!-- Custom Selection -->
<script src="https://cdn.jsdelivr.net/npm/custom-selection@1.1.2/dist/custom-selection.core.min.js"></script>
With simple detection.
<body data-selection data-selection-color="1a1a1a" data-selection-text-color="#fffff"></body>
attribute | value |
---|---|
data-selection |
- |
data-selection-color |
#f7f33a |
data-selection-text-color |
#cba6d6 |
<body data-selection data-selection-color="#f7f33a" data-selection-text-color="#cba6d6"></body>
source : mdn web docs
supportFull
=
supportRequires a vendor prefix or different name for use
=
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on iOS | Samsung Internet | WebView Android | |
---|---|---|---|---|---|---|---|---|---|---|---|
::selection |
|||||||||||
dataset |
|||||||||||
custom-selection.core.js |