Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to position a background image relative to the centre of an element?

Tags:

css

I have an element that I wish to apply a background to, though I want the background image to be positioned based on its right co-ordinate.

I could use a container div to represent the background though it's not really practical in this situation.

I presently have the following rule:

.myelem {
  background-image: url("myelem.png");
  background-position: 5% 60%;
  background-repeat: no-repeat;
}

Which for the most part works because of the size of the image. If it were possible I'd like to have something that specified that the relative position of the background was middle instead of left.

like image 280
Brett Ryan Avatar asked Mar 18 '13 01:03

Brett Ryan


People also ask

How do I center align an image in the background?

You need to specify the position of the image through the "center" value of the background shorthand property. Set the width of your image to "100%". In this example, we also specify the height and background-size of our image.

Which property is used to specify the position of the background image?

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 .

Which property can be used to place or offset a background image in the element?

The background-position property sets the starting position of a background image. Tip: By default, a background-image is placed at the top-left corner of an element, and repeated both vertically and horizontally.

How do you apply a background-position?

Definition and Usage The background-position property sets the starting position of a background image. Tip: By default, a background-image is placed at the top-left corner of an element, and repeated both vertically and horizontally.


1 Answers

The css propoerty background-position accepts center as a value:

.myelem {
    background-image: url("myelem.png");
    background-position: center center;
    background-repeat: no-repeat;
}

Or the shorthand version:

.myelem {
    background: url("myelem.png") center center no-repeat;
}

Update 1

There is no simple css way to set the background-position to an offset of center (or bottom or right).

You could add padding to the actual image, use javascript to calculate the position after page load, add margin to the element as suggested in the following SO questions:

  • HTML background image offset by x pixels from the center
  • Offset a background image from the right using CSS

Alternatively you can use calc to calculate the correct position. Although calc is not supported by all browsers at this point.

Using calc you could do something like this:

.myelem {
    background-image: url("myelem.png");
    background-position: 5% 60%;
    background-position: -webkit-calc(50% - 200px) 60%;
    background-position: calc(50% - 200px) 60%;
    background-repeat: no-repeat;
}

Demo

like image 183
3dgoo Avatar answered Nov 15 '22 18:11

3dgoo