Personalizing the snippets selection background on Stack Overflow

I'm currently working on designing a custom dark theme for Stack Overflow using Stylish. Everything was going smoothly until I encountered an issue while customizing a particular snippet. I'm having trouble changing the selection color to something other than the default one specified:

.CodeMirror-line::selection,.CodeMirror-line>span::selection,.CodeMirror-line>span>span::selection{
    background:#d7d4f0;
}

I attempted to add the following code, but it doesn't seem to have any effect:

.CodeMirror-line::selection,.CodeMirror-line>span::selection,.CodeMirror-line>span>span::selection{
    background: blue !important;
}

Is there a way to modify the selection background in this case?

Here's the complete CSS code from the theme, in case it provides any useful information:

body, #questions div, .qa-block, #content {
    background:#282828 !important;
}

#footer.categories {
    background:#181818 !important;
}
...
/* The rest of the CSS code continues here */
...

Answer №1

Open up codemirror.css and navigate to line 321 where you'll find the following CSS rules:

.CodeMirror-selected { background: #d9d9d9; }
.CodeMirror-focused .CodeMirror-selected { background: #d7d4f0; }
.CodeMirror-crosshair { cursor: crosshair; }
.CodeMirror-line::selection, .CodeMirror-line > span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }
.CodeMirror-line::-moz-selection, .CodeMirror-line > span::-moz-selection, .CodeMirror-line > span > span::-moz-selection { background: #d7d4f0; }
  1. Ensure to include these rules in your stylesheet.
  2. Experiment with changing the background-color property to a custom color for each of these rules.
  3. Consider adding !important to see how that affects the styles.

Answer №2

It seems that my feedback on the solution answer is effective.

.CodeMirror-selected {
    background: blue;
}

.CodeMirror-focused .CodeMirror-selected {
    background: blue;
}

https://i.sstatic.net/uPDLF.png

Answer №3

Big shoutout to @vals for pointing out something interesting:

.CodeMirror-selected {
    background: red;
}

.CodeMirror-focused .CodeMirror-selected {
    background: red
}

It seems that the above code was functioning perfectly fine on its own, but not within my Stylish theme. After some investigation, I finally discovered the reason:

The following rule was causing interference by overriding any background changes we attempted to make on the selection:

.prettyprint, blockquote, pre, pre>.spaces, .hi, .badge-tag {
    background:#353535 !important;
}

To address this issue, I made a targeted adjustment to specifically focus on the Snippet line:

pre.CodeMirror-line{
  background: inherit !important;  
}

Thanks to implementing @vals' solution, everything is now working smoothly :)

I appreciate everyone who contributed to helping me resolve this issue!

Answer №4

::-moz-selection { /* Styling for Firefox */
   background:#d7d4f0;
}

::selection {
    background:#d7d4f0;
}

Try using this CSS code to fix the issue you are experiencing.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhancing webpage design by dynamically changing borders and headers using JavaScript

I have implemented a fixed positioning for the table headers using the following code: onScroll={() => { document.querySelector('thead').style.transform = `translate(0,${this.scrollRef.scrollTop}px)`; }} However, when I scroll the ta ...

Utilizing d3.js to implement a scatterplot with zoom functionality that focuses solely on zooming the axis without affecting

Having trouble creating a scatterplot with zoom functionality where only the axis is getting zoomed, not the data itself. Can anyone provide some assistance or insight on what might be wrong? If you're interested in checking out the project, here&apo ...

Create a navigation bar using CSS that is designed from an unordered list without any specific

Is it possible to create a multilevel menu using only CSS for the menu structure mentioned below? Websites like cssmenumaker.com showcase examples of menus with 2-3 level submenus by adding classes like has-submenu. Can we achieve this without adding any ...

What is the best way to create non-standard text wrapping in HTML and CSS that is both semantic and sleek, avoiding square or circular shapes?

Is there a way to achieve text wrapping like this using semantic and clean HTML and CSS that is compatible with all browsers? The only solution I can think of is adding different classes to the <p> element. However, the drawback of this approach is ...

Error loading CSS file on Google App Engine

I attempted to add a CSS file as a static file in my project just to experiment with how it functions, but encountered difficulties right from the start. The content of the CSS file is: body { background:#00FF00; } In addition, here is my configurat ...

I am trying to make my CSS divs remain in the exact same position on the background image, even when the browser is resized both horizontally and vertically. How can I achieve this?

I need the red dot divs to remain fixed on a world map, regardless of browser resize. Not sure if I should be adjusting div properties or background. Here's my CSS code, any feedback is appreciated as I'm new to web design: HTML: <!DOCTYPE ...

Concealing and revealing an element using the CSS property visibility:hidden/visible

There are two div-boxes that should show/hide when clicking on another two div-boxes. I want the divs to maintain their space so as not to disrupt the DOM, ruling out the use of .toggle(). I attempted this approach without success: $('#red, #pink&ap ...

Ensure that the child div completely fills the entire width of its parent container,

My table contains another table within each row. I have implemented scroll overflow for the mobile view, but this causes the rows to not fill the available space and lose their background when scrolling. I have experimented with using width: fit-content a ...

The CSS style of the Div element is not being displayed properly when embedded with JavaScript

Currently, I am working on a simple page for practice purposes. The main issue I am facing is with a div element that has a red border and a blue background. Inside the div, there is a script tag calling an external JavaScript file. Surprisingly, the JavaS ...

What can I do to keep my navbar on top of the slideshow?

https://i.sstatic.net/SfiLZ.jpg Is there a way to create a responsive navbar that sits in front of a slideshow containing images? I attempted to place the div within the main, but unfortunately it was unsuccessful. ...

Animating with React using the animationDelay style does not produce the desired effect

Is there a way to apply an animation to each letter in a word individually when hovering over the word? I want the animation to affect each letter with an increasing delay, rather than all at once. For example, if scaling each letter by 1.1 is the animatio ...

The paragraph tag remains unchanged

I am currently working on developing a feature that saves high scores using local storage. The project I'm working on is a quiz application which displays the top 5 scores at the end, regardless of whether the quiz was completed or not. However, I&apo ...

maintain visibility of website menu while scrolling

I am having trouble getting my drop-down menu to stay fixed at the top of the screen while scrolling down on my website. Despite several attempts, I have not been able to achieve this using the current CSS and menu setup. Can someone please assist me wit ...

Using an image as a dropdown menu in HTML: A step-by-step guide

I am facing a challenge in creating an HTML/CSS button that should display a dropdown when hovered over, but unfortunately, it's not working as expected. Here is the link to the code. .Panel { overflow: hidden; width: 25%; height: 100%; co ...

Utilize a CSS rule exclusively if the following div element is present - Purely CSS, no need

Can we apply a CSS rule only when a div follows another div? For example: <div class="div1">...</div> <div class="div2">..</div> CSS Example: (if div2 exists) .div2 .div1 (apply CSS rule to div1) { backgrou ...

increasing the size of the drop-down menu width

I have been struggling to adjust the width of my dropdown list box on my page. Despite my efforts, I cannot seem to make it bigger. Here is the code snippet causing me trouble: <div class="form-group row"> <div class="col-sm-1&quo ...

Displaying a banner at the bottom of a React page

I need to display a banner with the text "All items..." on every page. Currently, I have set the position attribute to fixed, but the banner is aligned to the left and I want it to be centered horizontally instead. Below is my code snippet: export const Ba ...

Issue with a input element having relative positioning within a flexbox

Objective: Aim to align the middle DIV (MIDDLE) within a flexbox row in the center. Issue: The right element includes an input element with relative positioning. Consequently, the middle DIV element (MIDDLE) is no longer centered but instead shifted to th ...

Displaying two dropdown menus with just a single coding component

Hey everyone, I'm having a problem with the dropdown list. I added an ASP dropdown list to a modal popup using a Bootstrap theme, but when the modal pops up, the dropdown list appears twice. Check out this image for reference: https://i.stack.imgur.co ...

Solutions for concealing the current div when clicking on a new div that reveals a fresh one

Is there a way to hide the current div once you click on a new div that reveals another one? The code below toggles the display of the div, but what I am attempting to achieve is that when you click on a new item after clicking on the first one, the first ...