How to Create Image Swap Effect using CSS
The image swap effect is a very common thing for those freelance web designers and those working in web design companies. For navigational purposes, it can make menus much easier on the eyes by highlighting the user’s pick. This helps to prevent misclicks, and looks nice as well. When you want to create an image swap effect, the standard procedure is usually to throw in an OnMouseOver event in JavaScript.
Unfortunately, there are a number of disadvantages to this approach, like the fact that a large number of people have JavaScript disabled in their web browser for security purposes. Thankfully, the same effect can be created using standard HTML and a neat trick using CSS.
Here’s how the trick works for CSS. First you’ll create a division in your code with the div tag and give it a class. Inside of the division, you’ll place the image in standard image tags that will be used for when the mouse is not over the designated area. Next, you’ll add a background image to the division’s class using CSS, and this image will be the one that appears when the mouse is over the designated area.
Finally, you’ll add a piece of CSS that changes the display attribute to “block” for the anchor tags within the div class you created, as well as changing the display attribute to “hidden” for when the mouse hovers over anchors tags inside of that division class.
While this is a little tricky to understand conceptually, in practice it’s much easier to see. Here is a sample piece of code that achieves this effect:
Unfortunately, there are a number of disadvantages to this approach, like the fact that a large number of people have JavaScript disabled in their web browser for security purposes. Thankfully, the same effect can be created using standard HTML and a neat trick using CSS.
Here’s how the trick works for CSS. First you’ll create a division in your code with the div tag and give it a class. Inside of the division, you’ll place the image in standard image tags that will be used for when the mouse is not over the designated area. Next, you’ll add a background image to the division’s class using CSS, and this image will be the one that appears when the mouse is over the designated area.
Finally, you’ll add a piece of CSS that changes the display attribute to “block” for the anchor tags within the div class you created, as well as changing the display attribute to “hidden” for when the mouse hovers over anchors tags inside of that division class.
While this is a little tricky to understand conceptually, in practice it’s much easier to see. Here is a sample piece of code that achieves this effect:
<div> <a href="#"> <img src="logo.gif" width="187" height="136" alt="" /> </a> </div>And the CSS to go along with it:
div.nav { height: 187px; width: 136px; margin:0; padding:0; background-image:url("logo-red.gif"); } div.nav a, div.nav a:link, div.nav a:visited { display:block; } div.nav img { width:100%; height:100%; border:0; } div.nav a:hover img { visibility:hidden; }
Comments
Post a Comment