1. Purpose

Set a random color for the selected text and its background, if the page already has colors for the background and text that are different from the standard ones. Any solution that does not spoil anything is welcome.



2. Minimum code example

body { background: sienna; color: pink; margin: 0; font-size: 3rem; } ::selection { background: moccasin; color: lightcoral; } ::selection:window-inactive { background: moccasin; color: lightcoral; } ::-moz-selection { background: moccasin; color: lightcoral; } 
 Не принять мне итоги, чья щемит прямота. Отречений дороги заведут в никуда. Ты — цветок Поднебесья, Ты — улыбка в ночи, эхо сладостных песен и Венеры лучи. Ты — колибри прекрасный, Амазонки сапфир, — дочь природы несчастной в звуках музыки лир. Личность мерно теряешь, изучая Тебя. Сон, еду забываешь, без сознанья Любя. 

He's on Codepen.io . For Firefox, it appears that the highlighted text in the inactive window is not supported.



3. Desired behavior

In the pseudo-element :: - moz-selection, the background of the selected text is rgb(255, 228, 181) , which corresponds to the color I specified moccasin . Tested in Firefox.

moz-selection



4. Actual behavior

But in the pseudo-element :: selection, the background of the selected text is rgb(216, 158, 096) . It turns out badly. Tested in Chrome and Opera.

selection

If you remove the above code background: sienna; color: pink; background: sienna; color: pink; , colors for selection in Chrome and Opera are displayed normally.

selection without background

But if you set the background and color properties for the body tag, then the selected colors become irregular, as if colors overlap. How to remove it, google failed.



5. Attempts to solve

  1. Specifying color instead of X11 in other formats has not changed anything.
  2. Using the attribute contenteditable , as advised on Stack Overflow, also does not change anything.

    1 answer 1

    The background color of the selection in Chrome has a certain degree of transparency, so you need to specify your color in the RGBA format, setting the opacity to less than 100% :

     ::selection { background: rgba(255, 228, 181, 0.99); color: lightcoral; } 

    In this case, 255, 228, 181 corresponds to the moccasin color you specified.

     body { background: sienna; color: pink; margin: 0; font-size: 3rem; } ::selection { background: rgba(255, 228, 181, 0.99); color: lightcoral; } ::selection:window-inactive { background: rgba(255, 228, 181, 0.99); color: lightcoral; } ::-moz-selection { background: rgba(255, 228, 181, 0.99); color: lightcoral; } 
     Не принять мне итоги, чья щемит прямота. Отречений дороги заведут в никуда. Ты — цветок Поднебесья, Ты — улыбка в ночи, эхо сладостных песен и Венеры лучи. Ты — колибри прекрасный, Амазонки сапфир, — дочь природы несчастной в звуках музыки лир. Личность мерно теряешь, изучая Тебя. Сон, еду забываешь, без сознанья Любя. 

    A live example on JSFiddle: jsfiddle.net/terron/nrwjLkbm/

    Chrome result:

    Chrome result

    Result in Firefox:

    Firefox result

    • terron, y ::selection and ::moz-selection different behavior is not in favor of ::selection . ::selection also highlights images and fonts. See results in Firefox and Chrome . If in the case of pictures you can set background: transparent , then I don’t understand how to cancel or customize the selection of fonts connected via Cufon. - Sasha Chernykh
    • The code is the result of the execution , unimportant in Chrome. Laid out on the site, because Codepen does not miss Cufon. // Therefore, I rejected your editing and have not yet accepted (plus put) your answer: maybe there will be a better solution in JavaScript that normally selects fonts. Thank. - Sasha Chernykh
    • @SachaBlack, you can solve the problem with transparency from the side by choosing a color that will be displayed as moccasin with 50% transparency. The problem with the curve selection on Cufon could not be solved even on the official website , where the selection of words climbs on each other. On the other hand, if you admit that someone will highlight or copy text on your site, I would recommend you to abandon Cufon for UX and use more traditional and Orthodox methods for loading fonts. - neluzhin
    • terron, yes, I know that Cufon is an outdated solution, but I will have to change a lot. In Firefox, fonts do not stand out at all . Only how to implement it for other browsers, I will not google. Thank. - Sasha Chernykh