2

I've 3 images, a center block that should be repeated and two end parts (left and right). I want to be able to generate the button below using these three images.

Here is the complete button.

Button

Sorry for the dark background.

I've this code so far.

li {
  background-image: url(/images/middle.png), url(/images/right.png), url(/images/left.png);
  background-position: center, right, left;
  background-repeat: repeat-x, no-repeat, no-repeat;
}

Which generates this button.

Button 2

Anyone knows why my button looks like it does and how to render the first button above? I must use the 3 given images. And no, this isn't an home/school assignment :)

EDIT: I found this tutorial on how to solve the problem. Isn't there a better way to solve the problem, maybe a more semantic way?

Linus Oleander
  • 17,746
  • 15
  • 69
  • 102

2 Answers2

1

Personally, I would try to keep it as simple as possible and use 1 repeat-x image across the middle of the button, and use CSS3 rounded corners to finish off the edges. Gracefully degrades as a square button. A good example is the "Download" button from JQueryUI.com:

Download button from JQueryUI.com


If you only want to support CSS3 compliant browsers, then you can attach multiple images in background-image.

However, pre CSS3, you can only attach 1 background-image per HTML element. You tried to attach 3, and the last 2 images in your CSS are getting ignored.

The tutorial that you found is a good solution for the type of button you are trying to create. It defines at least 3 elements to attach images to. They use 4, but you can do it with only 3.

I assume that your HTML looks like this so far:

<ul>
  <li>
    <a href="#" title="My Text button">My Text</a>
  </li>
</ul>

So you have 2 elements for which to attach; you need at least 1 more:

<ul>
  <li class="link-button">
    <a href="#" title="My Text button"><span>My Text</span></a>
  </li>
</ul>

Should do it. Then the CSS:

li.link-button {
  background: url(/images/middle.png) repeat-x;
}
.link-button a {
  background: url(/images/left.png) left center no-repeat;
}
.link-button span {
  background: url(/images/right.png) right center no-repeat;
}

However, you middle repeating image is a different color!

Community
  • 1
  • 1
JohnB
  • 18,046
  • 16
  • 98
  • 110
  • @JohnB I'm not sure why, but I'm getting a warning symbol next to the span background in chrome: http://imgur.com/2l40j Any ideas why? EDIT: It's the same thing with the href (a) background. – Linus Oleander Nov 13 '11 at 20:52
  • What's "links"? No I don't know why. I can't help you anymore sorry. – JohnB Nov 13 '11 at 20:53
  • @Oleander: have you tried with just the code JohnB gave you? Without any additions? (I'm not seeing warning symbols in Chrome) – Jarno Argillander Nov 13 '11 at 21:08