4

Does css include possibility to change other element on hover. Because I'm kinda struggling with simple task. For this example, I want form to appear when you hover <a> tag.

<a id="log" href="#">text</a>
      <form id="form" action="" method="post">
            <input type="text" name="id"/>
            <input type="submit" name="submit"/>  
      </form>

Should css look something like this:

#form{
    opacity:0;
}
#log:hover ~ #form {
    opacity: 1;
}

or:

#log:hover + #form {
    opacity: 1;
}

It really drives me crazy :/ Any suggestions how I could make it work? I don't want to use javaScript if there is other way.

1
  • How about making the form nest in a div and display:block and display:none solution? Oct 22, 2012 at 14:09

2 Answers 2

8
#form {
  display: none;
}

#log:hover ~ #form {
  display:block;
}

#form:hover {
  display:block;
}

You'll need #form:hover so that the form doesn't disappear when you stop hovering the link, but this will not work if you're on a touch device. #log:active ~ #form may work on touch, can't confirm right now.

e.g: http://jsbin.com/etuxab/1/edit

1
  • Thanks @mdk for pointing issue about touch devices. I guess I will use alternative css for them. Thanks for help again ;]
    – Guu
    Oct 22, 2012 at 14:43
5

Write it as -

#form{
    opacity:0;
    filter:alpha(opacity=0); /*For IE8*/
}

#log:hover ~ #form {
    opacity: 1;
    filter:alpha(opacity=100); /*For IE8*/
}

Working Demo

5
  • 1
    @Ark: what 'ugly' CSS? Which part bothers you? Oct 22, 2012 at 14:29
  • 1
    @Ark Filter is used to support opacity in IE8.... do you have any alternative for the same?
    – Dipak
    Oct 22, 2012 at 14:30
  • 1
    who asked for a solution for ie8?
    – Toping
    Oct 22, 2012 at 14:32
  • @Ark the OP has already posted correct answer in his question... IE8 solution is just an assumption considering he must be struggling in IE8
    – Dipak
    Oct 22, 2012 at 14:36
  • 1
    Thanks @Dipaks. I thought I should use ~ or + and I have no idea how I missed them, because I was looking for this all day. Thanks again ;]
    – Guu
    Oct 22, 2012 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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