tags:

views:

15

answers:

1

I think the answer is no, but can you position a background image with CSS so that it is a fixed amt of pixels away from the right?

If I set background position values of x and y, it seems those only give fixed pixel adjustments from the left and top respectively.

A: 

As far as I know, the CSS specification does not provide for exactly what you're asking, outside of CSS expressions, of course. Working off the assumption that you don't want to use expressions or Javascript, I see three hackish solutions:

  • Make sure your background image matches the size of the container (at least in width) and set background-repeat: repeat or repeat-x if only the width is equalized. Then, having something appear x pixels from the right is as simple as background-position: -5px 0px.
  • Using percentages for background-position exhibits special behaviour that is better seen than described here. Give it a shot. Essentially, background-position: 90% 50% will make the right edge of the background image line up 10% away from the right edge of the container.
  • Create a div containing the image. Explicitly set the position of the containing element position: relative if not already set. Set the image container to position: absolute; right: 10px; top: 10px;, obviously adjusting the final two as you see fit. Place the image div container into the containing element.
Steven Xu
thanks for the tips! yeah, i do have it now set w/ % values, but it doesn't work so well in my case. the places this bg image should appear are on elements that vary a lot in size, so of course the offset amount to the right also varies a lot killing the consistency. anyway, i'll go with some hacky workaround i'm sure, but it's odd to me this isn't part of the spec. seems simple enough, you can do it from top or left, why not start bottom or right? anyway, maybe in css4 which will be properly supported in all major browsers around the time i'm dead ;)
Doug