I am having trouble trying to figure out how I can make a text box change content depending on what link is being hovered over. I can get it to work with which ever one is closest to the div but the other links seem to have no effect. I do not however want to have text being inserted in between the links nor do I want to create several text boxes. My main goal is to have the links always in the same place and when you hover over them a text box underneath will display the correct content.
Here is some code and a JSFiddle I threw together to help better illustrate my question: JSFiddle
HTML:
<a class="one" href="#">one</a>
<a class="two" href="#">two</a>
<a class="three" href="#">three</a>
<div class="element">hello</div>
CSS:
.element {
display: none;
}
a:hover + .element {
display: block;
}
Read more about the content property here: http://www.w3schools.com/CSSref/pr_gen_content.asp As i read it, you can not use it with hover. It also only manipulates content of the current htmlelement.
With jQuery you can solve this. See http://jsfiddle.net/fhD3A/
$("a").hover(function() { $('.element').html('art'); });
You need the follwing HTML mark-up:
and then apply the following CSS:
See demo: http://jsfiddle.net/audetwebdesign/p7WUu/
The CSS is slightly repetitive but it works and no JavaScript required.
The sibling combinator (~) is needed to pick out sibling elements, see reference.
Reference: http://www.w3.org/TR/selectors/#sibling-combinators
<div class="element special">hello</div>
Use~
instead of+
a:hover ~ .element.special { display: block; }
~
sibling combinator is similar toX + Y
, however, it's less strict. While an adjacent selector(X + Y)
will only select the first element that is immediately preceded by the former selector,~
is more generalized.