I understood one thing: The element which i want to show up or disappear from page as a result of mouse event in relation to a link, must be a sub element of that link.
Well, no, it doesn’t HAVE to be a child, not for modern browsers:
<p class=“demand”>Hover me!</p>
<p class=“show”>Hey whoa how’d I get here??</p>
p.show {
position: absolute;
left: -999px;
}
p.demand:hover + p {
position: static;
}
(in this case, we’re assuming there are only two p’s in the body, so the absolute positioning is working to hide the p.show but it’s very sloppy code… it’s also a bad example because it can’t be affected by keyboards, as p’s aren’t focussable… it’s only showing the possibility)
But, IE6 does not understand +, so your last comment makes me think you’ll need to do it ralph’s way because yes, for older browsers the showing thingie has to be a child.
Why did you assign
p a span {left: -9999px;}
instead of display:none?
There’s another good reason to to it ralph’s way: the most popular screen readers (for the blind) won’t read out text that’s display: none (except in special circumstances) and text that’s merely positioned off-screen is completely accessible to them.
So display: none might be an option if you also definitely wanted to hide that text from screen readers… of course, they’re not likely to mouse over your anchor either, so :hover to make that text show would be inadequate.
Sighted people also may not have a mouse. Add :focus to your anchor’s hover statement to make it keyboard-accessible.
p a:hover span, p a:focus span {
position: static;
}
(should do the same thing)
b. Your code didnt work with ie6. How do i make it work with that browser too ?
It should, but IE6 and 7 do sometimes have trouble changing “states” (block, inline etc) with :hover. Though the position: static might work, because that explicitly makes the span NOT position: absolute, and therefore should fall back into whatever its default state (in this case, inline) is.
Adding display: block to an abso-po’d element doesn’t change it from being absolutely positioned (which is already putting the span in block context anyway) so IE might only be seeing new coordinates (top and left).
Sometimes when you’re only changing coordinates of something on :hover, IE might need a “trigger”. If you’ll be having a lot of anchors getting hovered to hide/show stuff, you can use this:
a:hover {
visibility: visible;
}
This code doesn’t actually do anything. The anchors are already visibility: visible by default anyway. But simply stating this seems to make IE accept some defaults from the anchor to the :hover state. Or something. There are a lot of little bugs regarding this and the fixes are always weird: adding a background colour to :hover will do it, or changing the font weight.
Heck I just came back from fixing an IE6/7 bug where an anchor absolutely positioned over an image wasn’t clickable in IE. It was the correct size and in the correct place, but setting a high z-index didn’t help. Only stating a bogus background on it magically fixed it:
a.speciallink {
background: url(#);
}
bleh. IE : (
So anyway, using Ralph’s code, start by removing the coordinates and see if changing to position: static works in IE. If it doesn’t, then go back to ralph’s code, remove the “display: block” (not doing anything) and set a top coord for the default (where the left: -999 is) and before both of those lines of code, see if a:hover {visibility: visible} magically makes it work.