
CSS Topics
- CSS Intro
- CSS How To
- CSS Editors
- CSS Properties
- CSS Selectors
- .class
- .class1.class2
- .class1 .class2
- #id
- * (all)
- element
- element.class
- element,element
- element element
- element>element
- element+element
- element1~element2
- [attribute]
- [attribute=value]
- [attribute~=value]
- [attribute|=value]
- [attribute^=value]
- [attribute$=value]
- [attribute*=value]
- :active
- ::after
- ::before
- :checked
- :default
- :disabled
- :empty
- :enabled
- :first-child
- ::first-letter
- ::first-line
- :first-of-type
- :focus
- :fullscreen
- :has()
- :hover
- :in-range
- :indeterminate
- :invalid
- :lang()
- :last-child
- :last-of-type
- :link
- ::marker
- :not()
- :nth-child()
- :nth-last-child()
- :nth-last-of-type()
- :nth-of-type()
- :only-of-type
- :only-child
- :optional
- :out-of-range
- ::placeholder
- :read-only
- :read-write
- :required
- :root
- ::selection
- :target
- :valid
- :visited
- CSS Comments
- CSS Length
- CSS Image Sprites
- CSS Grid Layout
- CSS Grid Flexbox
- CSS @charset Rule
- CSS @font-face Rule
- CSS @import Rule
- CSS @keyframes Rule
- CSS @media Rule
CSS ::selection Selector
Photo Credit to CodeToFun
🙋 Introduction
The ::selection selector in CSS is used to define the styles for the portion of a document that a user has highlighted or selected.
This allows you to customize the appearance of text when it is selected by the user, rather than sticking to the default selection styling provided by the browser.
💡 Syntax
The signature of the ::selection Selector is as follows:
::selection {
/* CSS properties */
}The ::selection pseudo-element can be used with various text properties like color, background-color, and text-shadow.
Supported Properties:
- color
- background-color
- text-shadow
Note: Only a limited set of CSS properties can be applied to the ::selection pseudo-element.
📝 Example
Below is an example demonstrating how to use the ::selection selector to style text when it is highlighted by the user.
☠️ HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS ::selection Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<p>
Highlight this text to see the custom selection styling applied by the `::selection` selector.
</p>
</body>
</html>🎨 CSS
/* Styling for selected text */
::selection {
background-color: yellow;
color: black;
}In this example:
- When you highlight text in the paragraph, the selection will have a yellow background and black text color.
💬 Usage Tips
- Use contrasting colors for the background and text when styling with
::selection. This ensures readability when text is highlighted. ::selectioncan only be applied to block-level elements like paragraphs, headings, or lists. However, when the user highlights the text, the styling will only apply to the selected portion.- Keep in mind that not all CSS properties are supported in
::selection. You can usecolor,background-color, andtext-shadow, but properties likefont-sizeorborderare not allowed.
⚠️ Common Pitfalls
- Limited Browser Support for All Properties: While
::selectionis widely supported, not all browsers may support advanced properties liketext-shadow. Stick to basic styles likecolorandbackground-colorfor cross-browser consistency. - No Effect on Non-Selectable Text: The
::selectionselector will have no effect on elements where text cannot be selected, such as images or elements withuser-select: none.
🎉 Conclusion
The ::selection selector offers a simple way to improve the user experience by customizing the appearance of selected text. It provides a unique opportunity to align the look of selected text with your site's design, making for a more cohesive and polished visual experience. Use it carefully and keep browser compatibility in mind to ensure your custom selection styles work smoothly across different environments.
👨💻 Join our Community:
Author

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee












If you have any doubts regarding this article (CSS ::selection Selector), please comment here. I will help you immediately.