Open In App
Related Articles

How to change the color of selected text using CSS ?

Improve Article
Save Article
Like Article

The colour of selected text can be easily changed by using the CSS | ::selection Selector. In the below code, we have used CSS ::selection on <h1> and <p> element and set its colour as yellow with green background.

Below example implements the above approach:


<!DOCTYPE html>
<html lang="en">
        How to change the color of
        selected text using CSS?
        .geeks h1 {
            color: green;
        h1::selection {
            background: green;
            color: yellow;
        p::selection {
            background: green;
            color: yellow;
    <div class="geeks">
            A Computer Science portal for
            geeks. It contains well written,
            well thought and well explained
            computer science and programming
            articles, quizzes and many more.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 15 Apr, 2020
Like Article
Save Article
Similar Reads
Complete Tutorials