Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Offset a background image from the right using CSS

Tags:

css

Is there a way to position a background image a certain number of pixels from the right of its element?

For example, to position something a certain number of pixels (say, 10) from the left, this is how I'd do it:

#myElement {     background-position: 10px 0; } 
like image 620
nickf Avatar asked Feb 28 '11 13:02

nickf


People also ask

How do I move a background image to the right in CSS?

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.

How do I stretch a background image in CSS?

You can use the CSS background-size: cover; to stretch and scale an image in the background with CSS only. This scales the image as large as possible in such a way that the background area is completely covered by the background image, while preserving its intrinsic aspect ratio.


1 Answers

I found this CSS3 feature helpful:

/* to position the element 10px from the right */ background-position: right 10px top; 

As far as I know this is not supported in IE8. In latest Chrome/Firefox it works fine.

See Can I use for details on the supported browsers.

Used source: http://tanalin.com/en/blog/2011/09/css3-background-position/

Update:

This feature is now supported in all major browsers, including mobile browsers.

like image 89
Steven Avatar answered Sep 18 '22 02:09

Steven