How to Change the Colors of Highlighted Text

In most browsers, the default colors for highlighted text are white letters on a blue background. If you highlight this paragraph, that is what you should see. However, using CSS3, you can change the both the background color and text color of a text selection. You can use this to your advantage by choosing colors that complement your website design.

Selection color is part of the CSS3 standard and is currently supported by Firefox, Chrome, Internet Explorer 9, Opera, and Safari. The highlighting color will not change in older versions of Internet Explorer. Firefox requires its own code with -moz- prefix, so you will have to write the same code twice to get the effect in every browser.

First, create a new CSS class for selection. In this example, we will call the class "xxx" and write the codes for both Firefox and Chrome/Opera:

.xxx::selection {

}

/* works for Chrome, Safari, Opera, and IE9+ */



.xxx::-moz-selection {

}

/* works for Firefox */


Next, within the braces, write the CSS code for the background and text colors. For the sake of demonstration, we will choose a light green background color and magenta text. In practice, it is better to choose colors with higher light/dark contrast and avoid colors that are too bright.

.xxx::selection {
background:#e0ffe0;
color:#e80080;
}

/* works for Chrome, Safari, Opera, and IE9+ */



.xxx::-moz-selection {
background:#e0ffe0;
color:#e80080;
}

/* works for Firefox */


This is a sample paragraph with the xxx class. If you select the text, the highlighted colors should be different from the highlighted colors in the paragraphs above. Note that you will only see the new colors if you are using Firefox, Safari, Chrome, IE9+, or Opera.



© Had2Know 2010