154

I'm looking for a way to change the CSS rules for pseudo-class selectors (such as :link, :hover, etc.) from JavaScript.

So an analogue of the CSS code: a:hover { color: red } in JS.

I couldn't find the answer anywhere else; if anyone knows that this is something browsers do not support, that would be a helpful result as well.

Karun
  • 188
  • 2
  • 11
user39882
  • 1,549
  • 2
  • 11
  • 4

12 Answers12

214

You can't style a pseudo-class on a particular element alone, in the same way that you can't have a pseudo-class in an inline style="..." attribute (as there is no selector).

You can do it by altering the stylesheet, for example by adding the rule:

#elid:hover { background: red; }

assuming each element you want to affect has a unique ID to allow it to be selected.

In theory the document you want is http://www.w3.org/TR/DOM-Level-2-Style/Overview.html which means you can (given a pre-existing embedded or linked stylesheet) using syntax like:

document.styleSheets[0].insertRule('#elid:hover { background-color: red; }', 0);
document.styleSheets[0].cssRules[0].style.backgroundColor= 'red';

IE, of course, requires its own syntax:

document.styleSheets[0].addRule('#elid:hover', 'background-color: red', 0);
document.styleSheets[0].rules[0].style.backgroundColor= 'red';

Older and minor browsers are likely not to support either syntax. Dynamic stylesheet-fiddling is rarely done because it's quite annoying to get right, rarely needed, and historically troublesome.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • 36
    Why wasn't this chosen as answer? – Shlomo Zalman Heigh Mar 24 '11 at 23:26
  • More specific reference URLs: - < http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleSheet > - < https://developer.mozilla.org/en/DOM/CSSStyleSheet/insertRule > - < http://developer.apple.com/library/safari/#documentation/AppleApplications/Reference/WebKitDOMRef/CSSStyleSheet_idl/Classes/CSSStyleSheet/index.html#//apple_ref/js/instm/CSSStyleSheet/insertRule/unsignedlong/%28inDOMString,inunsignedlong%29 > –  Aug 28 '11 at 06:13
  • 2
    Firefox: "Error: The operation is insecure." – 8128 Jul 14 '12 at 16:14
  • @fluteflute the operation is considered insecure if you're attempting to manipulate a CSS file from a different domain (I guess it's a type of same-origin policy thing). Shame! Simple function to check conforms to same-origin policy: `function sameOrigin(url) { var loc = window.location, a = document.createElement('a'); a.href = url; return a.hostname === loc.hostname && a.port === loc.port && a.protocol === loc.protocol; }` – WickyNilliams Nov 07 '12 at 14:43
  • 4
    Manipulating the stylesheet just to apply styles to a specific element is not recommended since it causes the browser to reflow the whole document everytime the stylesheet is changed. http://www.stubbornella.org/content/2009/03/27/reflows-repaints-css-performance-making-your-javascript-slow/ – Johannes Ewald Aug 23 '13 at 10:07
  • The only problem with this option is if there is an array of elements of stylesheets. Be sure to set up a for loop to look for the specific one you need like I had to do. – Jester Jun 15 '20 at 17:35
30

I threw together a small library for this since I do think there are valid use cases for manipulating stylesheets in JS. Reasons being:

  • Setting styles that must be calculated or retrieved - for example setting the user's preferred font-size from a cookie.
  • Setting behavioural (not aesthetic) styles, especially for UI widget/plugin developers. Tabs, carousels, etc, often require some basic CSS simply to function - shouldn't demand a stylesheet for the core function.
  • Better than inline styles since CSS rules apply to all current and future elements, and don't clutter the HTML when viewing in Firebug / Developer Tools.
David Tang
  • 92,262
  • 30
  • 167
  • 149
19

Just place the css in a template string.

const cssTemplateString = `.foo:[psuedoSelector]{prop: value}`;

Then create a style element and place the string in the style tag and attach it to the document.

const styleTag = document.createElement("style");
styleTag.innerHTML = cssTemplateString;
document.head.insertAdjacentElement('beforeend', styleTag);

Specificity will take care of the rest. Then you can remove and add style tags dynamically. This is a simple alternative to libraries and messing with the stylesheet array in the DOM. Happy Coding!

collapsar
  • 17,010
  • 4
  • 35
  • 61
