CSS Hacks for IE PC & Mac
I have been trying to find a good CSS Image Replacement Technique for a while now. The things you think are really simple end up being really difficult thanks to IE. What I wanted from this Image Replacement Technique was the following:
- Image when user has CSS enabled
- Heading text (h1) when CSS is disabled
- Link both Image or Heading text to home.
- Be centered in the header box
Now, this may seem simple, but to get all browsers to play along nicely was a nightmare. It now works in IE PC, Netscape PC, Firefox PC, Safari Mac, Opera Mac, Camino Mac, Firefox Mac, and looks a little quirky but ok in IE Mac.
So here is the HTML:
<div id="header"><h1><a href="http://sam.brown.tc" title="Home">sam brown tc</a></h1></div>- Download this code: /code/iecsshacks_html.txt
and the CSS:
#header {background: transparent url(/img/header.jpg) center no-repeat;height: 120px;text-align: center;}/* IEMAC Backslash Hack \*/#header h1 {background: url(/img/logo.gif);height: 26px;overflow: hidden;text-indent: -100em;width: 193px;position: relative;top: 45px;left: 271px;_left: 0;}/* End Hack */#header a {color: #fff;display: block;height: 26px;}- Download this code: /code/iecsshacks_css.txt
Note: The IE Mac hack breaks my code colour coding.
Now, because of the text-align: center; in the #header div, IE PC puts the relative positioned #header h1 271px left of the center, instead of left of the div like every other browser. Bad IE PC So to fix that we have an _left: 0; which IE PC reads but no-one else, thank goodness.
Then IE Mac, it doesn’t like displaying the logo as a background image and thus leaves no logo and no link in the header, great! Bad IE Mac So everything in the #header h1 is placed inside a backslash comment hack which IE Mac can’t read. Thus it just leaves the heading text centered at the top of the div, good enough for me!
With IE now fixed, the Image Replacement Technique does the rest, it offsets the heading text way off to the left so you can’t see it, places the logo in the background centered on the header, and creates a link, the same size as the logo right on top of it.
So when CSS is enabled, the logo is a link and displayed in the header, and when CSS is disabled the heading text is a link home. Hooray, but none of that was necessary if IE just did its job properly!