Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS class:hover select all other elements

I can't find how to do this online, how do I go about selecting any elements within a element and apply styles to them. For example:

HTML:

<div class="cont">
   <div class="txt">Hello World!</div>
   <img src="img1.jpg">
   <img src="img2.jpg">
</div>

CSS:

.txt:hover + img {
   display:none;
}

I want that class style to hide ALL images next to it. It only hides ONE image at the moment though...

like image 805
Martyn Ball Avatar asked Jun 06 '26 16:06

Martyn Ball


2 Answers

If you want to hide all succeeding image elements, use the general sibling combinator, ~.

.txt:hover ~ img {
   display:none;
}

EXAMPLE HERE

You were using the adjacent sibling combinator, +, which will only hide the adjacent element.

like image 119
Josh Crozier Avatar answered Jun 08 '26 12:06

Josh Crozier


You can do this in two ways; With CSS-only, and with jQuery:

CSS-way:

.txt:hover ~ img {
   display:none;
}

jQuery-way:

$(function() {

   $('.txt').hover(function() {
      $('img').toggle();
   });

});
like image 30
TheYaXxE Avatar answered Jun 08 '26 10:06

TheYaXxE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!