0

i have a div which is box and a 'p' element whose opacity is set to 0. when i hover over the div i want the 'p' elements opacity to change to 1. i have the following code . its looks proper to me but its not working. i could not figure out the problem with it. can some one help me. thanks in advance.

HTML:

<p class="se">Hover over the div element below</p>
 <div class="box"></div> 

css:

.se{
position: relative;
color:red;
opacity:0;
}

.se{
-webkit-transition: opacity 2s;
transition: opacity 2s;
} 
.box{
position: relative;
left: 400px;
width: 100px;
height: 100px;
background: red;  
}

box:hover + .se{
opacity:0;
}

jsFiddle http://jsfiddle.net/2f1k5yq4/

1
  • + is the adjacent sibling selector. It selects a sibling which immediately follows it.
    – Abhitalks
    Commented Dec 13, 2014 at 8:23

2 Answers 2

3

CSS selector +

Any element that is the next sibling of a previous element (that is: the next child of the same parent)

.box {
  position: relative;
  left: 400px;
  width: 100px;
  height: 100px;
  background: red;
}
.se {
  color: red;
  opacity: 0;
  position: relative;
  transition: 1s linear;
}
.box:hover + .se {
  opacity: 1;
}
<div class="parent">
  <div class="child">
    <div class="box"></div>
    <p class="se">Hover over the div element below</p>
  </div>
</div>

8
  • thank you. your answer worked. one small doubt though. when i make these two elements as children of another div which inturn is a child of another div , this solution does not work. maybe i'm doing something wrong with my selectors. my doubt is, is there another way to trigger the transition when these divs are made as children to another div. thanks in advance.
    – Prem
    Commented Dec 13, 2014 at 11:04
  • your need to target the class properly @Prem Commented Dec 13, 2014 at 11:08
  • my html is as follows:
    – Prem
    Commented Dec 13, 2014 at 11:12
  • my html is as follows: <div class="myworks"> <p class="wot">hello world</p> <p id="practice">hello world</p> <section> <nav class="cl"> <a class="wo1" href="#">WEB DESIGNING</a> <a class="wo2" href="#">3d modeling</a> <a class="wo3" href="#">illustrations</a> <img class="wook1" src="img/bg3.jpg"/> <img id="woo2" class="wook2" src="img/king.png"/> </nav> </section> </div> when i hover on the class "wo1" i want the image of class "wook1" to change its opacity to 1.
    – Prem
    Commented Dec 13, 2014 at 11:20
  • my css is as follows. .wook1{ -webkit-transition:opacity 2s; transition: opacity 2s; opacity:0; } .wo1:hover +.wook1{ opacity:0; }
    – Prem
    Commented Dec 13, 2014 at 11:22
0

This is actually a kind of annoying problem with just CSS. I'd prefer using Javascript, especially if you already have jQuery loaded into your site.

But, that said, this can sort of be done in CSS. I'm going to assume you just want to show/hide an element, so I'll use display for the example rather than opacity, so modify as needed to use a transition.

The first problem you have is that you box:hover rather than .box.hover. The second problem is a bit more annoying. Abhitalks is correct that +, the adjacent sibling selector, only selects the next sibling, so you can't have the text above the div that will show it. If, however, you switch the order, moving the div above the text, it works. You could use some clever positioning, floating, etc. in order to make the order of their appearance different than their order in the markup, if you wanted.

.se{
  display: none
}

.box{
  width: 100px;
  height: 100px;
  background: red;  
}

.box:hover + .se{
  display: block;
}
<div class="box"></div> 
<p class="se">Hover over the div element below</p>

1
  • Sure thing! You should accept it if it's correct and useful.
    – NSU
    Commented Dec 13, 2014 at 14:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.