{"id":55,"date":"2011-05-12T00:00:54","date_gmt":"2011-05-12T10:00:54","guid":{"rendered":"http:\/\/mymonkeydo.com\/?p=55"},"modified":"2020-05-03T16:44:09","modified_gmt":"2020-05-04T02:44:09","slug":"resizing-background-image","status":"publish","type":"post","link":"https:\/\/mymonkeydo.com\/resizing-background-image\/","title":{"rendered":"Resizing Background Image"},"content":{"rendered":"
I’ve been given a project to make a background image resize with the browser window size while retaining its ratio, not causing scrollbars, and always filling up the whole window. \u00a0It seems pretty easy until you actually see it. \u00a0Here’s a real life example of it: \u00a0http:\/\/ringvemedia.com\/<\/a>. \u00a0As you can see it pans as the browser is resized larger until a certain point and then it expands. \u00a0It never breaks its width to height ratio.<\/p>\n The solution is quite simple. \u00a0It only involves using some CSS and a table.<\/p>\n First we need to make sure the image doesn’t overflow and cause scrollbars.<\/p>\n Then we need to center the div and make sure it’s not too small.<\/p>\n Finally, center the image. I’m not exactly sure why a table is needed in the html, but it does work better in filling the window with it in.<\/p>\n This is some pretty unique css coding and I’ve seen it in a couple places. \u00a0Surprisingly (not), it’s the same code at both sites! \u00a0I wonder who copied who? \u00a0Well, I can’t say I didn’t do that either.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[2],"tags":[17,36],"yoast_head":"\nSolution:<\/h2>\n
html,body,#bg,#bg table,#bg td {\n width:100%;\n height:100%;\n overflow:hidden\n}<\/pre>\n
#bg {\n position:absolute;\n width:200%;\n height:200%;\n top:-50%;\n left:-50%;\n}<\/pre>\n
#bg td {\n vertical-align:middle;\n text-align:center;\n}\n\n#bg img {\n min-height:50%;\n min-width:50%;\n margin:0 auto;\n}<\/pre>\n