{"id":163,"date":"2008-08-23T22:23:30","date_gmt":"2008-08-23T20:23:30","guid":{"rendered":"http:\/\/www.maratz.com\/blog\/?p=163"},"modified":"2013-01-15T19:13:56","modified_gmt":"2013-01-15T18:13:56","slug":"how-to-offset-background-from-the-right-or-bottom","status":"publish","type":"post","link":"https:\/\/www.maratz.com\/blog\/archives\/2008\/08\/23\/how-to-offset-background-from-the-right-or-bottom\/","title":{"rendered":"CSS 101: How to offset background from the right or bottom"},"content":{"rendered":"<div class=\"alignleft-pull\">\n<p><em>CSS level: beginner\/intermediate<\/em><\/p>\n<\/div>\n<div class=\"post-update\">\n<p>CSS3 now supports background-position property: <a href=\"http:\/\/www.w3.org\/TR\/css3-background\/#background-position\">http:\/\/www.w3.org\/TR\/css3-background\/#background-position<\/a><\/p>\n<\/div>\n<p><code>Background-position<\/code> CSS property allows us to position background image starting from top left corner of a HTML element.<\/p>\n<p>We are able to position either with some numeric values such as <code>100%<\/code> and<code>10px<\/code>, or with keywords such as <code>left<\/code> and <code>bottom<\/code>. Either way, you always have to offset it from the top left corner.<\/p>\n<p>If the design dictates positioning background image to bottom right corner, obviously we would use <code>background-position: 100% 100%;<\/code><\/p>\n<p>But sometimes we need an offset from the bottom right. And if you also have expanding box, it doesn&#8217;t come by default in CSS.<!--more--><\/p>\n<p>We are all aware that fixed height layouts are matter of history, but bare with me for a moment \u2014 with fixed width\/height layouts background positioning is fairly easy. You always know where the background image will end, thus you can make it a little bit smaller in size than the element itself.<\/p>\n<p>For example: if a paragraph is 300px wide, we can set the background-image in a way to leave a 10px wide empty area on the right.<\/p>\n<p>First, let\u2019s see the rule:<\/p>\n<pre><code>p {\r\n    width: 260px;\r\n    height: 260px;\r\n    padding: 20px;  \r\n    background: url(image.gif) no-repeat 0 0;\r\n}<\/code><\/pre>\n<div><img src=\"http:\/\/www.maratz.com\/img\/2008\/08-aug\/background-290.gif\" alt=\"Cut background\" width=\"420\" \/><\/p>\n<p class=\"caption\">Figure 1. Background image is cropped to 290 x 290px<\/p>\n<\/div>\n<p>In this example, if we crop the background image to be 290 x 290px, you can easily achieve an effect of <a href=\"http:\/\/webdesign.maratz.com\/lab\/background-position\/fixed.php\">background image being 10px shorter and 10px thiner than the paragraph<\/a>.<\/p>\n<p>I agree, it\u2019s no brainer. But keep reading.<\/p>\n<h2>Flexible is new fixed<\/h2>\n<p>With liquid or flexible layouts, dimensions of HTML elements are variable (different screen resolutions, window resizing, etc.). Say we have:<\/p>\n<pre><code>p {\r\n    width: 100%;\r\n    height: 100%;\r\n    background: url(image.gif) no-repeat 100% 100%;\r\n}<\/code><\/pre>\n<p>I\u2019ve seen developers reach for another element to achieve the offset. And really, if you&#8217;re in a hurry, add a child element, offset it with a margin, and then paste the background to that inner element.<\/p>\n<p>Naturally, there is a better way.<\/p>\n<p>Open the background image with Photoshop, and choose Image \u2192 Canvas Size (or Command\/Ctrl + Alt\/Option + C)  <\/p>\n<div class=\"alignleft-pulllarge image\"><img src=\"http:\/\/www.maratz.com\/img\/2008\/08-aug\/canvas-resize.jpg\" alt=\"Select Image \u2192 Canvas Size\" width=\"640\" \/><\/p>\n<p class=\"caption\">Figure 2. Resizing canvas<\/p>\n<\/div>\n<p>Enter the values as shown on the Figure 2. (tip: if you check Relative, you can use mathematical operands). When you&#8217;re done, hit OK.<\/p>\n<p>You should have some empty space on the right and bottom.<\/p>\n<div class=\"image\"><img src=\"http:\/\/www.maratz.com\/img\/2008\/08-aug\/empty-space.gif\" alt=\"Empty space around image\" width=\"420\" \/><\/p>\n<p class=\"caption\">Figure 3. Empty space after Canvas Resize<\/p>\n<\/div>\n<p>Save image as a GIF with transparency on and voil\u00e1 &#8211; it\u2019s <a href=\"http:\/\/webdesign.maratz.com\/lab\/background-position\/flexible.php\">always shifted 10px from the bottom and 10px from the right<\/a> (resize your browser window or increase font size when you are checking the example).<\/p>\n<p>At first, you might wonder where you should use this technique, but once you get your fingers on flexible layouts, you might find this useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Advanced Cascading Style Sheets trick. Geeks only.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[25,28],"_links":{"self":[{"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts\/163"}],"collection":[{"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/comments?post=163"}],"version-history":[{"count":0,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts\/163\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/media?parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/categories?post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/tags?post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}