0

I am trying to make text appear when mouse hovers over another text. FOr this I am using the following code:

<style>
#para1 {
    visibility:hidden;
}
#p2:hover, #para1 {
    visibility:visible;
}
</style>
</head>
<p id="para1">Hello World!</p>
<p id="p2">This paragraph is not affected by the style.</p>

This is however not working. Please suggest.

Mathias
  • 5,642
  • 2
  • 31
  • 46

6 Answers6

1

Using CSS3 properties on hover you you can select next element, This is called the adjacent sibling selector, and it is represented by a plus sign

DEMO

HTML

<p id="para1">Hello World!</p>
<p id="p2">This paragraph is not affected by the style.</p>

CSS

p {
    visibility:hidden;
}
#para1 {
    visibility: visible;
}
#para1:hover + p {
    visibility:visible;
}
Santy
  • 304
  • 1
  • 3
0

Instead of using visibility try operating by display:

#para1{display:none;}
#p2:hover + #para1{display:block;}
AlexPrinceton
  • 1,173
  • 9
  • 12
0

You could use some simple jQuery (I see you have tagged it in this category)

$('#p2').hover(function() {
$('#para1').show
}, function() {
$('#para1').hide
});
Shakesy
  • 335
  • 2
  • 8
0

There is no previous sibling selector in CSS, so you'd have to use JavaScript for this. Try:

$('#p2').hover(
    function(){
        $('#para1').css('visibility','visible')},
    function(){
        $('#para1').css('visibility','hidden')
})

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272
0

Try this 2 rules:

p {
    display:none;
}
#para1, #para1:hover + p {
    display: inline;
}

or this:

p {
    display:none;
}
#para1, #para1:hover + p {
    display: block;
}

If depends what kind of result do you want. You can try with jQuery too.

Luiggi
  • 358
  • 4
  • 12
-1

try:

#p2:hover #para1{
   visibility:visible;
 }

or:

#para1{
   display :none;
  }
#p2:hover #para1{
   display:block;
  }
Yax
  • 2,127
  • 5
  • 27
  • 53