Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use body::selection, i want to customize highlight color

Why cannot use body::selection, when i want anything that can highlight to be the color i desired, anything include p img li h1

Examples here Now i want everything highlight to be RED color, but i am using body::selection, it never work

http://jsfiddle.net/kent93/nu6ju/

like image 393
Kent Liau Avatar asked Jan 28 '12 14:01

Kent Liau


People also ask

How do you change the highlight color in HTML?

It's pretty simple. To change the color of the highlighted-text, simply target the ::selection selector and then define the color of the background property.

How do I change the selected blue background?

How to remove blue background for focus/hover on select/option dropdown. Selected value in select with color. Add padding to the select options and change the border color.


1 Answers

If you want to apply the selection background to all elements, omit the type selector:

::selection {
    background: red;
}

And for that matter, add ::-moz-selection so it works in Firefox too:

::-moz-selection {
    background: red;
}
::selection {
    background: red;
}

It was never decided how exactly the styles you set for E::selection for any element E should propagate to descendants of E. There's a much more in-depth discussion in the www-style mailing list. It is also for this reason that ::selection has been totally dropped from CSS3 with the latest LC revision of CSS3 UI; see this section, which says:

The ::selection pseudo-element has been dropped since it was dropped from Selectors after testing found interoperability problems and further details to explore/define.

My best guess is that browsers (at least Firefox) just don't apply the same rule to descendant elements. So if you apply the pseudo-element to body, then only body text will have the custom selection background; everything nested inside it won't have the selection background.

like image 145
BoltClock Avatar answered Oct 23 '22 00:10

BoltClock