I have a bootstrap 4 carousel:
<section id="carousel-landing" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
<div class="carousel-item active">
<h1>Title 1</h1>
<p>text 1</p>
<img src="img.jpg">
</div>
<div class="carousel-item">
<h1>Title 2</h1>
<p>text 2</p>
<img src="img.jpg">
</div>
<div class="carousel-item">
<h1>Title 3</h1>
<p>text 3</p>
<img src="img.jpg">
</div>
</div>
<a class="left carousel-control" href="#carousel-landing" role="button" data-slide="prev">
<span class="icon-prev" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-landing" role="button" data-slide="next">
<span class="icon-next" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</section>
The #carousel-landing, .carousel-inner and .carousel-item all have heights set to 100%. h1, p and img are stacked beneath each other. If the h1 and p on each slide have diferent heights, how do I set the height of each image to fill the rest of the viewport?
I tried with this:
$(document).ready(imgResize);
$(window).on('resize',imgResize);
function imgResize() {
$('#carousel-landing .carousel-item').each(function() {
var dynamic_height = ($('#carousel-landing .carousel-item').height() - $('#carousel-landing .carousel-item h1').outerHeight() - $('#carousel-landing .carousel-item p').outerHeight()) - 40;
$('#carousel-landing .carousel-item img').css("height", dynamic_height + "px");
});
}
but this targets only the first slide and it sets the rest of the images to have the same heights as the first. On browser resize it works only on the first slide, When the next slide slides in and the browser is resized the image suddenly resizes to 100% height as if h1 and p elements don't even exist