I have a vertical image that I want to be 100% height of its div container whenever a page is loaded. This works great but when I open the inspector or when the browser is resized the image naturally proportionally shrinks.
CSS:
body {width: 100%; height: 100%};
.container {width: 50%; height: 100%; position: relative};
.bkg-image {
background: url("../img/image.jpg");
background-position: right top;
background-repeat: no-repeat;
background-size: contain;
position: absolute;
height: 100%;
min-height: 500px;
width: 100%;
left: 0px;
top: 0px;
}
HTML
<body>
<div class="container">
</div>
<div class="container">
<div class="bkg-image"></div>
</div>
</body>
The first thing I want I could accomplish easily via jQuery, that is, to make the image having pixels instead of percentage when the page is ready. Css method gets the height's computed size and sets the 'new' height in pixels:
jQuery( document ).ready(function() {
var heightPx = jQuery('.bkg-image').css("height");
jQuery('.bkg-image').css("height", heightPx);
});
This does a very nice job but now let's say a user has the window minimized or not completely maximized and loads the page. After the load is done if the user maximizes the browser window, now that the image's height is converted to pixels it will have a 'fixed' height and of course will not be relative to its div container. So, what I ask you to help is:
Whatever the browser window size do the same behaviour I could accomplish BUT:
- if the browser window is resized to a bigger size convert the height to 100% again and after the resize is done convert it to pixels again.
- If the browser window is resized to a smaller size don't do nothing (I mean, stick with the jQuery already provided).
Is it possible?