5

My div has class:

.cls { background-color: #ff0000; }
.cls:hover { background-color: #0000ff; }

When with javascript I do:

mydiv.style.backgroundColor = "#555555";

It works but the hover doesn't work anymore!

I haven't found much information about this behavior on the net, is it normal?

How to fix could be another question but if you want to tell...

3
  • can youm add your html code? Commented Nov 22, 2016 at 5:35
  • 1
    it's normal - see this documentation to understand why Commented Nov 22, 2016 at 5:37
  • one easy fix is to set !important flag on the hover background color Commented Nov 22, 2016 at 5:38

6 Answers 6

10

As you are giving background-color from javascript so it is applied as inline style and if you want to give hover effect then apply !important to it.

.cls { background-color: #ff0000; }
.cls:hover { background-color: #0000ff !important; }
Sign up to request clarification or add additional context in comments.

Comments

3

Very interesting though it is weird behaviour.

var elem = document.getElementsByClassName("cls")[0];
elem.style.backgroundColor = "yellow"; // It'll become inline property
.cls {
  width: 100px;
  height: 100px;
  background-color: #ff0000;
}

.cls:hover { background-color: #0000ff; }
<div class="cls"></div>

When we are applying background-color from Javascript, it'll become inline property and taking high priority over other properties and even overriding background-color from hovereffect.

For a POC, look at this

.cls {
  width: 100px;
  height: 100px;
   background-color: #ff0000; }
.cls:hover { background-color: #0000ff; }
<div class="cls" style="background-color: yellow"></div>

Now, i'm applying background-color from inline and here also it is taking high priority over css styles.

Solution for this cause is, adding !important on hover

var elem = document.getElementsByClassName("cls")[0];
elem.style.backgroundColor = "yellow";
.cls {
  width: 100px;
  height: 100px;
  background-color: #ff0000;
}

.cls:hover { background-color: #0000ff !important; }
<div class="cls"></div>

UPDATE

As @Mr_Green said, adding !important property is not best practice, instead we can add one more class will also solves your problem.

var elem = document.getElementsByClassName("cls")[0];
elem.classList.add('secondary');
.cls {
  width: 100px;
  height: 100px;
  background-color: #ff0000;
}

.secondary {
  background-color: #000000;
}

.secondary:hover {
  background-color: #0000ff;
}
<div class="cls"></div>

Hope this helps :)

2 Comments

Yes, it is strange for me because I always thought that pseudo class styles will precede the inline styles. The solution would be not to go with !important for pseudo class styles but to add a separate class dynamically which holds the styles.
@Jackt classList is not supported by IE9 and older
0

I think it will work that giving class 'cls2' to mydiv in Javascript and

.cls:not(.cls2) { background-color: #ff0000; }
.cls2 { background-color:#555555; }
.cls:hover { background-color: #0000ff; }

Comments

0

maybe add onmouseenter, onmouseleave event listener to implement the hover.

Comments

0

Changing the background color using javascript will overwrite that element's style.

if you really want to add the hover effect using javascript you can refer to this post: Change :hover CSS properties with JavaScript

I recommend you to use JQuery for these type of codes.

Comments

0

check the snippet . its working

var myDiv = document.querySelectorAll('.cls')[0];
myDiv.style.background='green'
.cls{ background-color: #ff0000; height:100px; width:100px; }
.cls:hover { background-color: #0000ff !important; }
   

 <div class="cls">
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.