tags:

views:

29

answers:

4

I can position a small background-image/icon 4 pixels from the center left of its container with:

background: url(...) no-repeat 4px 50%;

How can I position it 4 pixels from the right?

EDIT:

Ooooh. Looks like CSS3 has a declaration for that. Anyone got a working example?

A: 

I'm afraid you can't as far as I know.

Popular tricks:

  • Adding a 4px transparent margin to the image and giving it background-position: right

  • Adding a 4px margin-right to the element (works in some situations, doesn't in others)

  • Using jQuery to determine the element's weight and adjust the image position (yuck!)

Pekka
Suddenly it's starting to look much better on the left...
Eric
A: 

[CSS3 link moved to question: Kept only for comments]

Eric
Yeah, but it's CSS 3....
Pekka
[Apparently](http://www.standardista.com/css3/css3-background-properties#bg4) supported by FF and Chrome (under the heading mutliple).
Eric
@Eric if that works for you, why not! (possibly provide an IE fallback though.)
Pekka
By 'Apparently', I meant '"Apparently"'. I can't get it to work...
Eric
+2  A: 

Depending on your situation and what browsers you want to support, this works (tested on IE7-8, Firefox):

background: url(...) no-repeat right 50%; border-right: 4px solid transparent;

Of course, if you are already putting a border on the right, this will not help you at all.

Added on edit: If the above doesn't work because your are using the border, and you don't care about IE7 (not sure we are quite at that point yet), and your "icon" width is known, then you could do:

.yourContainer {
  position: relative;
}

.yourContainer:after {
  content: ' '; 
  display: block; 
  position: absolute; 
  top: 0; 
  bottom: 0; 
  right: 4px; 
  width: 10px; //icon width
  z-index: -1; //makes it act sort of like a background
  background: url(...) no-repeat right 50%;
}
Scott
Oh, this is a very nice idea. +1
Pekka
A: 

how about

background: url(...) no-repeat right 50%;
padding:0px;
padding-right:4px;

in the case you ever want a border

isildur4