What is the browser-default background color when selecting text?
- Safari 6.0.3 Mac*:
#B4D5FE
- Chrome 26.0.1410.65 Mac*:
#ACCEF7
- Firefox 19.0 Mac*:
#B4D5FF
- Chrome 26.0.1410.64 m Windows 8+:
#3297FD
- Firefox 20.0.1 Windows 8+:
#3399FF
- Safari 5.1.7 Windows 8+:
#3298FD
- Internet Explorer 10.0.4 Windows 8+:
#3399FF
- Chrome 107.0.5304.88 Windows 11:
rgba(0, 116, 255, 0.8)
or#0074ffcc
*Found using ColorSnapper for Mac
+Found using ColorSchemer for Windows
And here's a screenshot of that information with the hex color codes highlighted in the same color:
This will be a never-ending list, however, since each...
- Browser
- Operating System
- Browser Version (maybe)
...will probably have a different color. Also, as of CSS3, you can change the color using:
::selection{
background-color:#000;
}
how to change the text selection background color
You can use certain CSS selectors to change CSS properties on selected text. (I tested this and it worked in Firefox, Safari, Chrome, and even Konqueror, but not IE). Example:
*::selection {
background: #cc0000;
color: #ffffff;
}
*::-moz-selection {
background: #cc0000;
color: #ffffff;
}
*::-webkit-selection {
background: #cc0000;
color: #ffffff;
}
You have to specify each selector separately or else it doesn't work (I guess the CSS parser stops processing a selector if it encounters an error). This changes the background color of the selected text to dark red and the color to white (and any other CSS you want to change). This doesn't have great cross-browser support (doesn't work in IE, and probably not Opera, either), but I think it is the only solution possible without some kind of complicated, buggy JavaScript script.
More info: http://www.quirksmode.org/css/selection.html
Is there any way to change background color of ::selection (selected text) using JavaScript?
In general where it's not possible to directly change pseudo element settings via JS it is possible to set CSS variables using JS.
So if you have something like this in your stylesheet:
::selection {
background-color: var(--selcolor);
}
and something like this in your JS:
element.style.setProperty('--selcolor', selcolor)
it will work.
Here's small example. It changes the selection color variable in the body:
function selChange(color) {
document.body.style.setProperty('--selcolor', color);
}
body {
--selcolor: yellow;
}
::selection {
background-color: var(--selcolor);
}
<button onclick="selChange('cyan');">Cyan</button>
<button onclick="selChange('magenta');">Magenta</button>
<button onclick="selChange('yellow');">Yellow</button>
<p>Highlight some of this text, then click a color and highlight some text...</p>
Permanently change selected text background color
You need to add event listener in javascript and when a text is selected at that time need to change the color like below
function logSelection(event) {
// Get Selection
sel = window.getSelection();
if (sel.rangeCount && sel.getRangeAt) {
range = sel.getRangeAt(0);
}
// Set design mode to on
document.designMode = "on";
if (range) {
sel.removeAllRanges();
sel.addRange(range);
}
// Colorize text
document.execCommand("ForeColor", false, "red");
// Set design mode to off
document.designMode = "off";
}
const input = document.querySelector('input');
input.addEventListener('select', logSelection);
How Do You Fix the Darkened Text Selection Color in Safari? - CSS
this issue described here
For now you can make your background-color 99% opacity
::selection {
background-color: rgba(0, 50, 0, 0.99);
color: #fff8dc;
}
Change background color of selected text on WebStorm?
Try changing background color of Editor > Selection background in Preferences | Editor | Color Scheme | General
Related Topics
CSS Checkboxes & Radio Buttons When Input Is Inside Label
How to Stretch a Div to 100% Page Height
CSS Flex Box Layout: Full-Width Row and Columns
Can Visual Studio 2013 Generate CSS Files from .Less Files
How to Select Multiple Input Field Types with CSS
Have Border-Radius Cover Inner Divs
Trying to Do a CSS Transition on a Class Change
What Font Format Should I Use for My Site
Horizontal Line in Background Using CSS3
How to Add Image Background to Btn-Default Twitter-Bootstrap Button
How to Fade in and Out Color of Svg
Responsive Design: Why Does Height Zero & Padding-Bottom Work for Making a Div Responsively Sized
How to Add Vertical Spacing Between Block Elements, But Not Top and Bottom
How to Prevent Fractional Pixels in an Element with Width Set to Auto
Whats The CSS to Make Something Go to The Next Line in The Page