Click here to Skip to main content
15,879,013 members
Articles / Web Development / HTML5
Tip/Trick

Change/Override the Default Selection Color for HTML

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
21 Nov 2014CPOL2 min read 11.3K   22   2  
Change/override the default selection color for HTML

Changing the Selection Colors in HTML

This CSS 3 (cascading style sheet) feature is incredibly simple to implement, but can provide your HTML pages a very unique look and helps your site stand out from the general crowd. With everything that we can do with CSS now a days, this trick can add a huge boost to the entire sites theme and help tie the entire site together with very little effort.

The fiddle is created here.

CSS
body {
    color:#284A66;
    background: #F9F9F9;
    font-family: Arial;
    padding:10px;
}

With the body of the page styled with the above CSS - where the style sets the font color to #284A66 (a Blue Gray color), background color to #F9F9F9 (a Light Gray shade) and sets the font as Arial and provides padding to all sides of the page to 10px, the page and its default selection will look as below:

Image 1

To change the selection color, we need to use CSS 3 pseudo-element ::selection as below:

CSS
::selection {
    color: #0BB7F5;
    background: #B6E9FC;
}

Here, the font color is set to #0BB7F5(a Sky blue color) and the selection/background color is set to #B6E9FC(a Light blue color).

CSS
::-moz-selection {
    color: #0BB7F5;
    background: #B6E9FC;
}

A Mozilla Firefox version of the pseudo-element is also set.

CSS
.highlight {
    color:white;
    background:#EC1455;
}
.highlight::selection {
    color: #0BB7F5;
    background: white;
}
.highlight::-moz-selection {
    color: #0BB7F5;
    background: white;
}

A CSS class .highlight is used to highlight some key phrases, which is applied to spans on the page. A different selection color scheme of white and light blue is used for the highlight class.

This shows that the ::selection pseudo-element can be combined with different elements on the page.

CSS
.highlight:hover {
    color:#8E1237;
    background:#EC1455;
}

Here, I have also added the pseudo-element :hover for the .highlight class, which changes the color schema of the highlight span when mouse hovers over it.

The page now looks as below. It's nothing fancy, but in a good designers hands, it can create pure magic.

Image 2

The final result with all the style and with the text selection looks as below. It can be seen that the spans with highlight class have a different color scheme.

Image 3

All the screen shots are from Mozilla Firefox. Chrome and Safari will not produce an opportune selection color for the highlight class - Excuse given: The pseudo-element ::selection, is not well documented in the CSS 3 specification!

And finally a real shocker, Internet Explored 8 and 9 produce the result as expected!!!

Happy coding...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
Be ahead of the curve.

Comments and Discussions

 
-- There are no messages in this forum --