Changing the color of text when they highlight with CSS

I wrote this little tid-bit down a while ago, and I snagged it from Chris over at css-tricks.com. He has no idea who I am, but I’d like to think if he did, we would be on a first name basis.

Anyway, you can change the color of the highlight that shows when people select text on your site with this little bit of CSS:

::selection {
        background: #ffb7b7; /* Safari */
        }
::-moz-selection {
        background: #ffb7b7; /* Firefox */
}

If you wanted different colors depending on the paragraph they were selecting in, you would add a class to the tag and write the style like this:

p.className1::selection {
	background: #ff0000;
}
p.className2::-moz-selection {
	background: #ff0000;
}

Nice little bit of customization. Unfortunately it only works in Safari and Firefox, but try it out anyway. Its cool.

Tags:

Leave a Reply