Skip to content
Related Articles

Related Articles

Improve Article

How to change the color of selected text using CSS ?

  • Last Updated : 15 Apr, 2020
Geek Week

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.


Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :