How to Change the Text Selection Color with CSS

If you’re like me, you’ve spent hours surfing the web looking for design inspiration. And if you have, then no doubt you’ve probably stumbled across the same websites of pristine designers who I pretty much think can do no wrong.

Once you’re there, highlight some of their text and you’ll see something you might not have seen before. Better yet, something you wish you had on your website.

I’m talking about the behavior and style of text selection, which is a part of CSS that most folks don’t know much about.

What is Text Selection?

By definition, the ::selection selector matches the portion of an element that is selected by a user.

In other words, when you select a portion of someone’s site content, you’ll see that the text is displayed as a highlight. By default, most browsers use a very light blue background color with no change to the font color.

What most folks don’t know is that through CSS, you can style the font color and background color.

How to Change the Default Text Selection Color

Now to the good stuff – only a few CSS properties can be applied to ::selection: color, background, cursor, and outline.

The ::selection selector is supported in IE9+, Opera, Google Chrome and Safari. Firefox supports an alternative, the ::-moz-selection.

So here’s how you would style the text selection in the second example above:

/* Body
------------------------------------------------------------ */

::-moz-selection {
    background-color: #dd2020;
    color: #fff;
}

::selection {
    background-color: #dd2020;
    color: #fff;
}

Please note that because of browser quirks the CSS above will not work if the you try to combine the selectors as such:

/* Body
------------------------------------------------------------ */

::-moz-selection,
::selection {
    background-color: #dd2020;
    color: #fff;
}

It’s really that simple. Go ahead and have some fun with CSS and styling the selected text on your website and tell me how it goes.

Some Really Nice Examples of Text Selection

1. Elliot Jay Stocks (view site)
2. Veerle Pieters (view site)
3. Natalie Jost (view site)
4. Jason Santa Maria (view site)
5. Janna Hagan (view site)

Email Newsletter

Like what you read here in this blog post?
Get more like it delivered to your inbox daily.

Comments

  1. says

    Very cool! This isn’t something I ever would have thought of doing but now I have to go implement it right away…somewhere. I also like that your text-selection color is orange.

  2. says

    This is really cool!

    This doesn’t apply to my phone, which I use way too much for everything, but it would be fun to mess around with.

    I think it would be neat if I matched up every aspect of color on my site, this included.

    Thanks,
    -Gabe

  3. says

    Not heard of this selector.
    Guess we just select text and don’t even think about how the selection is highlighted.

    Notice you are a white text on orange background man – looks good.

    Stored this away for future use.

  4. says

    This is been great and I love the way how simply you have presented or implementing any changes. Its been great feature and will be using this in my upcoming work. Thank you for sharing :-)

  5. GSalam says

    Hi Brian,
    Thanks for this post, and for the many other good posts (tutorials).
    I just switched to the Prose theme, from Generate. I noticed that the text selection colour that you created for the theme is quite nice.
    I guess I’m gonna stick to that for some time. I may however try other options outlined here, in future.
    Thanks again.

  6. says

    Thanks. i was trying to Change the Default Text Selection Color but was failing so Google it and found your post. Indeed its helpful for me in real terms.

  7. says

    Great tip. I’ve seen this @wpbeginner and I liked the effect.
    Now you know the trick, let’s put some quality content on our blogs so the readers would like to copy-paste the text and see the color of the selected text.

  8. Tomas says

    hi,

    nice tutorial. Do you use a plugin for the newsletter subscribe form above (just below the post content)?
    If so, I’d appreciate the name and where to get it (even if it’s for sale). I’ve been looking for a customised newsletter subscribe plugin for my blog.

    Regards

  9. says

    Thanks for the tip. I wish this were supported by more modern browsers. Reminds me of when developers were first able to style the vertical scrollbars in IE many years ago. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

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