0

Text color wont change on hover once function is used.

I have the following <div>:

<div class="test" id="test_id" onClick="tempFunc()">HELLO</div>

CSS for the above line:

.test {     
    font-family: 'CenturyGothicRegular', sans-serif;
    font-size: 0.8vw;
    color: white;
}

.test:hover {   
    color: orange;  
}

Above all works as expected.

Color of text starts as white, hover changes to orange. When clicked, it opens tempFunc(). In that function this line is passed:

function tempFunc() {   
   document.getElementById('test_id').style.color = "orange";
}

That will change the color to orange fine. When user clicks another button:

<div class= home" id="home_id" onClick="homeFunc()">CLOSE</div>

function homeFunc() {   
   document.getElementById('test_id').style.color = "white";
}

Problem - After that happens the CSS test:hover is completely ignored. Nothing happens when hovering over "HELLO", color remains white.

8
  • 1
    Have you tried setting .style.color = ""; to remove the inline style and default back to what's in the stylesheet? (As compared to explicitly setting inline .style.color = "white";, which overrides the stylesheet?) Commented Oct 3, 2017 at 2:12
  • see css specificity Commented Oct 3, 2017 at 2:13
  • I think may be this css is override from another Commented Oct 3, 2017 at 2:13
  • 1
    I don't need that, you are correct. I see the problem is Javascript overrides CSS Commented Oct 3, 2017 at 2:19
  • 1
    no, it does not. inline style has a higher specificity than any css selector - which, in your case you are adding the inline style through javascript, but it's not javascript that is the problem Commented Oct 3, 2017 at 2:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.