简体   繁体   中英

css on hover change div

I have the following code and basically I am trying to change css of a div that is within another div and can not get it to respond when the div that needs to be changed is within another div outside the context of the hover item.

<style>
    p.hometown:hover > #test  { 
        background: yellow;
    }

#test {   background: blue;}

</style>
</head>
<body>

<p>My name is Donald.</p>
<p class="hometown">I live in Ducksburg.</p>

<p>My name is Dolly.</p>
<p class="hometown">I also live in Ducksburg.</p>
<div id="tt">
<div id="test">dfgfdg</div></div>

I have tried multiple methods such as:

p.hometown:hover #tt+#test  { 
    background: yellow;
}

also

p.hometown:hover > #test  { 
    background: yellow;
}

None of these attempts are working. If I remove div tt then it will work just fine. Problem is I am trying to use this method on squarespace to fix the limitations there. NO JAVASCRIPT AND CAN NOT ALERT HTML css alteration only.

You are almost good BUT you need to first target the parent div. You can also use ~ instead of + to make this working for all the p with class .hometown :

 p.hometown:hover ~ #tt > #test { background: yellow; } #test { background: blue; } 
 <p>My name is Donald.</p> <p class="hometown">I live in Ducksburg.</p> <p>My name is Dolly.</p> <p class="hometown">I also live in Ducksburg.</p> <div id="tt"> <div id="test">dfgfdg</div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM