1

I want to do something like this. I have a link inside a parent and I want to change the background of the parent and the link both whenever I click on the link. Is it possible?

Here is my fiddle http://jsfiddle.net/x5m5m035/

a:active {
   background-color: yellow;
}
div{
   background-color: red; 
}

So when I click on the link, parent's background color should also be yellow.

I am sorry if it is too silly to ask.

Thanks

user3745870
  • 321
  • 1
  • 5
  • 13

2 Answers2

2

It's not possible using pure CSS because CSS hasn't parent selector, but you can do that using javascript or JQuery.

Try this JQuery code for instance:

$('a').mousedown(function() {
    $(this).parent("div").css("background-color","yellow");
}).mouseup(function() {
    $(this).parent("div").css("background-color","red");
});

Check JSFiddle Demo

Moshtaf
  • 4,833
  • 2
  • 24
  • 34
0

There's no way to implement if without scripts.

Alex Velickiy
  • 541
  • 1
  • 7
  • 22