How to apply style on child tag of child tag? [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to apply style on child tag of child tag? [duplicate]



This question already has an answer here:


<div class="product">
<div class="image">
<div class="mainimage"></div>
<div class="secondimage"></div>
</div>
<div class="productdescription"></div>
</div>



At the mouse hover on class productdescription I want to apply display: block on class secondimage. How can I do it?


productdescription


secondimage



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





You can't do it without javascript, because the trigger element .productdescription have to be a parent of the element .secondimage.
– thhan
Aug 10 at 14:25




2 Answers
2



I guess you'll need some JavaScript and jQuery to do this.



After putting an id in each div you want to interact:


$('#productDescription').hover(function()
$('#secondImage').css("display","block");
);



I don't know if it's what you looking for, but it works.



There isn't a way to do this in pure CSS with the structure you have, as a general rule CSS avoids any selectors that 'travel up' the DOM node tree to avoid complexity. However if you swap the order like so:


<div class="product">
<div class="productdescription"></div>
<div class="image">
<div class="mainimage"></div>
<div class="secondimage"></div>
</div>
</div>



Then you should be able to do the following using the Adjacent Sibling selector


.productdescription:hover + .image .secondimage
display:block;




.productdescription:hover + .image .secondimage
display:block;
color:red;


<div class="product">
<div class="productdescription">Product Description</div>
<div class="image">
<div class="mainimage">Main Image</div>
<div class="secondimage">Second Image</div>
</div>
</div>



Alternatively, if you need to keep the structure, then a bit of JavaScript/jQuery is required.




$(".productdescription").hover(function()
$(this).parent().find(".image .secondimage").toggleClass("hoverActive")
);


.secondimage.hoverActive
display:block;
color:red;


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="product">
<div class="image">
<div class="mainimage">Main Image</div>
<div class="secondimage">Second Image</div>
</div>
<div class="productdescription">Product Description</div>
</div>

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS