selection

Stunning Snippet – CSS Text Selection

February 20, 2010 in Snippets by Paul D'Amora

We’re all familiar with the classic blue color scene when you highlight text in your browser. Essentially the text turns white, and the background turns blue. What a surprisingly large portion of people don’t know is that you …

We’re all familiar with the classic blue color scene when you highlight text in your browser. Essentially the text turns white, and the background turns blue. What a surprisingly large portion of people don’t know is that you can control this with CSS. Okay, you don’t have absolute control over it, since it only works in Webkit and Gecko based browsers, but think of it as an extra treat for the people using those browsers. You’ll notice that if you highlight any text on this blog, it gives you a nice green background with white text. This is an important, yet often overlooked element of the design. It keeps everything standard, rather than looking like it was hacked together. Ok, enough talking, this is how you can get it to work on your website.

::selection {										background: #7fc548; color: white;text-shadow:none;/* Safari */}
::-moz-selection {										background:#7fc548; color: white;text-shadow:none;/* Firefox */}

You can use other properties to customize the ::selections pseudo class, but it isn’t recommended. You don’t want the padding of elements changing when you highlight them now do you?

Thank you for printing out this article. I'm glad you found the content on this site useful, and I hope you found everything you were looking for. For more awesome content like this, just visit http://net-cake.com, and contact me with any questions or concerns.

Share this post!

« »

Comments

  1. Alec February 20th, 20106:06 pm

    Way cool!

    ( Reply )
  2. Benje February 20th, 20109:07 pm

    I always wondered how you did that….

    ( Reply )
  3. Michelle April 20th, 20101:48 am

    Way cool!

    ( Reply )

Leave a Reply