Help on Link using image with an hover image

Discussion in 'CSS' started by deepman007, Feb 28, 2008.

  1. #1
    Hi

    How do you create an hover image on a link that uses an image ?

    For example: The link image is the homeup.gif, and hover image is the homedown.gif.

    <a href=" index.html"><img src="home.gif" /></a>

    any clues on how I can implement this using css?

    Thank you
     
    deepman007, Feb 28, 2008 IP
  2. mokujin

    mokujin Well-Known Member

    Messages:
    335
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #2
    Try this:

    <a href="index.html"><img src="firsimage.gif" onmouseover="this.src = 'secondimage.gif';" onmouseout="this.src = 'firstimage.gif'; /"> </a>

     
    mokujin, Feb 28, 2008 IP
  3. matthewbeckman

    matthewbeckman Peon

    Messages:
    140
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can implement a img class if you wanna use CSS. Just add something like
    img.home:hover {add properties here}
    Code (markup):
    and
    img.home {add properties here}
    Code (markup):
    to your css file and specify exactly what you want it to look like when hovered over, and what it looks like normally. Probably just add the background property with a no repeat after it.

    Then add the class to the <img> in the html file by doing
    <a href="index.html"><img src="home.gif" class=home></a>
    Code (markup):
     
    matthewbeckman, Feb 28, 2008 IP
  4. deepman007

    deepman007 Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here's what I did:

    <p class="go"><a href=" index.html" ><img src="home.gif" class="home" /></a></p>

    p.go img.home{
    position: relative;
    top: 0;
    letf: 0;
    }

    p.go img.home:hover {
    background: url(homedown.gif) no repeat 0 0;
    }

    I tried and it doesn't work.
     
    deepman007, Feb 28, 2008 IP
  5. matthewbeckman

    matthewbeckman Peon

    Messages:
    140
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well for starters you spelled left wrong in the normal state properties. Secondly I told you wrong

    .home a:hover {background:home.gif top left no-repeat;}

    Use <a href="link" class="home">
     
    matthewbeckman, Feb 28, 2008 IP
  6. ChaosFoo

    ChaosFoo Peon

    Messages:
    232
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The best way I have found to do this is to create 1 image that has both images in it. I have attached the image so you can see what I am talking about. Notice that the top portion of the image is a bit darker than the bottom.

    Then I use CSS to 'slide' the position of the background image on hover. See the code below:

    
    a.searchbutton {
             width: 63px;
             height:33px;
             background: url(../images/search-button2.png) no-repeat top;
             cursor:pointer;
    }
    
    a.searchbutton:hover {
             background-position: bottom;
    }
    Code (markup):
    Then I use the below HTML.
    
    <a class="searchbutton "></a>
    
    Code (markup):
    REMEMBER: IE 6 doesn't like to apply hover styles to any elements other than the anchor tag.
     

    Attached Files:

    ChaosFoo, Feb 28, 2008 IP
  7. Dan Schulz

    Dan Schulz Peon

    Messages:
    6,032
    Likes Received:
    436
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you're doing what I think you're doing (making an image based menu) that's not going to be the best (or semantically correct) way of going about it.

    Again, assuming that's what you're making, you're going to want to look into an image substitution technique for menus, such as the one used here: http://www.pmob.co.uk/temp/navimagereplace.htm

    Please note that while Paul O'Brien uses EM for the background image container that SPAN is a perfectly acceptable and semantically neutral way of doing this as well (that, and I don't support Mac-IE anyway).
     
    Dan Schulz, Feb 28, 2008 IP
  8. deepman007

    deepman007 Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I'm not doing imaged based Menu (that part has already done). Right now, I just want to add some buttons on the content page and it has to have default button with and hover image. I tried with the css method and some how it does not work.
     
    deepman007, Feb 28, 2008 IP
  9. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #9
    ^ then you might want what Foo said. If this is a form-style button (a submit) then you could use
    <input type="image" src="transparent.gif" width="whatyouwant" height="ditto" />
    input type="image" is nearly identical to type="submit" except it can have an image associated with it. But because you want it to change on hover, you put a transparent placeholder to set the size and then slide a background image which has both your normal and hover states.

    You're using a <p> though, so you want pretty much what Foo did... though I'd be more specific about placement than "bottom" (it depends how you made your image).

    Post your code. It should be easy to figure out what's not working. Post the image as well (or a link to it).
     
    Stomme poes, Feb 29, 2008 IP
  10. deepman007

    deepman007 Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Here's the code for link:

    <a href="gallery.php" class="gallery"><img id="gal" src="images/buttons/galleryUp.gif" border="0" /> 
    Code (markup):
    CSS Code:

    img#gal{
      position: relative;
      top: 10px;
      left: 0px;
      width: 72px;
      height: 19px;
    }
    a.gallery:hover{
      position:relative;
      top: 10px;
      left: 0;
      width:72px;
      height: 19px;
      display:block;
      background-image:url(images/buttons/galleryhover.gif);
      background-repeat: no-repeat;
      
    }
    Code (markup):
    right now the problem is when I move the mouse over the galleryup.gif, the galleryhover.gif appears under the galleryup.gif. Any clue?
     
    deepman007, Feb 29, 2008 IP
  11. ChaosFoo

    ChaosFoo Peon

    Messages:
    232
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Probably because it is a background-image. This means that the image in the <img> tag will be in front of the background-image, because the definition of a background image is that it is in the um...background. :)

    Seriously, if you want to use an image as a button, and apply hover styles, you would be better off using the code I posted earlier. It is quite easy to do once you wrap your brain around what is happening. It took me a while to understand it.

    If you want to see a live example. You can look at www pool-zone com. The search button is styled using this exact process.

    BTW - You only need to define styles that will CHANGE when you :hover.

    Did you define a height and a width in the CSS for the <a> tag? If you have an empty <a> tag, you MUST define a height and width.
     
    ChaosFoo, Feb 29, 2008 IP
  12. deepman007

    deepman007 Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I solved the problem by taking out both <img> tag in the html file and used background images in the css file. the idea was same as Foo, but I used two images. Thank you all for the support.
     
    deepman007, Feb 29, 2008 IP
  13. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Yeah, you can do two images. The advantage to one is that the browser only has to load one. Then you can change the position using background-position: coord coord;

    Google "CSS Sprites Meets Sliding Doors"
     
    Stomme poes, Mar 1, 2008 IP