Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS ::selection not working in Google Chrome

Tags:

html

css

I have this code which works fine in internet explorer and Mozilla Firefox but it fails on Google Chrome, this is the basic CSS ::selection selector. Attached is an illustration of the problem:

Desired result: (IE, Firefox)
enter image description here

What's wrong: (Chrome)
enter image description here

This only happens in Google Chrome, I am attaching the code I've tried here:

::-moz-selection {
  background-color: orange;
  color: white;
}
::selection  {
  background-color: orange;
  color: white;
}
::-webkit-selection {
  background-color: orange;
  color: white;
}
<ol>
  <li>Hello World!</li>
  <li>What's up?</li>
</ol>
like image 429
odedta Avatar asked Jul 05 '15 08:07

odedta


People also ask

Why is CSS not working in Chrome?

Make sure that your CSS and HTM/HTML files use the same encoding ! If your HTM/HTML files are encoded as UNICODE, your stylesheet has to be as well. IE and Edge are not fussy : stylesheets are rendered regardless of the encodings. But Chrome is totally intolerant of unmatched encodings.

Has selector not working in Chrome?

Enabling support for the :has() selectorMake sure you have Chrome 101+ and navigate to chrome://flags from the browser's address bar. Set the Experimental Web Platform features to Enabled and you're good to go. Relaunch the browser and you can work with CSS :has() in your Chrome browser.


3 Answers

::-webkit-selection doesn't seem to fix the issue, but there is another workaround for it:

::-moz-selection {
  background-color: orange;
  color: white;
}
::selection {
  background-color: orange;
  color: white;
}
ol {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}
ol span {
  -webkit-user-select: all;
  -moz-user-select: all;
  -ms-user-select: all;
}
<ol>
  <li><span>Hello World!</span></li>
  <li><span>What's up?</span></li>
</ol>
like image 151
Muhammet Avatar answered Sep 21 '22 14:09

Muhammet


You should add ::-webkit-selection

like image 39
Sreelal P Mohan Avatar answered Sep 18 '22 14:09

Sreelal P Mohan


Still wasn't working but this worked for me...

    * {
     -webkit-user-select: text;
     -moz-user-select: text;
     -ms-user-select: text; 
    }
like image 23
Josh Avatar answered Sep 21 '22 14:09

Josh