{"id":196146,"date":"2015-02-17T14:32:02","date_gmt":"2015-02-17T21:32:02","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=196146"},"modified":"2021-06-02T12:19:50","modified_gmt":"2021-06-02T19:19:50","slug":"background-position","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/properties\/b\/background-position\/","title":{"rendered":"background-position"},"content":{"rendered":"\n
The It has three different types of values:<\/p>\n\n\n\n The default values are Length values are pretty simple: the first value is the horizontal position, second value is the vertical position. So Percentages work a little differently. Get your math hats out: moving a background image by X% means it will align the X% point in the image to the X% point in the container. For example, Keywords are just shortcuts for percentages. It’s easier to remember and write It’s interesting to note that it doesn’t matter what order you use for the keywords: This demo shows examples of background-position<\/code> property in CSS allows you to move a background image (or gradient) around within its container.<\/p>\n\n\n\n
html {\n background-position: 100px 5px; \n}<\/code><\/pre>\n\n\n\n
100px 5px<\/code>)<\/li>
100% 5%<\/code>)<\/li>
top right<\/code>)<\/li><\/ul>\n\n\n\n
0 0<\/code>. This places your background image at the top-left of the container.<\/p>\n\n\n\n
100px 5px<\/code> will move the image 100px to the right and five pixels down. You can set length values in
px<\/code>,
em<\/code>, or any of the other CSS length values.<\/a><\/p>\n\n\n\n
50%<\/code> means it will align the middle of the image with the middle of the container.
100%<\/code> means it will align the last pixel of the image with the last pixel of the container, and so on.<\/p>\n\n\n\n
top right<\/code> than
0 100%<\/code>, and that’s why keywords are a thing. Here is a list of all five keywords and their equivalent values:<\/p>\n\n\n\n
top<\/code>: 0% vertically<\/li>
right<\/code>: 100% horizontally<\/li>
bottom<\/code>: 100% vertically<\/li>
left<\/code>: 0% horizontally<\/li>
center<\/code>: 50% horizontally if horizontal isn’t already defined. If it is then this is applied vertically.<\/li><\/ul>\n\n\n\n
top center<\/code> is the same as
center top<\/code>. You can only do this if you’re exclusively<\/em> using keywords, though.
center 10%<\/code> is not the same as
10% center<\/code>.<\/p>\n\n\n
Demo<\/h3>\n\n\n
background-position<\/code> set with length units, percentages, and keywords.<\/p>\n\n\n\n