Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a div from another div using CSS

Tags:

html

css

I want to hide a div con1 when i hover div con2 and vice versa. I am able to hide con2 when i hover con1 but can't do the same vice-versa. Why it is not working when i hover con2 to hide con1. Below are the codes:

<html>
  <head>
    <title>Home</title>
    <style type="text/css">
      #con1{
        float: left;
        width: 500px;
        height: 300px;
        background: #f00;
      }

      #con2{
        float:left;
        width: 500px;
        height: 300px;
        background: #808;
      }

      #con1:hover ~#con2{
        visibility:hidden;
      }

      #con2:hover ~#con1{
        display:none;
      }

    </style>
  </head>
  <body>

    <div id="con1">
    </div>
    <div id="con2">
    </div>

  </body>
</html>

Example: http://jsfiddle.net/mendesjuan/s8bbe/

like image 249
Laishram Pilot Avatar asked Mar 23 '26 13:03

Laishram Pilot


1 Answers

I believe this is not possible with the general sibling selector as it only applies to elements after it in the html-structure. See more: http://css-tricks.com/child-and-sibling-selectors/

A possible (althought not especially elegant solution): http://jsfiddle.net/s8bbe/4/

<div id="wrapper">
<div id="con1">
</div>
<div id="con2">
</div>

  #con1{
    float: left;
    width: 500px;
    height: 300px;
    background: #f00;
  }

  #con2{
    float:left;
    width: 500px;
    height: 300px;
    background: #808;
  }

  #con1:hover ~#con2{
    visibility:hidden;
  }

  #wrapper:hover #con1:not(:hover){
    visibility:hidden;
  }
like image 76
KnutSv Avatar answered Mar 25 '26 03:03

KnutSv



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!