custom-selection

Add custom ::selection css pseudo-element to website using data attributes all so you can improve your websites look and feel
Custom CSS Selection
Add custom ::selection
css pseudo-element to website using data attributes all so you can improve your websites look and feel
Installation
⚠️ 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.
CDN
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>
Usage
Basic
With simple detection.
HTML
<body data-selection data-selection-color="1a1a1a" data-selection-text-color="#fffff"></body>
Element Attributes
Attribute | Values | Description |
---|---|---|
data-selection |
Initialises JavaScript | |
data-selection-color |
hex code |
|
data-selection-text-color |
hex code |
|
data-selection-text-stroke |
boolean |
|
data-selection-text-stroke-color |
hex code |
|
data-selection-text-stroke-width |
string |
|
data-selection-text-stroke-fill-color |
hex code |
|
data-selection-text-decoration |
boolean |
|
data-selection-text-decoration-color |
hex code |
|
data-selection-text-decoration-line |
string |
underline , overline & line-through |
data-selection-text-decoration-style |
string |
solid , double , dotted , dashed & wavy |
data-selection-text-decoration-thickness |
string |
Demo
Values Used
attribute | value |
---|---|
data-selection |
- |
data-selection-color |
#f7f33a |
data-selection-text-color |
#cba6d6 |
Final Element
<body data-selection data-selection-color="#f7f33a" data-selection-text-color="#cba6d6"></body>
Browser support
source : mdn web docs
supportFull
=
supportRequires a vendor prefix or different name for use
=
supportNo
=
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 |