Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Menu bar using CSS sprites

Featured Replies

Hi, I'm having a small problem getting sprites to correctly line up side by side for an informal menu.

 

I've created individual sprites of each item for a very simple rollover effect:

weddings.pngsympathy.pngevents.pngcorporate.png

 

Each sprite is 70px vertically (of which only 35px will be visible at any one time).

 

I was going to put a 35px vertical separator graphic between each item:

tilde.png

 

So basically I'm aiming for something similar to this (where I've simulated hovering the mouse over the Sympathy menu item):

menu-graphic.png

 

I'm a novice, however I was attempting to be clever using CSS, but failing as I used inline CSS (due to the menu items differing in pixel dimensions horizontally and I couldn't think of another way to do it other than the perhaps longer method of writing out four new CSS IDs - one for each menu item - with multiple tags for each ID etc.), but I placed each item into an inline unordered list:

<ul class="menu-item">
<li><a href="#" style="width: 96px; background-image: url('weddings.png');"></a></li>
<li><img src="tilde.png" /></li>
<li><a href="#" style="width: 97px; background-image: url('sympathy.png');"></a></li>
<li><img src="tilde.png" /></li>
<li><a href="#" style="width: 68px; background-image: url('events.png');"></a></li>
<li><img src="tilde.png" /></li>
<li><a href="#" style="width: 97px; background-image: url('corporate.png');"></a></li>
</ul>

 

With more general CSS within the main Head tags.

 

ul.menu-item li {
list-style: none;
display: inline;
float: left;
}
ul.menu-item li a {
display: block;
height: 35px;
text-decoration: none;
background-position: 0px 0px;
}
ul.menu-item li a:hover {
background-position: 0px 35px;
}

 

And this all works great!:

 

http://infekted.org/...ng/ul-menu.html

 

...until I simply try to place the whole thing into an outer div (#menu) to consolidate and center it:

 

http://infekted.org/...g/menu-div.html

 

<div id="menu">
<ul class="menu-item">
<li><a href="#" style="width: 96px; background-image: url('weddings.png');"></a></li>
<li><img src="tilde.png" /></li>
<li><a href="#" style="width: 97px; background-image: url('sympathy.png');"></a></li>
<li><img src="tilde.png" /></li>
<li><a href="#" style="width: 68px; background-image: url('events.png');"></a></li>
<li><img src="tilde.png" /></li>
<li><a href="#" style="width: 97px; background-image: url('corporate.png');"></a></li>
</ul>
</div>

 

...with the added CSS:-

 

#menu {
width: 436px;
height: 35px;
margin: 0px auto;
overflow: hidden;
}

 

I counted up all the horizontal pixels that all four menu items use, including three of the 'tilde' separator graphics inbetween them, and it all comes to 436px. However if I try to put the menu into a 436px wide div, it wont fit! In fact it wont fit until I enlarge the outer div to something crazy like 480px.

 

Looking at the preview of the overall 'menu div' using Dreamweaver (I know Dreamweaver isn't particularly great, but can occasionally be useful to visualise problems that you can't necessarily see) and it shows the unordered list is being physically pushed down, to the side and out of the main div.

 

dream.png

 

I applied the overflow:hidden; tag to the #menu div to hide everything outside of it, but I'm not sure why the menu items are being pushed outside of it to start with.

 

But would it be better just to not do the menu as a list, and instead just do each image separately (with separate IDs, each with multiple tags)?

 

Any thoughts on what I'm doing wrong, or what might be better practice?

 

Thank you :)

Edited by Timo

I tested your code with your own images. 436px is correct but there is left side padding and at first I could only get it to fit using width: 476px for the #menu div. This shows the importance of using a reset css http://meyerweb.com/...ools/css/reset/

In your case I just added a style to the ul tag for speed:

ul { margin: 0; padding: 0; }
ul.menu-item li {
list-style: none;
display: inline;
float: left;
}
ul.menu-item li a {
display: block;
height: 35px;
text-decoration: none;
background-position: 0px 0px;
}
ul.menu-item li a:hover {
background-position: 0 -35px;
}
#menu { width: 436px; /*476px;*/ background: lime; height: 35px; }

When I had it with width: 476px; background: lime; for #menu I could see the left padding.

 

I also thought that the a:hover offset should be -35px as you want to raise the background-image to show the lower green font which means a negative top margin, but both 35px and -35px work, which seems odd to me.

 

The menu appeared cut in half vertically is probably because you didn't use a reset. My code doesn't cut it in half.

 

If you use Meyer's reset you will have to set margins and padding individually for a lot of tags like h1 to h6, p, etc. because they will all be 0, but it helps to get all browsers the same first.

 

Remember to use a doctype.

Edited by Wickham

  • Author

I tested your code with your own images. 436px is correct but there is left side padding and at first I could only get it to fit using width: 476px for the #menu div. This shows the importance of using a reset css http://meyerweb.com/...ools/css/reset/

In your case I just added a style to the ul tag for speed:

ul { margin: 0; padding: 0; }

That was it. Thank you very much for that. :) However I will certainly do a more general overall reset from now on.

 

Remember to use a doctype.

For cleanliness I omitted it when posting the code here, but the working links contained it.

 

And remember to make it accessible.

I'm sorry, what does that mean?

 

Thank you very much. :)

If you use font squirrels font-face generator kit, you can do the whole thing in css, no need to use images. Nice and quick, nice and acceptable.

 

And remember to make it accessible.

I'm sorry, what does that mean?

 

How it's written right now the menu isn't usable by people with images turned off or people using screen readers. That's an accessibility problem.

 

According the the Equality Act 2010 anyone with a disability using your site should not be put at a "substantial disadvantage" compared to a non-disabled person. The law puts both site owners and web hosts liable for damages.

 

Legalities aside, it's just good practice to make your website accessible to all users and all technologies.

 

This is a good checklist for accessibility provided by the W3, here.

And remember to make it accessible.

Good point. One method is to put some text inside the "a" tag and set it with text-indent: -9999px; which will make it read in a screen reader but won't show over the menu background image. You may have to use display: block; for a span tag around the text link to make the text-indent work.

Edited by Wickham

Good point. One method is to put some text inside the "a" tag and set it with text-indent: -9999px; which will make it read in a screen reader but won't show over the menu background image. You may have to use display: block; for a span tag around the text link to make the text-indent work.

All seems like a lot of work when you can just use the font-face I mentioned earlier, use the font you want and just use a:hover ....

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.