How to make an image fit the carousel? [duplicate] - css

This question already has answers here:
How to create a responsive image that also scales up in Bootstrap 3
(8 answers)
Closed 4 years ago.
I have a carousel with fixed size (for example, width=800px, height=450px). But I my images have different aspect ratio (16 : 9, 16:10, 1:1, etc) and the size, bigger than the size of my carousel. I am trying to make all the images reduce their size to fit my carousel.
Code of the carousel is:
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="carousel-img" src="{{ last_post.image.url }}"
alt="First slide">
</div>
<div class="carousel-item">
<img class="carousel-img"
src="{{ second_last_post.image.url }}" alt="Second slide">
</div>
<div class="carousel-item">
<img class="carousel-img" src="{{ third_last_post.image.url }}"
alt="Third slide">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
CSS:
.carousel-inner {
height: 450px;
width: 800px;
}
How can I resize images to fit the carousel?

The following works for me. It will cover the entire div with your photo but this means that the photo will be zoomed-in as much as it needs to be to do that. So it must hide some (maybe critical) parts from the photo.
.carousel-img
{
width: 100%;
height: 100%;
object-fit: cover;
}
Otherwise if you can use object-fit: contain; instead, to always display the entire photo into your measures.

Related

How can I make multiple items appear on a bootstrap 5 carousel slide?

I have a bootstrap 5 carousel with controls. My desired output is a slideshow with 2 items per slide and I would like to scroll through one image at a time.
From here:
To here:
The problem with solutions I have found which use the bootstrap grid system like the ones posed on this topic are that they rely on the correct multiple of items to work and they scroll two items per click, not one. A lot of the solutions I have found online are buggy or overcomplicated.
Is there a simple way to achieve this?
you mean like this ?
<div id="carouselExample" class="carousel slide">
<div class="carousel-inner">
<div class="carousel-item active ">
<div class="col-12 row">
<div class="col-6" style="background-color: yellow; height: 10vh;"></div>
<div class="col-6 " style="background-color: blue; height: 10vh;"></div>
</div>
</div>
<div class="carousel-item">
<div class="col-12 row">
<div class="col-6" style="background-color: red; height: 10vh;"></div>
<div class="col-6 " style="background-color: green; height: 10vh;"></div>
</div>
</div>
</div>
<button class="carousel-control-prev" type="button" data-bs-target="#carouselExample" data-bs-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="visually-hidden">Previous</span>
</button>
<button class="carousel-control-next" type="button" data-bs-target="#carouselExample" data-bs-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="visually-hidden">Next</span>
</button>
</div>

Why has making my carousel narrower created glitchy transition between slides?

I'm want my carousel to be a certain size (not sure what size until I play about, but certainly not width: 100%) and also to be centred horizontally in the page.
My new CSS rule has made the carousel smaller and central, but now the slides do not move automatically. The ol with class "carousel-indicators" works fine, so I can select and successfully move to each slide manually.
This same CSS has seemingly removed the click functionality of my next/prev arrows. Even stranger, when I click my next arrow, the page 'smooth-scrolls' down the page a little. Thanks in advance :)
Here is the code....
HTML:
<div id="event-carousel" class="carousel slide" data-right="carousel">
<ol class="carousel-indicators">
<li data-target="#event-carousel" data-slide-to="0"></li>
<li data-target="#event-carousel" data-slide-to="1"></li>
<li data-target="#event-carousel" data-slide-to="2"></li>
<li data-target="#event-carousel" data-slide-to="3"></li>
<li data-target="#event-carousel" data-slide-to="4"></li>
</ol>
<div class="carousel-inner" roll="listbox">
<div class="carousel-item active">
<img class="d-block w-100" src="./recources/images/pioneeringSpirit.png" alt="Temporary structures built on the Pioneering Spirit catamaran">
<div class="carousel-caption">
<h3>Pioneering Spirit</h3>
<p>Weeks at sea produced thousands of square meters of temporary hospitality structures.</p>
</div>
</div>
<div class="carousel-item">
<img class="d-block w-100" src="./recources/images/rugbyWorldCup.png" alt="Main structure for England's Rugby World Cup.">
<div class="carousel-caption">
<h3>Rugby World Cup</h3>
<p>Twickenham is part of the White Rose Crew calander, but this year was special.</p>
</div>
</div>
<div class="carousel-item">
<img class="d-block w-100" src="./recources/images/thales.png" alt="A three story structure for Farnborough Airshow.">
<div class="carousel-caption">
<h3>Thales</h3>
<p>Our favourite biannual build of the worlds largest airshow.</p>
</div>
</div>
<div class="carousel-item">
<img class="d-block w-100" src="./recources/images/masterpiece.png" alt="Masterpiece Arts Fair, Chelsea.">
<div class="carousel-caption">
<h3>Masterpiece</h3>
<p>We are proud to be an integral part of this incredible transformation year in, year out.</p>
</div>
</div>
<div class="carousel-item">
<img class="d-block w-100" src="./recources/images/fieldHospital.jpg" alt="Emergancy hospital built in the Pricipality Stadium.">
<div class="carousel-caption">
<h3>Emergancy Covid-19 Field Hospital</h3>
<p>Helping turn The Pricipality Stadium into Wales largest hospital in just 10 days.</p>
</div>
</div>
<a class="carousel-control-prev" href="#event-carousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#event-carousel" role="button" data-slide="next">
<span class="carousel-control-next-icon"></span>
<span class="sr-only">next</span>
</a>
</div>
</div>
CSS:
.carousel-inner {
margin: auto;
width: 55%;
height: auto;
}
JS:
$(document).ready(() => {
$('#event-carousel').carousel({
interval: 8000
});
$('#event-carousel').carousel('cycle');
});

