CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

::Selection


The ::selection pseudo-element in CSS is used to style the portion of a document that has been highlighted by the user, for instance, by clicking and dragging the mouse over text. It provides a way to customize the appearance of selected text, enhancing user experience.


Example 1: Basic ::selection Styling

/* Style for the highlighted text within a paragraph */
p::selection {
  color: white; /* Sets the text color of the selection to white */
  background-color: #ff5733; /* Sets the background color of the selection to a shade of orange */
}

Explanation

This code targets any text selected by the user within a <p> tag. It changes the default selection colors to a white text color and an orange background, making the selection more visually distinct.


Example 2: Styling ::selection with text-shadow

/* Applying text-shadow to selected text */
::selection {
  color: #ffffff; /* White text color for the selection */
  background-color: #c70039; /* Deep red background for the selection */
  text-shadow: 2px 2px 4px #000000; /* Adds a black shadow to the selected text */
}

Explanation

This example demonstrates how to apply a text-shadow to selected text sitewide. The selected text will appear white with a deep red background and a subtle black shadow, adding depth to the highlighted content.


Example 3: Different ::selection Styles for Different Elements

/* Custom selection style for h1 elements */
h1::selection {
  color: #f9f9f9; /* Light gray text color */
  background-color: #333; /* Dark gray background */
}

/* Custom selection style for blockquotes */
blockquote::selection {
  color: #333; /* Dark gray text color */
  background-color: #f9f9f9; /* Light gray background */
}

Explanation

This CSS snippet shows how you can define different selection styles for various elements. Here, <h1> elements and <blockquote> elements will have contrasting selection colors, improving the visual hierarchy of selected content.


Example 4: Using RGBA for Semi-Transparent ::selection

/* Semi-transparent selection background */
div::selection {
  background-color: rgba(255, 193, 7, 0.5); /* A semi-transparent yellow background */
  color: black; /* Black text color */
}

Explanation

In this example, rgba() is used to create a semi-transparent background for the selected text within <div> elements. This allows the original background to be partially visible through the selection, creating a layered effect.


Example 5: No Text Color Change in ::selection

/* Changing only the background color of the selection */
::selection {
  background: #d4edda; /* A light green background for the selection */
}

Explanation

This code snippet illustrates how you can modify only the background color of the text selection, while keeping the original text color intact. This provides a more subtle highlighting effect.


Example 6: High Contrast ::selection for Accessibility

/* High contrast selection for better readability */
::selection {
  color: #000000; /* Black text color */
  background-color: #ffff00; /* Bright yellow background */
}

Explanation

This example focuses on accessibility by using a high-contrast color combination for the text selection. The bright yellow background with black text ensures that the selected content is easily readable for users with visual impairments.


Example 7: Gradient Background for ::selection (Limited Support)

/* Styling selection with a linear gradient background */
::selection {
  color: white; /* White text color */
  background: linear-gradient(to right, #6a11cb, #2575fc); /* A purple to blue gradient */
}

Explanation

This code demonstrates an advanced styling technique using a linear gradient as the background for the selected text. Browser support for this can be limited, but it creates a modern and visually appealing effect where supported.