Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

background-position value manipulation

Tags:

css

I have a project where I am building slides described inside of an XML file but it requires to allow image positioning of the slides based on offset values.

Now I have Y offsets down pat, only problem now is that I require the ability to offset something in the X by an amount but still keep the %'age value behavior.

So basically is there anyway to have background-position's x start at 50% and then offset it by a pixel amount and keep the relative behavior of the %'age( 50% + offsetInPixels)?

like image 599
Stefan Avatar asked May 12 '11 17:05

Stefan


People also ask

What is the value of background-position?

The background-position CSS property sets the initial position for each background image. The position is relative to the position layer set by background-origin .

What is the value of background-position in CSS?

The default values are 0 0 . This places your background image at the top-left of the container. Length values are pretty simple: the first value is the horizontal position, second value is the vertical position. So 100px 5px will move the image 100px to the right and five pixels down.

What is the value of the background image property?

The background-image property is used to set one or more background images for an element. By default, it places the image on the top left corner. To specify two or more images, we need to specify the separate URLs with a comma for both images.

What are the two values of background attachment property?

The background-attachment property in CSS specifies how to move the background relative to the viewport. There are three values: scroll , fixed , and local .


2 Answers

You can do this, but it isn't widely supported.

background-position: -moz-calc(50% - 20px) 0;
background-position: calc(50% - 20px) 0;

Currently (May 2011) this only works in Firefox 4 and IE9.

See http://caniuse.com/#calc for compatibility.

like image 52
Rich Bradshaw Avatar answered Sep 28 '22 05:09

Rich Bradshaw


You can't do that with plain CSS (at this point in time, see Rich Bradshaw's answer).

You could accomplish that in javascript with something like:

var totalWidth = 960;
var xOffset = 10;
el.style.backgroundPosition = ((totalWidth/2) + xOffset) +"px 50px";
like image 21
jessegavin Avatar answered Sep 28 '22 05:09

jessegavin