bertdesign.de
music, webdesign, stuff-
The problem:
I want to add a hover effect to some images. These images aren’t background images, they are images of different people. Anyway, I’d like to use the idea of css sprites. Here, I want to “translate” this technique for using regular images instead of background-images. The reason to do this is simple: I don’t need one picture for layout reasons, I need many pictures to behave in the same way — changing color & border in a certain way. By the way, the images will be links, too
So let’s get started:
The HTML code:
<div> <a href="#"> <img title="”Mr. X" src="”the_image.jpg" alt="”Mr. X" /> </a> </div>
There are three main elements included:
- A div with the class “member”
- The link
- The image itself
As you can see, the link contains the image, the div contains these two. The image already contains the states I need: On the left side a b/w-picture with a green border, on the right side the colored one with a yellow square added.
Pretty boring without CSS if you ask me. Let’s move on to the CSS-part:
Attention: The different px values are matching the pixel size of the image, as commented in the CSS file. You have to fit these to your needs.
div.member { width: 210px; /* 50% of image width */ height: 200px; /* image height */ overflow: hidden; } div.member a { text-decoration: none; display: block; width: 420px; /* 100% image width */ } div.member a img { border: 0; } div.member a:hover { margin-left: -210px; /* as above, 50% of image width. Watch out: negative value! */ }At first, the surrounding div gets a fixed width, which is exactly 50% of the images width. The height matches the image height.
Setting “overflow: hidden;” causes only the left side of the image to appear.
Now, the link-element is styled. “text-decoration: none;” prevents the underline (as you might know…). Far more important is the next step: The link element turns into a block-element with “display: block;”. Now you can add a fixed width to this element. As you see, the width is exactly the image width.
To prevent little pixel errors, the img element gets rid of the border with “border: 0;”.
Finally, the real “action” is done with editing the a:hover state.
Simply add a negative left margin to the picture. This margin again matches 50% of the images width. In this way, the image gets pushed to the left, revealing the colored right side.
Result:
- minimal usage of CSS & extra markup
- nice hover effect
- should work in most browsers (please comment if you find errors!)
Any comment on this technique is appreciated. If you like this tutorial, let me know!
(There might be tutorials about this technique in other places. I couldn’t find any in a quick google search
This should at least work with FireFox and Safari on a Mac, that’s what I was able to test. IE seems to work too, according to some friends testing it.)
-
Hover-Effekte mit einem Hintergrund-Bild lassen sich einfach finden. Allerdings brauche ich für meine Anwendung kein Hintergrund-Bild, sondern ein einfaches, eingebundenes Bild, da ich nicht für jedes Mitglied ein eigenes Hintergrund-Bild (inklusive eigener #id etc.) anlegen möchte.Was tun? Zuerst einmal der HTML-Code:
<div id="memberarea">
<a href="#">
<img title="Mr. X" src=”das_bild.jpg” alt=”Mr. X" />
</a>
</div>
Wie man sieht, ist das Bild im Link enthalten, der Link in einem umgebenden div. Das eingebundene Bild enthält bereits die beiden gewünschten Zustände . Ohne CSS sähe das Ergebnis noch recht trostlos aus.
Kommen wir also zum CSS-Teil:
div.memberarea { width: 210px; height: 200px; overflow: hidden; } div.memberarea a { text-decoration: none; display: block; width: 420px; } div.memberarea a img { border: 0; } div.memberarea a:hover { margin-left: -210px; }Zuerst wird dem umgebenden div eine feste Weite (die Hälfte der Bild-Breite) sowie eine feste Höhe zugewiesen (die Höhe des Bildes). Die Angabe overflow: hidden bewirkt, dass nur die linke Bild-Hälfte angezeigt wird.
Nun wird das Link-Element bearbeitet: Das Unterstreichen des Links wird unterbunden und ganz wichtig: Der Link wird zum Block-Element gemacht. Diesem Block-Element kann ich dann eine entsprechende (nämlich die Bild-) Breite geben.
Dem Bild-Element wird der Rahmen genommen, sieht nun mal schöner aus & verhindert kleine Pixel-Fehler, die durch einen Rahmen entstehen könnten.
Die eigentliche Hover-Action findet im letzten Teil statt: Durch die negative Verschiebung des Links (der sich als Block-Element verschieben lässt) erscheint nun im div der rechte Teil des Bildes. Fertig.
(Vielleicht gibt es ein ähnliches Tutorial schon irgendwo, ich konnte allerdings auf die Schnelle keins finden. Browser-Kompatibilität wurde ebenso nicht getestet, mit FireFox sowie Safari & Mac OS X funktionierts. Windows-Browser kann ich leider gerade nicht testen
Was macht der IE? )
Mag ich! (1)

Frisch diskutiert: