DEV Community

Cover image for Changing the text selection color with CSS
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

13

Changing the text selection color with CSS

Did you know there is a CSS property that can change the user selection behavior?

It got some quite good support lately and is one of these cool gimmicks to have. It doesn't break anything for the browser that doesn't support it.

The end result will look like this.

CSS Selection selector

HTML Structure

For our demo, we will have multiple paragraphs that will each do something different.

<div>
  <h3>Select this paragraph in yellow marker</h3>
  <p class="yellow">Content here</p>
</div>

<div>
  <h3>Select this paragraph in black/white</h3>
  <p class="black">Content here</p>
</div>

<div>
  <h3>Select this paragraph in shadow text</h3>
  <p class="shadow">Content here</p>
</div>
Enter fullscreen mode Exit fullscreen mode

CSS Selection selector

Now we are going to use the selection attribute selector.

The default selector is written like this:

::selection {
  color: red;
}
Enter fullscreen mode Exit fullscreen mode

Then we can add a Mozilla specific one:

::-moz-selection {
  color: red;
}
Enter fullscreen mode Exit fullscreen mode

For our instance let's start with the yellow marker one:

p.yellow::selection {
  background: #FFFF00;
}
p.yellow::-moz-selection {
  background: #FFFF00;
}
Enter fullscreen mode Exit fullscreen mode

This will make the background of the selection a yellow color.

Now if we move on to our black and white selector:

p.black::selection {
  background: #000;
  color: #fff;
}
p.black::-moz-selection {
  background: #000;
  color: #fff;
}
Enter fullscreen mode Exit fullscreen mode

This will make the background black and the text color white.

Lastly I wanted to show you can do more then basic color selection and even add a text-shadow!

p.shadow::selection {
  text-shadow: 2px 2px 2px rgba(0, 0, 0, 0.5);
}
p.shadow::-moz-selection {
  text-shadow: 2px 2px 2px rgba(0, 0, 0, 0.5);
}
Enter fullscreen mode Exit fullscreen mode

Have a play around on this Codepen.

Browser Support

The browser support for the ::selection has really improved lately, and it can be used safely.

CSS selection support

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay