Change Text Selection Color using CSS

 

Recently when visiting a website I noticed that when its text was selected it had a theme that fit his website. Normally when text is selected it is a dark blue color. Now if your website doesn’t have any blue in it, would this not look a bit out of place?

When hunting for the solution of how this was done, I was pleasantly surprised how easy it was. Just add the following CSS to your page:

::selection            { background: #A8141B; color: white; /* Safari */ }
::-moz-selection    { background: #A8141B; color: white; /* Firefox */ }

As you can see, this only works in Firefox and Safari at the moment. This may seem like nothing, but it is a subtle effect that can add some visual appeal to your website!

Highlight any text on this page to view the above CSS in action (using Safari / Firefox).

Related posts:

  1. Cross Browser Placeholder Text For Input Fields
  2. Animate Text with jQuery Easing (screencast)
  3. Snazzy CSS Double Border Effect
  4. IE Cleartype (Blurry Text) Solution
  5. Plain Text, XML, and JSON Transfer Formats


Written by Brenley Dueck

 

10 Responses to “Change Text Selection Color using CSS”

  1. Hugo Says:

    December 15th, 2008 at 5:56 am

    Works well in Google Chrome 0.4 – 1.0 and FLock 2.0. Maxthon and IE (as expected) did not work.
    Nice effect! Adds that touch of detail and what I like to call ‘distinction’ to a site. Thanks for sharing!

  2. Cambiar el color del texto seleccionado en el navegador con CSS « Webmaster Libre Says:

    December 19th, 2008 at 8:43 am

    [...] Vía Brenezl [...]

  3. Hashim Says:

    December 21st, 2008 at 11:49 pm

    I need to know more about CSS

  4. Jake Says:

    January 1st, 2009 at 4:11 pm

    Thanks for this, I was afraid I would have to go into complicated javascripts to get this result.

  5. Cambiar el color del texto seleccionado en el navegador con CSS | Mantis Technology Solutions Blog Says:

    January 10th, 2009 at 6:50 pm

    [...] Vía Brenezl [...]

  6. Joren Rapini Says:

    January 19th, 2009 at 10:26 am

    Nice man! I just saw this effect on css-tricks, googled it and yours was the first site that came up.

  7. Aksel Says:

    May 18th, 2009 at 2:45 pm

    Testing your CSS trick I found out that you have one of the most broken appearance in IE 6 that I have seen lately.

  8. Akhyar Says:

    May 20th, 2010 at 8:08 pm

    Thank’s For Your Tutorial about CSS

  9. Tito Says:

    April 6th, 2011 at 6:19 pm

    Works fine in IE9, Chrome, Firefox

  10. Raj Says:

    July 25th, 2011 at 3:57 am

    thanks for the tip… i am looking for this… do u know any other code which also works on IE..?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
connect with me!