Results 1 to 2 of 2

Thread: Trying to get an image to hover over an image

  1. #1
    Join Date
    Feb 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to get an image to hover over an image

    I have been looking numerous places all over the internet, but I can't seem to find the answer. I am trying to find a way using CSS (NO JAVASCRIPT) to have an image hover over another image. They will be two different links.

    I have attached an image to better explain what I'm trying to do:


    Any help with this would be greatly appreciated, I'm going crazy.

  2. #2
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Finished Product: Layered Links (CSS)


    First, it's important not to get overcomplicated with the markup. If you organize things correctly, you should be able to achieve your goal through CSS. Most of the timee the bare minimum is all that is needed.

    In this case this means:
    1) A background image
    2) A "play" icon
    3) A container div to house them both in.

    HTML Code:
    <div id='container'>
    	<a href='#'><img class='image' src='image.jpg'></a>
    	<a href='#'><img class='play' src='play.jpg'></a>
    </div>
    Now the CSS:
    1. Hide the Play Button
    Code:
    #container 
    { 
    	width:480px;  /* Width of image */
    	height:272px; /* Height of image */
    	padding:0px;
    	overflow:hidden;
    }
    This makes the container div the exact size of the image. Since the image is on top of the play button, the play button is not shown (but does exist beneath the image).

    2. Set an :hover state to the image to display the play button when we hover over the image. Basically, we're going to add a negative margin to bring up whatever is underneath:

    Code:
    img.image 
    { 
    	margin:0px;
    	padding:0px;
    }
    a:hover img.image 
    { 
    	cursor:pointer; /* Hand cursor so people know it's a link */
    	margin-bottom:-32px;  /* Remove margin thereby showing the icon */
    	*margin-bottom:-36px;
    }
    Note: I did some tweaking for IE. The bottom margin is the height of the play button plus a few to allow for a margin.


    3. If you test now, it won't do anything. Even with a negative margin, the play button can only move up as high as the element above it will allow. In other words, the play button can't overlap, unless we change it's positioning

    Code:
    img.play 
    { 
    	text-align:right; 
    	position:relative; 
    	top:0px;
    	float:right;
    	margin-right:3px;
    }

    4. Now, when you hover over the image, the play button should appear. But if you mouse onto the play button, it starts flickering. This happens because once you mouse over the play button, you're no longer hovering over the image. Since you're not hovering over the image, the negative margin dissapears. And as soon as that margin dissapears, you're back on top of the image, which again displays the play button. And so on, and so forth...

    So to fix that, we set a top offset to the hover state of the play button.

    Code:
    a:hover img.play 
    { 
    	cursor:pointer; 
    	top:-32px;  /*  Increase top offset to account for margin differences */
    	*top:-36px;
    }

    That's it...it should work now.


    Full code:
    HTML Code:
    <html>
    
    <style>
    body { text-align:left; background:black; }
    img { border:none; padding:0; margin:0; }
    
    #container 
    { 
    	width:480px;  /* Width of image */
    	height:272px; /* Height of image */
    	padding:0px;
    	overflow:hidden;
    }
    
    
    img.image 
    { 
    	margin:0px;
    	padding:0px;
    }
    a:hover img.image 
    { 
    	cursor:pointer; /* Hand cursor so people know it's a link */
    	margin-bottom:-32px;  /* Remove margin thereby showing the icon */
    	*margin-bottom:-36px;
    }
    
    img.play 
    { 
    	text-align:right; 
    	position:relative; 
    	top:0px;
    	float:right;
    	margin-right:3px;
    }
    a:hover img.play 
    { 
    	cursor:pointer; 
    	top:-32px;  /*  Increase top offset to account for margin differences */
    	*top:-36px;
    }
    
    </style>
    <body>
    
    
    <div id='container'>
    	<a href='http://www.designsbyvishal.com'><img class='image' src='image.jpg'></a>
    	<a href='http://www.google.com'><img class='play' src='play.jpg'></a>
    </div>
    
    </body>
    </html>
    Last edited by Medyman; 02-02-2008 at 07:59 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •