{"id":122,"date":"2011-03-11T17:14:24","date_gmt":"2011-03-11T17:14:24","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=122"},"modified":"2011-03-11T17:38:32","modified_gmt":"2011-03-11T17:38:32","slug":"background-image-stretched-and-constrained-proportions","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/background-image-stretched-and-constrained-proportions\/","title":{"rendered":"Background Image &#8211; Stretched and Constrained Proportions"},"content":{"rendered":"<p>This morning, we set about finding a way to stretch a site background image with the following requirements:<\/p>\n<ul>\n<li>The background must fill the height and width of the browser viewing window<\/li>\n<li>The background must maintain it&#39;s size proportions<\/li>\n<li>It must work in browsers that don&#39;t yet support CSS 3<\/li>\n<\/ul>\n<p>The following snippets illustrate our solution:<\/p>\n<h3>The HTML<\/h3>\n<p><code>...<br \/>\n\t&lt;body&gt;<br \/>\n\t&nbsp; &lt;div id=&quot;page-background&quot;&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;img src=&quot;\/src\/img\/bg.jpg&quot; width=&quot;100%&quot; height=&quot;100%&quot; alt=&quot;&quot; \/&gt;<br \/>\n\t&nbsp; &lt;\/div&gt; <br \/>\n\t&nbsp; &lt;div id=&quot;document&quot;&gt;<br \/>\n\t...<br \/>\n\t<\/code><\/p>\n<h3>The CSS<\/h3>\n<p><code>html,body { height: 100%; margin: 0; padding: 0; }<br \/>\n\t#page-background { position: fixed; bottom: 0; <br \/>\n\t&nbsp;&nbsp;&nbsp; left: 0; width: 100%; height: 100%; }<br \/>\n\t#document { position: relative; z-index: 1; }<\/p>\n<p>\t<\/code><\/p>\n<h3>The JavaScript \/ jQuery<\/h3>\n<p><code>resizeBG = function() {<br \/>\n\t&nbsp;&nbsp;&nbsp; var image = $(&#39;#page-background img&#39;);<br \/>\n\t&nbsp;&nbsp;&nbsp; image.removeAttr(&quot;width&quot;); <br \/>\n\t&nbsp;&nbsp;&nbsp; image.removeAttr(&quot;height&quot;);<br \/>\n\t&nbsp;&nbsp;&nbsp; var imageWidth = image.attr(&#39;width&#39;) || image.width();<br \/>\n\t&nbsp;&nbsp;&nbsp; var imageHeight = image.attr(&#39;height&#39;) || image.height();<br \/>\n\t&nbsp;&nbsp;&nbsp; var ratio = imageHeight \/ imageWidth;<br \/>\n\t&nbsp;&nbsp;&nbsp; <br \/>\n\t&nbsp;&nbsp;&nbsp; var h = $(window).height();<br \/>\n\t&nbsp;&nbsp;&nbsp; var w = $(window).width();<br \/>\n\t&nbsp;&nbsp;&nbsp; <br \/>\n\t&nbsp;&nbsp;&nbsp; $(&#39;#page-background img, #page-background&#39;).css(&#39;width&#39;, w);<br \/>\n\t&nbsp;&nbsp;&nbsp; $(&#39;#page-background img, #page-background&#39;).css(&#39;height&#39;, w * ratio);<br \/>\n\t}<\/code><\/p>\n<p><code>$(window).load( resizeBG );<br \/>\n\t$(window).resize( resizeBG );<br \/>\n\t<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>\n\tIf you have a higher res background image, you may notice some &quot;jumpiness&quot; when the page first loads, as the image is resized in front of the viewer&#39;s eyes.<\/p>\n<p>To avoid this, we can hide the image until the resizing is complete.<\/p>\n<p>Add the following CSS:<\/p>\n<p><code>#page-background { visibility: hidden; }<br \/>\n\t<\/code><\/p>\n<p>And, the following jQuery to the end of your resizeBG function:<\/p>\n<p><code>$(&#39;#page-background&#39;).css(&#39;visibility&#39;, &#39;visible&#39;);<br \/>\n\t<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This morning, we set about finding a way to stretch a site background image with the following requirements: The background must fill the height and&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[5],"tags":[56,55,54],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/122"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":4,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":126,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/122\/revisions\/126"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}