views:

172

answers:

3

Linking a background image with CSS is giving me so me issues. They seem pretty simple but I can't quite get around them:

I have list items for my main menu:

<div id="menuContainer">
 <ul id="menu">
 <a href="#"><li id="home" title="Home" alt="Home">Home</li></a>
 <a href="#"><li id="current" title="Current Students" alt="Current Students">Current Students</li></a>
 <a href="#"><li id="prospective" title="Prospective Students" alt="Prospective Students">Prospective Students</li></a>
 <a href="#"><li id="facultyStaff" title="Faculty & Staff" alt="Faculty & Staff">Faculty & Staff</li></a>
 <a href="#"><li id="visitors" title="Visitors" alt="Visitors">Visitors</li></a>
 </ul>

my css sets the li to inline-block and gives defines the id's with a size and background image accordingly. I had to use zoom: 1; and *display: inline; for IE to work and everything shows up fine in IE for that now.

When I use text-indent: -9999px; to remove the text and leave the image, Chrome and Firefox works fine with this. However, in IE the whole li shifts the number of pixels listed.

Finally, In Chrome the entire image is the link, in IE and Firefox only the text is the link so with no text the menu has no function.

Any ideas?

+4  A: 

You are using syntactically incorrect HTML. You can't wrap an <a> around a <li>. While fixing this may not necessarily make your problem go away, it will probably ensure that every browser behaves the same way.

You're not very clear about what you want to achieve, and what your menu looks like. If you want the whole area of the <li> to become clickable, you're probably best off giving the <a> a display: inline-block and fixed dimensions.

If you need more detailed answers, you may want to give us an online example.

Pekka
I formatted the html per your suggestion. I also had the <a> wrapped around the <li> in my footer (however the footer worked fine). Now the footer images no longer link.Basically, I'll i'm trying to do is create an inline menu using <li> and css background.live test site: ignore the black borders they are for checking alignment and positioning:http://www.chattahoocheetech.edu/sandbox/newctc
dcp3450
For the top menu, I think you need to give the `<a>` elements the same size as you give the surrounding `<li>`s. That should work if the `<a>`s are `display: block or inline-block. For the footer, I'm not sure which images you mean. Can you clarify on that?
Pekka
the social media icons
dcp3450
Same there, the `<a>` need `display: block` or `inline-block` and explicit width / height settings. Otherwise, the clickable area of the link will reach only as far as its text.
Pekka
with the footer images i never defined <a> with a display, width, and height. I get what your saying and agree. odd that the footer links work though.
dcp3450
I redid the html:<ul><li><a href="#" class="home">Home</a></li>....</ul>css:#menuContainer a.home{background: transparent url(images/mainMenu.png) no-repeat 0 0;display: block;height: 37px;width: 73px;}text-indent: -9999px;the display and actions are correct in Chrome and firefox. IE still will not allow the image to be a link
dcp3450
Odd. Can you try adding a `line-height: 37px`?
Pekka
New developments:I completely re-did the footer and main menu IDENTICALLY. The footer works but the menu still does not work in IE. I pulled the menu html and css out and made a plank page: it works! So, one of the div's i have it contained in is causing an issue
dcp3450
You could use Firebug or IE8's developer toolbar to find out what CSS rules affect the various elements.
Pekka
It doesnt work when inside <div id="container">
dcp3450
+1  A: 

First well form the html, then try your css again.

<ul id="menu">
   <li id="home" title="Home" alt="Home"><a href="#">Home</a></li>
   <li id="current" title="Current Students" alt="Current Students"> <a href="#">Current Students</a></li>
   <li id="prospective" title="Prospective Students" alt="Prospective Students"><a href="#">Prospective Students</a></li>
   <li id="facultyStaff" title="Faculty & Staff" alt="Faculty & Staff"><a href="#">Faculty & Staff</a></li>
   <li id="visitors" title="Visitors" alt="Visitors"> <a href="#">Visitors</a></li>
</ul>
gmcalab
that had no affect. my footer links have the <a> wrapped around the <li> and they work perfect. I even moddled my menu off them and the menu won't react the same way.
dcp3450
A: 

it's better to use line-height instead of text-indent. you need to use image replacement technique. like this

<ul id="menu">
  <li><a href="#" title=""><span>Home</span></a></li>
</ul>

and CSS

ul#menu li a { width: 100px; height: 20px; background: url(../images/myimage.gif) no-repeat 0 0; }
ul#menu li span { line-height: 200px; display: block; }
pixeltocode