Worldscope

Selection CSS

Palavras-chave:

Publicado em: 14/08/2025

Styling Text Selection with CSS

The `::selection` pseudo-element in CSS allows you to customize the visual appearance of selected text within a web page. This article explores how to use `::selection` to enhance the user experience by providing custom highlight colors and backgrounds when users select text.

Fundamental Concepts / Prerequisites

Before diving into styling text selections, you should have a basic understanding of:

  • CSS selectors: Understanding how to target specific HTML elements using selectors.
  • CSS properties: Familiarity with common CSS properties like `background-color`, `color`, and `text-shadow`.

Core Implementation

The `::selection` pseudo-element targets the portion of the document that has been selected by the user. Here's how to use it:


/*  Styling for the entire document */
::selection {
  background-color: #4CAF50; /* Green background */
  color: white; /* White text */
  text-shadow: 1px 1px 2px black; /* Add a shadow */
}

/* Styling for only 

elements */ p::selection { background-color: #008CBA; /* Blue background */ color: yellow; /* Yellow text */ }

Code Explanation

The first CSS rule targets the `::selection` pseudo-element globally. This means that the specified styles (green background, white text, and text shadow) will be applied to any selected text on the page, unless overridden by a more specific rule. It's a good practice to define a general default selection style.

The second CSS rule targets `::selection` specifically within `

` (paragraph) elements. When text within a paragraph is selected, it will have a blue background and yellow text. This demonstrates how to apply different selection styles to different elements on the page.

Complexity Analysis

Styling the `::selection` pseudo-element has negligible performance impact. The browser handles text selection natively. Applying a CSS style only affects the visual rendering of the selected text.

Therefore, the time complexity of applying the `::selection` style is considered to be O(1) (constant time) because the browser's rendering engine handles the selection and styling updates very efficiently.

The space complexity is also O(1) because we are only storing style information and not data that scales with input size.

Alternative Approaches

While `::selection` is the standard and most straightforward way to style text selection, JavaScript could be used to detect and style selected text. This would involve listening for the `mouseup` or `selectionchange` events, retrieving the selected text using the `window.getSelection()` method, and then manually wrapping the selected text in a `` element with custom styles.

This JavaScript approach offers more control and flexibility but comes at the cost of increased code complexity and potential performance overhead. It is generally not recommended unless you require specific functionalities that `::selection` cannot provide, such as dynamically changing the selection style based on user interaction.

Conclusion

The `::selection` pseudo-element provides a simple and effective way to customize the appearance of selected text on your web pages. It allows you to create a more visually appealing and consistent user experience without adding significant overhead. While JavaScript alternatives exist for more complex scenarios, `::selection` should be your go-to method for most text selection styling needs.