Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

::selection not working correctly on line break

I am using ::selection to alter the color of the highlight, since the default blue doesn’t really work in my color theme. However, when I have multiline text with <br> (or \n from js, which just gets converted to <br>) the line breaks are always the default blue.

<!DOCTYPE html>
<html lang="en">
<body>
      <div class="selector">Try<br>selecting<br>me.</div>
</body>
<style>
.selector::selection {
    background-color: #8d8d8d;
}
</style>
</html>

How can I make all the selected text including line breaks the same color?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Add an additional .selector *::selection selector to the css that will define all child elements inside .selector.

<!DOCTYPE html>
<html lang="en">
<body>
      <div class="selector">Try<br>selecting<br>me.</div>
</body>
<style>
.selector::selection,
.selector *::selection {
    background-color: #8d8d8d;
}
</style>
</html>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading