I need to find a way to set a div's height (in px) responsively depending on the window width. I am using Bootstrap 3, and can use media queries to set the height at specific breakpoint widths, but when you reach less than 767px the page content becomes full-width and "fluid", changing size constantly.
A JSFiddle of my problem: https://jsfiddle.net/qd7t1p8j/2/ (Resize the output window to <767px sizes to see the problem - note the gold background color).
This is my code right now:
HTML:
<div class="header-banner"></div>
CSS:
.header-banner {
margin-bottom: 0;
background-repeat: no-repeat;
background-size: contain;
background-position: center;
}
@media screen and (min-width: 1199px) {
.header-banner {
height: 145px !important;
background-image: url('http://placehold.it/1314x167/');
}
}
@media screen and (max-width: 1199px) {
.header-banner {
height: 120px !important;
background-image: url('http://placehold.it/1314x167/');
}
}
@media screen and (max-width: 991px) {
.header-banner {
height: 103px !important;
background-image: url('http://placehold.it/1170x167/');
}
}
@media screen and (max-width: 767px) {
.header-banner {
height: 170px !important;
background-image: url('http://placehold.it/768x170/');
background-color: gold;
}
}
Ideally I would like to be able to do this purely through CSS, and not checking the window's size via JavaScript (as it might not always be on the client's machine).
I have tried to change my background-size
property to cover
at the smallest screen size, but this actually hides content of the image as you shrink.