0
<style>
#count2{
visibility:hidden;
}
#count1:hover{
background:#123456;
//how do I change the visibility property of #count2 here?
}
</style>
<div id="count1">My visible element</div>
<div id="count2">My flickering element</div>

My question is clear and might be little weird though. How do I change the visibility property of #count2 to true when somebody hovers on #count1, using only css.

5
  • I'm not pretty sure so I post this as a comment. I think this isn't possible without javascript. Commented Nov 11, 2011 at 13:53
  • When you rollover #count2, you want it to be shown, not hidden? Commented Nov 11, 2011 at 13:53
  • Are you at leisure to use the sibling selector? Commented Nov 11, 2011 at 13:53
  • no i want to manipulate #count2's visibility while hovering on #count1 , is that possible in css, or should i be using jquery? Commented Nov 11, 2011 at 13:54
  • @JennyDcosta - As BoltClock mentioned in his answer, it's possible in CSS, but if you need to support IE6, you'll need some Javascript help. (JQuery is, of course, an option.) Commented Nov 11, 2011 at 14:04

5 Answers 5

8

Since you're modifying two different elements on hovering one of them, you can use a sibling combinator followed by the #count2 selector in a separate rule for modifying that particular element:

#count2 {
    visibility: hidden;
}

#count1:hover {
    background: #123456;
}

#count1:hover + #count2 {
    visibility: visible;
}
Sign up to request clarification or add additional context in comments.

2 Comments

If you're unfortunate enough to have to support IE6, you'll have to go with jQuery...
@JennyDcosta - Which browser are you testing with? This works on FFX 3.5, IE7, IE8 and Chrome... As BoltClock said, if you need IE6 support, you'll need some Javascript help.
3

You'll have to use the + selector, which selects adjacent siblings:

#count2 {
    visibility:hidden;
}
#count1:hover {
    background:#123456;
}
#count1:hover + #count2 {
    visibility: visible;
}

Here's the fiddle: http://jsfiddle.net/Yyr64/


If you have to target older browsers, and you're using jQuery, this is what you gotta do:

var $count2 = $('#count2');

$('#count1').hover(function(){
    $count2.css('visibility', 'visible');
}, function(){
    $count2.css('visibility', 'hidden');
});

...and here's the fiddle for this: http://jsfiddle.net/Yyr64/1/

2 Comments

@Jenny Dcosta - Sure. You'll just have to alter the selectors.
@Jenny Dcosta: Did you check the links Joseph posted? If his links work but your code doesn't, you might want to show us your exact code if it's different from what you posted.
1

The above solutions can be abstracted with the following jsfiddle: http://jsfiddle.net/mousepotatoweb/PVHzK/2/

<style>
[id|="count-1"]{
background:#123456;
}

[id|="count-2"]{
visibility:hidden;

}

[id|="count"]:hover ~ [id|="count"] { visibility: visible;}
</style>

<div id="count-1">My visible element</div>
<div id="count-2">My flickering element</div>

2 Comments

Be careful - :nth-of-type() looks at element type, not attribute. Combining attribute selectors with :nth-of-type() may cause unexpected results.
ye, edited it out - was going a bit nuts with pseudo selectors. Thanks for heads up.
0

count2 would need to be a child of count1 in order to do this via css only.

<div id="count1">
    My visible element
    <div id="count2">My flickering element</div>
</div>

#count1:hover #count2{ display: block; background: #ffff00; }
#count2{ display: none; }

When using css2 though, you can use the + selector as in Joseph Silber's answer

Comments

-1

Use

 display:none;

instead of visibility property.

You can take a look at jquery http://api.jquery.com/show/

1 Comment

using the visibility property will retain the structure of the layout. display: none; will result in elements filling the space as long as the element is set to none. May not be viable for the question asker to use display: none;

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.