Posted on November 27, 2008 - 9 Comments

Link with CSS Background Overlay

While I’m visiting Komodomedia, there is an interesting small unique thing I ever found. It’s a Last.fm bar on the footer of page which is decorated as the CDs! Is it fancy though? Did you wondering how to make it? No, he didn’t draw it on Photoshop. It’s a CSS trick, I don’t know the exactly trick used by Rogie and thanks to him, but all we need it’s an empty span floating over the cover art and apply the background image overlay. This trick has been tested on most popular browser: Firefox, Safari, Opera, Internet Explorer 7 even Internet Explorer 6. Continue to read the rest of this article to find out how and if you already knew how to do it or just have a better solution, feel free to leave a comment.

linkwithcssoverlay_sshoot

Check out the demo or Download demo ZIP

How does this work?

illus_cdoverlayThe trick is “simple enough”. Basically we are just doing PNG alpha transparency overlay over the cover art. So the first thing we need to find the right CD image or you may also draw by your own. Don’t forget to make it transparent enough until you can see the cover art behind the CD, whatever the size you like but keep it feet the cover art. I have drawn it and the PSD also included in Download demo ZIP, so you can change the background color as you need.

The Markup

It’s just like usual floating unordered list block, but add a <span> before after the image add a &nbsp; inside to make it semantic.

<li>
  <a title="Dido: No Angel" href="http://www.last.fm/music/Dido/No+Angel">
    <img src="http://userserve-ak.last.fm/serve/126/16070125.jpg" alt="cover" width="80" height="80" /><span> </span>
  </a>
</li>

We use that span in order to overlay the cover art with the PNG background.

Styling

The key is a { position: relative; } and a span { position: absolute; }.

#music a {
  position: relative;
}
#music a span {
  background: url(images/cd.png) left top no-repeat;
  position: absolute;
}

You may check out the demo.

PNG color correction on IE 6/7

Since IE considered gamma for PNG, the image and background color looks different. I have tried the scripting way to fix it but doesn’t work so the next solution is TweakPNG. Just open your file with TweakPNG, remove the gamma metadata and save.

tweakpng

Make it work on IE 6

You may leave this section if you won’t care about IE6 user.

1. PNG alpha transparency

Here we go again the legendary lack of IE 6, the PNG alpha transparency on PNG-24. I’m using IE PNG Alpha Fix v 1.0 by TwinHelix and add the following piece of code. Well simply just add this piece of code to the Stylesheet:

<!--[if lt IE 7]>
<style type="text/css">
#music a span {
  background: none;
  filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/cdoverlay.png', sizingMethod='crop');
} 
</style>
<!--[endif]-->

In case you want to use other script, I tried few jquery plugins in purpose for PNG fix like jquery.pngFix.js by Andreas and IE PNG Fix by Paul but somehow it don’t work at all while handling the background on mouse over event. The last one is jQuery.ifixpng by Kush M and it’s work!

Why if block instead of * html #music a span { … }? The CSS will be a valid one.

2. Handling the mouse over event

IE 6 doesn’t recognize the Element hover (mouse over) event except for a Link, so required to add javascript stuff a little more CSS tricks for it. I’m done with helped by jQuery the great Paul Boutin’s advice. Here is it the detail explanation:

First we make the <a> and <span> have a fixed width as the background we have.

#music a,
#music a span {
  display: block;
  width: 80px;
}

Make the <a> have a fixed height to feet the background we wish to overlaying and mark the height property as important to force the element feet into the height, optionally we may also add overflow property as hidden to avoid element overflow.

#music a {
  height: 80px !important;
  overflow: hidden;
}

Here we make the <span> fixed to the “real” background height and place it absolutely at the top left of parent element (<a>).

#music a span {
  top: 0;
  left: 0;
  height: 160px;
}

Now we’re about to handling the hover event. At the a:hover we need to set the background position to left-bottom, so when mouse over triggered the background move to bottom position and display our second background. But we need also shift the span position to blind spot area in order to make our second background visible. See how important the previous steps?

#music a:hover {
  background-position: 0 bottom;
}
#music a:hover span {
  top: -80px;
}

This is the illustration about what happened above.

hover_handling_illustration

Credit to Paul Boutin, thanks.

The last thing

Another IE problematic, the cursor won’t change to hand when you move to the CD link so we need to add another line to the stylesheet

<!--[if IE]>
<style type="text/css">
#music a span {
  cursor: hand;
}
<!--[endif]-->

Limitations

  • IE PNG hack and javascript stuff is required to make it fully work on IE 6.
  • TweakPNG is required to remove the gamma metadata in order to archived the color correction on IE.
  • For the best result you need to discovering over the image transparency before applying the background image overlay.

I’m apologized about my poor english. Just leave a comment if you have a question, I will try my best to answer it.

 

Tags: , , ,

 

9 Comments

  • Paul Boutin says:

    This is a very cool effect thanks for sharing. I will however help you simplify the code involved. the JavaScript is not needed as long as the span is inside the “a” tag, you can access the span on the “a” tags hover event in CSS like this… a:hover span { background:url(); }

  • Hi Paul, thanks for the suggestion. I have tried your trick but somehow it doesn’t work at all, still the ironic of IE 6.

  • I’m creating my png images using GIMP and there is no gamma metadata, so i don’t need TweakPNG.

    btw, thanks for the iepngfix links from TwinHelix, it works

    ^_^

  • Sound nice Heru, you may just place your image directly and save your time :D

  • Paul Boutin says:

    Firman, The reason its not working in IE is because the span is positioned absolute and it is now on top of the a tag, so the “a” tag never gets the hover event. I tried it again without using positioning and I got it to work in all browsers. Put your span after the image tag in the source code and set the spans top margin to -80px. This will place the span over the image but not the “a” tag.

  • Hi Paul, I have tried it out but still IE 6 won’t care about it. I’m wondering, can you point me to your source?

  • It could also be done, using a solid background color, which would make it easier to change the desired color, although i must admit it looks nicer with rounded corners :)

    a.overlay:hover span {
    top: -24px; /* height of the used image */
    background-color: #888;
    filter: alpha(opacity=50);
    -moz-opacity: .5;
    opacity: .5;
    cursor: hand;
    }

  • …>HTML & Web Design December 29 How to customize Graphical Links with CSS Background Overlay
    resource: http://firmanw.com/link-with-css-background-overlaydemo:

  • …”category”>usefull links January 1 How to customize Graphical Links with CSS Background Overlay
    resource: http://firmanw.com/link-with-css-background-overlaydemo:

 

Leave a comment

 


Required


Required, but will not be published