tags:

views:

199

answers:

4

How can I achieve the affect like here on SO when you hover over a comment:

  • an arrow up to vote up
  • a flag to mark that message
  • if you are the comment author you get option to delete as well

How can I make links and images like that show when hovering over a DIV or even a table cell?

+1  A: 
div:hover {
    background-image:url('arrow.gif');
}
Dave Markle
How would I make that link as well then?
jasondavis
Instead of using a <div>, you could use the CSS selector a:hover and set it's width, height, and background image. You also would need to set display:block; on it, since <a> tags are normally inline elements.
Dave Markle
+3  A: 

Try this:

.comment .button {
   visibility: hidden;
}  

.comment:hover .button {
   visibility: visible;
}

Assuming your HTML is something like this:

<div class="comment">
  <a ...><img class="vote button" ...></a>
  <a ...><img class="flag button" ...></a>
  <!-- #if author -->
  <a ...><img class="delete button" ...></a>
  <!-- #endif -->
  <span class="comment-text">...</span>
</div>


Andrew noted that this pure CSS solution won't work in IE6. And as Noel pointed out, hovering just isn't an option in mobile browsers. You can use progressive enhancement to have the buttons always visible in those cases.

<style type="text/css" media="screen">

.comment .button {
   visibility: hidden;   
}  

.comment:hover .button {
   visibility: visible;
} 

</style> 


<!--[if lt IE 7]>  
<style type="text/css" media="screen"> 
.comment .button {
  visibility: visible; 
}   
</style>
<![endif]-->

The screen media type should cause the styles to only apply to full-screen browsers (not mobile devices).

IE6 will understand the first style, making the buttons hidden, but not the second, making them visible again on hover. The third style is in a conditional comment, which non-IE browsers and IE7+ will ignore. It overrides the first style, making the buttons visible always.

Patrick McElhaney
Thanks that helps a lot, it works greta in chrome but in firefox, it always shows the buttons, anyway to fix that?
jasondavis
What version of Firefox? Works for me on 3.5.2 / Mac.
Patrick McElhaney
weird I have the same version but on windows and it does not work for me on FF, I even tried putting your example on it's own page to verify no interference but still won't work =(
jasondavis
I have 3.0.11 in Windows, and it works there. A friend just verified it on 3.5.1/Win. Here's the complete file I'm using to test: http://pastie.textmate.org/578735
Patrick McElhaney
A: 

The key to what you are trying to do -- as I think the other answers are saying-- isn't to create the content on hover, but to make it "visible" on hover. It's always there, just not in a way the user can see or interact with. So you'd have something like:

 <div class="vote_arrow">
     <a ...>Clicking on me is fun</a>
 </div>

and then a CSS rule like:

 .vote_arrow a {
     display:none;
  }

  .vote_arrow:hover a {
      display: block;
  }

Be aware, though, that this method requires that the user have CSS turned on. Make your hidden content set up in such a way that if I have CSS off, the links still make some amount of sense.

Anthony
A: 

Consider the following HTML:

<div class="special">
    <div class="links_holder">
        <a class="flag" title="Flag" href="flag.html">Flag</a>
    </div>
    <div class="content">
        Hello, this is my content!
    </div>
</div>

You can use the following CSS to hide the links:

div.special div.links_holder {
    float: left;
    width: 16px; /* For a 16x16 link image */
    margin: 0 4px 0 0; padding: 0;
    visibility: hidden;
}

div.links_holder a.flag {
    display: block;
    width: 16px; height: 16px;
    overflow: hidden;

    /* Move the text out of the way 
       It's there for screen readers */
    text-indent: -9999px; 
    background: url('flag.gif') top left no-repeat;
}

div.special:hover div.links_holder {
    visibility: visible;
}

Note that this will not work in IE6 since IE6 and below only supports the :hover pseudo-tag on <a> tags. In which case you'll need to revert back to a JavaScript solution. Example with MooTools:

$$('div.links_holder a.flag').each(function(el) {
    el.addEvents({
        'mouseenter': function() {
             el.addClass('hover');
         },
         'mouseleave': function() {
             el.removeClass('hover');
         }
    });
}, this);
Andrew Moore