Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS :hover Within an 'absolute' Positioned Element [closed]

Tags:

css

I've got a friend, mrOverAbsolute, who wants to sit on top of another friend, mrAbsolute. Neither one seems to complain. I've got another friend, who responds to "mrHover" that wants to use a sprite. mrHover is only happy when the big dogs, mrHover and mrOverHover aren't so absolute.

Here's the drama:

<style>
div#mrOverAbsolute
{
  z-index:1;
  /* ... and some other stuff */
}
div#mrAbsolute
{
  position:absolute;
  z-index:-1;
  /* ... and some other stuff */
}
a.mrHover
{
  background:url('mrImage') 0 0;
}
a.mrHover:hover
{
  background-position:0 25;
}
</style>

<div id="mrOverAbsolute"></div>
<div id="mrAbsolute">
  <table>
    <tr>
      <td><a href="" class="mrHover"></a></td>
    </tr>
  </table>
</div>

Okay okay -- friends aside -- if I remove "position:absolute;", the sprite works. Otherwise, Chrome ignores the fact that I've declared a ":hover" in CSS.

Any suggestions?

like image 927
user2280110 Avatar asked Apr 23 '13 00:04

user2280110


People also ask

Does float work with position absolute?

Generally speaking, float is a relative positioning statement, since it specifies the position of the element relative to its parent container (floating to the right or left). This means it's incompatible with the position:absolute property, because position:absolute is an absolute positioning statement.

Why Hover is not working on div?

You might have linked your stylesheet to your HTML file improperly. Some other CSS in the context of your project may be overriding the piece that you've given here. You might be running into browser compatibility issues with the :hover selector or something else in your code that is breaking the styling.

Does position absolute work with position fixed?

Fixed positioning is really just a specialized form of absolute positioning; elements with fixed positioning are fixed relative to the viewport/browser window rather than the containing element; even if the page is scrolled, they stay in exactly the same position inside the browser window.


1 Answers

There is no problem with :hover on absolutely positioned elements.

The problem is that you place the #mrAbsolute element behind the #mrOverAbsolute when you set its z-index:-1 so the mouse events are intercepted by #mrOverAbsolute.

like image 67
Gabriele Petrioli Avatar answered Oct 04 '22 22:10

Gabriele Petrioli