tangle sites
  • 199
  • 1
  • 3
  • `insertAdjacentElement` requires 2 arguments in the the major browsers (Chrome, Firefox, Edge), the first of which establishing the position relative to the reference element ([see here](https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement)). Is this a recent change ? (Added 'beforeend' to the answer). – collapsar Jul 16 '18 at 14:24
  • I don't like this solution. Your CSS won't be cached. – StackSlave Nov 06 '20 at 02:00
  • @StackSlave Who cares and why? – Michael Aug 28 '22 at 23:19
18

A function to cope with the cross-browser stuff:

addCssRule = function(/* string */ selector, /* string */ rule) {
  if (document.styleSheets) {
    if (!document.styleSheets.length) {
      var head = document.getElementsByTagName('head')[0];
      head.appendChild(bc.createEl('style'));
    }

    var i = document.styleSheets.length-1;
    var ss = document.styleSheets[i];

    var l=0;
    if (ss.cssRules) {
      l = ss.cssRules.length;
    } else if (ss.rules) {
      // IE
      l = ss.rules.length;
    }

    if (ss.insertRule) {
      ss.insertRule(selector + ' {' + rule + '}', l);
    } else if (ss.addRule) {
      // IE
      ss.addRule(selector, rule, l);
    }
  }
};
8

My trick is using an attribute selector. Attributes are easier to set up by javascript.

css

.class{ /*normal css... */}
.class[special]:after{ content: 'what you want'}

javascript

  function setSpecial(id){ document.getElementById(id).setAttribute('special', '1'); }

html

<element id='x' onclick="setSpecial(this.id)"> ...  
Sergio Abreu
  • 2,686
  • 25
  • 20
  • 6
    This solution uses jQuery – introducing a dependency the size of jQuery for something as simple as this, when the questioner asked for pure Javascript, is bad. – Tom Ashworth Mar 20 '13 at 14:55
  • Although pratically all websites nowadays USE jquery, I will modify it to use pure javascript. – Sergio Abreu May 02 '13 at 01:48
  • 1
    And exactly how does this method gets to change the CSS attributes of the .class[special]:after pseudo element, like, the background color or anything else? – andreszs Dec 21 '14 at 02:01
  • 1
    Thanks @SergioAbreu. By using this with some of my tricks I was able to stop the css keyframe auto run on webpage load. – Shohidul Alam May 19 '22 at 13:24
  • I am happy to have helped @ShohidulAlam – Sergio Abreu May 20 '22 at 23:34
6

One option you could consider is using CSS variables. The idea is that you set the property you want to change to a CSS variable. Then, within your JS, change that variable's value.

See example below

function changeColor(newColor) {
  document.documentElement.style.setProperty("--anchor-hover-color", newColor);
  // ^^^^^^^^^^^-- select the root 
}
:root {
  --anchor-hover-color: red;
}

a:hover { 
  color: var(--anchor-hover-color); 
}
<a href="#">Hover over me</a>

<button onclick="changeColor('lime')">Change to lime</button>
<button onclick="changeColor('red')">Change to red</button>
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • 1
    This solution was by far the easiest route for me. Why deal with altering stylesheets through adding and removing rules? I was using the same logic for my sliders, and it works like a charm on setting dynamic caption (subtitle) font sizes in my video control. – mseifert Sep 16 '22 at 20:38
5

Instead of directly setting pseudo-class rules with javascript, you can set the rules differently in different CSS files, and then use Javascript to switch one stylesheet off and to switch another on. A method is described at A List Apart (qv. for more detail).

Set up the CSS files as,

<link rel="stylesheet" href="always_on.css">
<link rel="stylesheet" title="usual" href="preferred.css"> <!-- on by default -->
<link rel="alternate stylesheet" title="strange" href="alternate.css"> <!-- off by default -->

And then switch between them using javascript:

function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")<i>); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}
TRiG
  • 10,148
  • 7
  • 57
  • 107
  • What if you need to dinamically change the class to a value retrieved by an AJAX request? You cannot create a CSS file now... – andreszs Dec 21 '14 at 01:51
5

There is another alternative. Instead of manipulating the pseudo-classes directly, create real classes that model the same things, like a "hover" class or a "visited" class. Style the classes with the usual "." syntax and then you can use JavaScript to add or remove classes from an element when the appropriate event fires.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
2

As already stated this is not something that browsers support.

If you aren't coming up with the styles dynamically (i.e. pulling them out of a database or something) you should be able to work around this by adding a class to the body of the page.

