CSS Selection Styling

By  on  

The goal of CSS is to allow styling of content and structure within a web page.  We all know that, right?  As CSS revisions arrive, we're provided more opportunity to control.  One of the little known styling option available within the browser is text selection styling.  Windows provides an ugly dark blue color for text selection and Mac provides a lighter blue color.  Mozilla Firefox, Internet Explorer 9, Opera, and Webkit-based browsers allow you to customize the styling of selected text.  Let me show you how.

The CSS

/* webkit, opera, IE9 */
::selection { background:lightblue; }
/* mozilla firefox */
::-moz-selection { background:lightblue; }

The -moz prefixed property is obviously for Firefox while the basic ::selection selector is for Webkit-based browsers.  Much like the other CSS selectors, you can nest usage of selection to use different colors in different places and depths:

/* webkit, opera, IE9 */
div.highlightBlue::selection { background:lightblue; }
/* mozilla firefox */
div.highlightBlue::-moz-selection { background:lightblue; }

/* webkit, opera, IE9 */
div.highlightPink::selection { background:pink; }
/* mozilla firefox */
div.highlightPink::-moz-selection { background:pink; }

CSS selection styling is just another one of those simple touches you can add to make your website more refined and vibrant!

Recent Features

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

Incredible Demos

  • By
    CSS Fixed Positioning

    When you want to keep an element in the same spot in the viewport no matter where on the page the user is, CSS's fixed-positioning functionality is what you need. The CSS Above we set our element 2% from both the top and right hand side of the...

  • By
    Creating the Treehouse Frog Animation

    Before we start, I want to say thank you to David for giving me this awesome opportunity to share this experience with you guys and say that I'm really flattered. I think that CSS animations are really great. When I first learned how CSS...

Discussion

  1. I’d like to add IE9 also good supports ::selection. It pains me to say that.

  2. Opera also supports the ::selection

  3. I am assuming that the -moz prefix can be dropped in FF4 than?

    Question, what is the real difference between pseudos with a “:” and “::”. Based on what I have observed I can only come to the conclusion one is for an element state and one is for element attr styling such as selection, placeholder etc….

  4. Thanks for the info i look forward to using it soon.

  5. KFee

    This doesn’t seem to apply to input or textarea boxes, at least in Chrome. Is there any way to accomplish selection styling in those elements?

  6. Coolest thing I’ve learnt today!

  7. Any more CSS goodies?

  8. me

    It also works on windows phone using IE.

  9. How can you change selection styles only in a specific div?

  10. Dan

    @Evil1

    Say you have a div with a class of tennisBall and another div with class of baseBall.

    to style it, you would use:
    /* webkit, opera, IE9 */
    div.tennisBall::selection { background:lightblue; }
    /* mozilla firefox */
    div.tennisBall::-moz-selection { background:lightblue; }
    
    /* webkit, opera, IE9 */
    div.baseBall::selection { background:pink; }
    /* mozilla firefox */
    div.baseBall::-moz-selection { background:pink; }
    

    That code would style the selection of Baseball to pink, and the style selection of tennis ball to lightblue..Hope this helps!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!