Accessing <img> inside nested <div> in CSS

I'm trying to fill carousel-background image fill carousel. But I'm not able to access tag from css.
<div id="carouselSlider" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<!-- data-slide-to part -->
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<div class="carousel-background"><img src="img/portfolio/fullsize/1.jpg"></div>
<div class="carousel-container">
<div class="carousel-caption">
<h2>We are xyz</h2>
<p>We help you to blah blah blah!</p>
Get Started
</div> <!--carousel-content-->
</div> <!--carousel-container-->
</div> <!--carousel-item-->
<div class="carousel-item">
<!-- similar content with <img>-->
</div> <!--carousel-item-->
<div class="carousel-item">
<!-- similar content with <img>-->
</div> <!--carousel-item-->
</div> <!--carousel-inner-->
<a class="carousel-control-prev" href="#carouselSlider" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselSlider" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div> <!--carousel-->
CSS File:
#carouselSlider .carousel-inner .carousel-item .carousel-background img{
width: 100%;
height:400px;
}
I'm not able to edit image. Also is there a way to fill image in carousel without stretching it?
You can add classes to images. That line of code should look like
<div class="carousel-background"><img class="whateveryouwant" src="img/portfolio/fullsize/1.jpg"></div>
You're trying to navigate through your nested structure using classes but that's not how it works. Because you only have one image inside your carouselSlider div you could simply access it using:
#carouselSlider img {<your css here>}
But this will probably break when you have more images in there. If you can change that HTML you should probably add a class to your img or an id to to one of those inner divs and access it directly.
To avoid the image getting stretched check out the "background-size" CSS property. Either 'cover' or 'contain' should do it for you.

Auto height increase on bootstrap carousel as token-input list increase

I have a carousel which is actually a form.
One carousel item is a multi selectbox
How can i auto increase the height on only one carousel-item as the height of the multi select box increase.
The image 1 is how i get.
Image 2 how i want it.
Much thanks for much insights. I cannot refresh the carousel. It will unset my form inputs
<div class="row no-gutters" id="mob-carousel-row" [style.height.px]="mobileCarouselRowHeight">
<div class="col-1 col-sm-1">
<a href="#carouselExampleIndicators" role="button" data-slide="prev">
<i class="fa fa-angle-left mobile-form-nav" ></i>
</a>
</div>
<div class="col-10">
<div
#plMobileCarouselForm
id="carouselExampleIndicators"
class="carousel slide"
data-ride="carousel"
data-interval="false">
<div class="carousel-inner" role="listbox" id="mob-carousel">
<div class="carousel-item active">
<div class="form-group md-form mob inputbox">
<!-- first input box item -->
</div>
</div>
<div class="carousel-item active">
<div class="form-group md-form mob muti-select">
<!-- muti select box item need auto height -->
</div>
</div>
<div class="carousel-item active">
<div class="form-group md-form mob inputbox">
<!-- first input box item -->
</div>
</div>
</div>
</div>
</div>
<div class="col-1 col-sm-1">
<a href="#carouselExampleIndicators" role="button" data-slide="next">
<i class="fa fa-angle-right mobile-form-nav" ></i>
</a>
</div>
</div>
This is intended as a comment but I can't comment yet.
Since you haven't provided your CSS it's just guess work.
However, if you set the height to auto for the container that contains the input forms or select boxes, it will adjust itself as more options become available.
.carousel-item active {
height: auto;
}

Bootstrap Carousel Pushing and Pulling Content

I am having some issues with my bootstrap carousel. The images that are currently in it are different sizes and switching between images pushes and pulls content of the page. I want a static height and for the pictures to fill the static height while maintaining aspect ratio. Where have I gone wrong here?
Here is the code from the Carousel:
<header id="main_carousel" class="carousel slide">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#main_carousel" data-slide-to="0" class="active"></li>
<li data-target="#main_carousel" data-slide-to="1" class=""></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img class="center-block fill" style="background-color:#6A6A6A" src="sample_house_one.jpg" height="inherit" alt="First slide" >
<div class="carousel-caption">
<h3>Next Esate Sale</h3>
<p>July 8th at 9:00 AM</p>
</div>
</div>
<div class="item">
<img class="center-block fill" style="background-color:#6A6A6A" src="Sample_house_two.jpg" height="inherit" alt="Second slide">
<div class="carousel-caption">
<h3>Congrats to the new owners</h3>
<p>Winning bid olny $65,000!!!</p>
</div>
</div>
</div>
<a class="left carousel-control" href="#main_carousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#main_carousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</header>
Here is the css I'm using with the carousel everything else is standard Bootstrap theme and css:
html,
body {
height: 360px;
}
.carousel {
height: 360px;
}
.carousel-inner > .item img,
.carousel-inner > .item > a > img {
min-height:500px;
max-width: 100%;
}
.item,
.active,
.carousel-inner {
height: 100%;
}
.fill {
width: 100%;
height: 100%;
background-position: center;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
}
footer {
margin: 50px 0;
}
Put a max-height: 500px on your img tag so that way it can't shrink or grow with different picture sizes.
Or better, get rid of the min-height and just define height. If your pictures are cropping oddly, you might want to either use a background-image div or crop them to the same size and aspect ratio in an image editing program for the simplest solultion. Since you're using a carousel, cropping the images ahead of time might be the easiest solution.
You could also just allow the div to overflow and hide scroll bars as well, and use flexbox to position the image in the middle with align-items: center to crop the top and bottom evenly.

Resources