The css would look something like:

a:hover { background: red; }
.theme1 a:hover { background: blue; }

And the javascript to change this would be something like:

// Look up some good add/remove className code if you want to do this
// This is really simplified

document.body.className += " theme1";  
Nathaniel Reinhart
  • 1,173
  • 1
  • 8
  • 21
0

In jquery you can easily set hover pseudo classes.

$("p").hover(function(){
$(this).css("background-color", "yellow");
}, function(){
$(this).css("background-color", "pink");
});
vasanth
  • 13
  • 4
0

here is a solution including two functions: addCSSclass adds a new css class to the document, and toggleClass turns it on

The example shows adding a custom scrollbar to a div

// If newState is provided add/remove theClass accordingly, otherwise toggle theClass
function toggleClass(elem, theClass, newState) {
  var matchRegExp = new RegExp('(?:^|\\s)' + theClass + '(?!\\S)', 'g');
  var add = (arguments.length > 2 ? newState : (elem.className.match(matchRegExp) === null));

  elem.className = elem.className.replace(matchRegExp, ''); // clear all
  if (add) elem.className += ' ' + theClass;
}

function addCSSclass(rules) {
  var style = document.createElement("style");
  style.appendChild(document.createTextNode("")); // WebKit hack :(
  document.head.appendChild(style);
  var sheet = style.sheet;

  rules.forEach((rule, index) => {
    try {
      if ("insertRule" in sheet) {
        sheet.insertRule(rule.selector + "{" + rule.rule + "}", index);
      } else if ("addRule" in sheet) {
        sheet.addRule(rule.selector, rule.rule, index);
      }
    } catch (e) {
      // firefox can break here          
    }
    
  })
}

let div = document.getElementById('mydiv');
addCSSclass([{
    selector: '.narrowScrollbar::-webkit-scrollbar',
    rule: 'width: 5px'
  },
  {
    selector: '.narrowScrollbar::-webkit-scrollbar-thumb',
    rule: 'background-color:#808080;border-radius:100px'
  }
]);
toggleClass(div, 'narrowScrollbar', true);
<div id="mydiv" style="height:300px;width:300px;border:solid;overflow-y:scroll">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a eros metus. Nunc dui felis, accumsan nec aliquam quis, fringilla quis tellus. Nulla cursus mauris nibh, at faucibus justo tincidunt eget. Sed sodales eget erat consectetur consectetur. Vivamus
  a diam volutpat, ullamcorper justo eu, dignissim ante. Aenean turpis tortor, fringilla quis efficitur eleifend, iaculis id quam. Quisque non turpis in lacus finibus auctor. Morbi ullamcorper felis ut nulla venenatis fringilla. Praesent imperdiet velit
  nec sodales sodales. Etiam eget dui sollicitudin, tempus tortor non, porta nibh. Quisque eu efficitur velit. Nulla facilisi. Sed varius a erat ac volutpat. Sed accumsan maximus feugiat. Mauris id malesuada dui. Lorem ipsum dolor sit amet, consectetur
  adipiscing elit. Sed a eros metus. Nunc dui felis, accumsan nec aliquam quis, fringilla quis tellus. Nulla cursus mauris nibh, at faucibus justo tincidunt eget. Sed sodales eget erat consectetur consectetur. Vivamus a diam volutpat, ullamcorper justo
  eu, dignissim ante. Aenean turpis tortor, fringilla quis efficitur eleifend, iaculis id quam. Quisque non turpis in lacus finibus auctor. Morbi ullamcorper felis ut nulla venenatis fringilla. Praesent imperdiet velit nec sodales sodales. Etiam eget
  dui sollicitudin, tempus tortor non, porta nibh. Quisque eu efficitur velit. Nulla facilisi. Sed varius a erat ac volutpat. Sed accumsan maximus feugiat. Mauris id malesuada dui.
</div>
kofifus
  • 17,260
  • 17
  • 99
  • 173
-4

If you use REACT , There is something called radium. It is so useful here :

  • Add handlers to props if interactive styles are specified, e.g. onMouseEnter for :hover, wrapping existing handlers if necessary

  • If any of the handlers are triggered, e.g. by hovering, Radium calls setState to update a Radium-specific field on the components state object

  • On re-render, resolve any interactive styles that apply, e.g. :hover, by looking up the element's key or ref in the Radium-specific state

Community
  • 1
  • 1
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254