Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML background image offset by x pixels from the center

Tags:

html

css

I'd like to put an image as the background of a webpage but have it offset by some number of pixels with respect to the center.

How can I do this?

I want:

background-image: url("bg.png"); background-position: 25% center; background-attachment: fixed; background-repeat: no-repeat; 

but instead of 25%, I want something along the lines of "center - 50px". Is there any solution to this?

like image 934
rhombidodecahedron Avatar asked May 12 '11 17:05

rhombidodecahedron


People also ask

What is background-position X?

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

How do I stop my background image from stretching CSS?

1 Answer. Show activity on this post. The 100% 100% background-size value means the background should stretch (100%) of the width of the element and (100%) of the height of the element. Have either of them set to auto , which will size the undefined dimension automatically, while preserving the images aspect ratio.

How do you set a background image as a margin?

You can't really add margins to a background image, since it's not actually an element. A couple things you might try: If you've got a containing element just inside the body, you can apply the background image to this element and set margins on it.


1 Answers

I believe I have a solution that achieves what you're wanting:

A background image (specifically a page background) offset by a number of pixels with respect to the center.

This works using only HTML & CSS - no javascript required.


Update

This can now be easily achieved using background-position and calc as a CSS unit.

The following CSS will achieve the same outcome as the previous solution (see "Original Solution" below):

#background-container {     width: 100%;      background-image: url("background-image.png");     background-position: calc(50% - 50px) 50%;     background-repeat: no-repeat; } 

Note: Don't use this method if you require support for legacy versions of IE.


Original Solution

#background-container {     width: 100%;     left: -100px; /* this must be TWICE the required offset distance*/     padding-right: 100px; /* must be the same amount as above */      background-image: url("background-image.png");     background-position: center;     background-repeat: no-repeat; } 

What this does is moves the entire container horizontally by the amount specified (in this case to the left 100px). Because the background image is centered relative to the container it moves to the left with the container.

The padding fixes the 100px of blank space that would appear to the right of the container as a result of the move. Background images show through padding). Because browsers use the border-box value instead of the default content-box value to calculate background sizing and positioning, the background image is effectively moved back to the right 50px - half the distance of the padding. (Thanks to ErikE for clarification).

So your offset/padding must be twice the required offset distance.

I have prepared a sample page for you here: http://www.indieweb.co.nz/testing/background-offset-center.html

Have a play with resizing the window. You will see that the purple and blue background image (laid over a deeper background image marking the center of the page) remains exactly 50px (half the offset/padding distance) to the left of the page center.


like image 143
Luke Avatar answered Sep 17 '22 17:09

Luke