Hover in DIV1 change DIV2 with CSS

0

If I have a #div1 and a #div2 , and they are siblings , I know that I can use the ~ selector to style #div2 as follows:

#div1:hover ~ #div2 { /* regras */ }

Is there any equivalent selector to stylize #div2 when a #div1:hover event occurs in the following structure?

<div id="pai"> 
    <div id="div1"></div> 
</div>
<div id="div2"></div>
    
asked by anonymous 18.12.2014 / 19:45

6 answers

2

Are using the wrong markers, see this example:

#pai:first-child:hover ~ #div2 {
    background: #999
}
<div id="pai"> 
    <div id='div1'>CONTEUDO 1</div>
</div>

<div id='div'>NADA</div>
<div id='div'>NADA</div>
<div id='div'>NADA</div>

<div id='div2'>CONTEUDO 2</div>

If they are not relatives you can not find, but you can indicate an element that is relative and navigate your children to indicate the hover.     

17.08.2017 / 15:10
3

With javascript it is possible.

Here is an example using jquery:

$("#div1").hover(function() {
  $("#div2").toggleClass("vermelho");
}, function() {
  $("#div2").toggleClass("vermelho");
});
#div1, #div2 {
  width: 100px;
  height: 100px;
  border: solid 1px #000;
}

.azul {
  background-color: #00F;
}

.vermelho {
  background-color: #F00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><divid="pai"> 
    <div id="div1" class="azul"></div> 
</div>
<div id="div2" class="vermelho"></div>
    
18.12.2014 / 21:19
0

Hello,

Currently it is not possible to select the parent of an element via CSS.

All selectors currently available are in the specifications:

For the time being, this behavior is only possible with JavaScript

The Selectors Level 4 Working Draft includes% with% pseudo-class that works the same as the jQuery implementation . Until August 2017, this is not supported by any browser .

    
17.08.2017 / 14:29
0

For the time being it is not possible via css, but you can do this behavior via Javascript according to previous examples. You can change the behavior of an element within the same container, or select sub elements.

#pai div,
#div3 {
  width: 100px;
  height: 100px;
  border: solid 1px #000;
}

.azul {
  background-color: #00F;
}

.vermelho {
  background-color: #F00;
}


#div1:hover + #div2,
#pai:first-child:hover + #div3,
#pai:last-child:hover + #div3
{
  background-color: #0F0 !important;
}
<div id="pai"> 
    <div id="div1" class="azul"></div> 
    <div id="div2" class="vermelho"></div>
</div>
<div id="div3" class="vermelho"></div>
    
17.08.2017 / 15:21
-1

Try:

#div1:hover + #div2 {
    background-color:red;
}

or

#div1:hover ~ #div2 {
    background-color:red;
}

Take a look at:

link

    
19.06.2017 / 19:02
-4

There is. Example

<div id="pai"> 
    <div id="div1"></div> 
</div>
<div id="div2"></div>

<style>
    #div1:hover #div2{
        background-color:red;
    }
</style>
    
28.12.2014 / 13:08