Changing the text selection color with CSS

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
One of those little things that are nice to see. Thanks
This is a great feature, and can be extra fun when you randomly change the highlight color!
https://blog.braydoncoyer.dev/change-text-highlight-color-with-css
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

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.

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>
Now we are going to use the selection attribute selector.
The default selector is written like this:
::selection {
color: red;
}
Then we can add a Mozilla specific one:
::-moz-selection {
color: red;
}
For our instance let's start with the yellow marker one:
p.yellow::selection {
background: #FFFF00;
}
p.yellow::-moz-selection {
background: #FFFF00;
}
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;
}
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);
}
Have a play around on this Codepen.
The browser support for the ::selection has really improved lately, and it can be used safely.

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