Resize to a Specific Element’s Dimensions

Here’s a snip­pet to resize a back­ground image to the dimen­sions of an ele­ment. In this case, #bkgIm­age.

var resizeBkg = function() {
    var h = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    var w = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    var e = document.getElementById('bkgImage');
    if (w > h) {
        e.width = w;
        e.height = w;
    } else {
        e.height = h;
        e.width = h;
    }
} 

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>