Article

Accessible Header Images With CSS And XHTML

Page: 1 2 3

Overflow

So far, the code will work fine as long as the header text is short enough to fit underneath the header image.

When text is too long to fit inside its container, it is said to “overflow" out of it. We don't want to see this text, so we have to hide it. In fact, this is exactly the CSS we need:

h1 { overflow: hidden }

If you only worry about Windows browsers, the story ends here. Everyone else has one more crime to commit. With this naked code, Internet Explorer 5 for the Mac will hide the whole thing. Not just the overflow—the whole thing is gone. Poof!

This is a problem specific to IE5 for Mac. Thankfully, a simple hack will let us hide the line above from IE5 for Mac, and give it to everyone else.

What shall we give those Mac users instead? Simple: we'll use Mike's solution from before. Here's the two-part hack:

/* IE5 Mac Hack \*/  
h1 { overflow: hidden; }  
/*/  
h1 { text-indent: -100em; }  
/* End Hack */

The first line is given to all browsers except IE5 for Mac. On the other hand, IE5 for Mac only sees the second line. Learn more about the hack at Sam Foster's site.

Summing Up

We started with perfect XHTML and perfect CSS. Then we made concessions to browser support.

Now we have two methods for styling headers fully using CSS, one with clean XHTML, and one that still works without images.

I'll leave it up to you decide which is more important for your site.

XHTML-Centric header

<h1 id="album">My photo album</h1>    
#album {  
 width: 300px; height: 75px;  
 background-image: url(header.gif);  
 background-repeat: no-repeat;  
}  
h1 { text-indent: -100em; }

User-Centric Header

<h1 id="album"><span></span>My photo album</h1>    
/* repeated code per header */  
#album, #album span {  
 width: 300px; height: 75px;  
 background-image: url(header.gif);  
 background-repeat: no-repeat;  
}  
#album span { margin-bottom: -75px; }  
/* non-repeated code */  
h1 span {  
 display: block;  
 position: relative;  
 z-index: 1;  
}  
/* IE5 Mac Hack \*/  
h1 { overflow: hidden; }  
/*/  
h1 { text-indent: -100em; }  
/* End Hack */

Learn More

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: