Create a stunning Bootstrap carousel that showcases images perfectly centered and automatically resized in both width and height to fit the Div container

Currently, I am working on integrating a Bootstrap carousel that features centred and auto-resized images. In the JSFiddle example provided (https://jsfiddle.net/koba1/mb94dgzu/6/), you will notice that the first image (a vertical one) stretches to fill the entire div, while the third image remains at the top. My goal is to achieve the same carousel behaviour seen on Facebook or Twitter, where the images are centrally positioned and resize according to the containing div. Can anyone assist me with this?

<div id="myCarousel" class="carousel slide">
<!-- Indicators -->
<ul class="carousel-indicators">
    <li data-target="#myCarousel" data-slide-to="0" class="active">One</li>
    <li data-target="#myCarousel" data-slide-to="1">Two</li>
    <li data-target="#myCarousel" data-slide-to="2">Three</li>
</ul>

<!-- Inner -->
<div class="carousel-inner">
    <div class="item active">
        <img src="https://scontent.ffco3-1.fna.fbcdn.net/v/t1.6435-9/171147625_1208393786271117_8898528403586821491_n.jpg?_nc_cat=111&ccb=1-3&_nc_sid=730e14&_nc_ohc=dDKFCCAYB-oAX_m2xtu&_nc_ht=scontent.ffco3-1.fna&oh=aa418a1e5facad55e6e4781237602778&oe=609A1892" 
             class="img-responsive" />
        <div class="container">
            <div class="carousel-caption">
                <h1>Photo 1 - Vertical</h1>
                <p>Hello!</p>
            </div>
        </div>
    </div>
    <div class="item">
        <img src="https://scontent.ffco3-1.fna.fbcdn.net/v/t1.6435-9/173646266_1210629629380866_4626783539462302067_n.jpg?_nc_cat=111&ccb=1-3&_nc_sid=730e14&_nc_ohc=QelU_ZVtqAcAX9wHI-l&_nc_ht=scontent.ffco3-1.fna&oh=272c997f1febf514a031aefe9cb712cb&oe=609BBCA2" 
             class="img-responsive" />
        <div class="container">
            <div class="carousel-caption">
                <h1>Photo 2 - Square</h1>
                <p>Hello again!</p>
            </div>
        </div>
    </div>
    <div class="item">
        <img src="https://scontent.ffco3-1.fna.fbcdn.net/v/t1.6435-9/167432599_1203101340133695_8518788973824059736_n.png?_nc_cat=107&ccb=1-3&_nc_sid=730e14&_nc_ohc=CItpIApuYmgAX_zHj8t&_nc_ht=scontent.ffco3-1.fna&oh=83145492bb38c8c2148df70086382459&oe=609A928F" 
             class="img-responsive" />
        <div class="container">
            <div class="carousel-caption">
                <h1>Photo 3 - Horizontal</h1>
                <p>Hello again and again!</p>
            </div>
        </div>
    </div>
</div>

<!-- Controls --> 
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
    <span class="icon-prev"></span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
    <span class="icon-next"></span>
</a> 
</div>

CSS

.carousel-indicators > li, .carousel-indicators > li.active {
    width: 100px;
    height: 25px;
    border-radius: 0;
    border: solid 1px grey;
    background: lightgrey;
    text-indent: 0;
}
.carousel-indicators > li.active {
    background: white;
}

#myCarousel {
  width: 500px;
  height: 500px;
}

Answer №1

Include the following script

$('#myCarousel').owlCarousel({
    autoHeight:true
});

Delete the height property from the CSS file

#myCarousel {
  width: 400px;
  /*height: 400px;*/ /*this line removed*/
}

Code has been updated as follows

#myCarousel .img-responsive{
  margin:0 auto;
  max-height:400px;
}

Updated code available here

Latest version can be found at this link

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Visual display: reveal different images by hovering over specific regions

Imagine creating an interactive group photo with two mouseover effects: a) Display a tooltip above, and b) Highlight the person / show an alternate image. I am inspired by this example (Map #2) - I want the group photo to have a darkened effect when the ...

How can I transfer a variable from ASP.NET to HTML using an Iframe?

Is it feasible to pass a variable from an ASP.NET page to an HTML page loaded in an IFrame? Specifically, I aim to have the ASP.NET page populate a field within the form on the HTML page. ...

Why am I unable to access a list item's fields in my Bootstrap 4 project using Thymeleaf within the scope ("Neither BindingResult nor plain target object...")?

After creating a modal in my Bootstrap 4 view, I encountered an error. ERROR 328 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing ...

Only one div is revealed by Jquery show and hide, while the other remains hidden

I'm attempting to utilize a single href link to toggle the visibility of two other divs. The first div should be visible by default, while the second div remains hidden. <a href="#ben1" class="fa fa fa-times closer" > <p> clickab ...

Adding a div element with a background behind a text element using Jquery

Is there a way to position an absolute div behind text or images while staying in front of background images? I have content with background images behind the text, so setting the z-index of the absolute div to -1 won't work. Any suggestions on how to ...

Enhance the efficiency of your website by ensuring that js/css files are loaded before the completion of the HTML

Currently, my website uses a template system involving XML/XSLT which outputs the generated HTML in one piece after completing all its tasks. As a result, browsers tend to start downloading all the content only after the entire HTML is generated, causing d ...

I am really struggling to comprehend this CSS calc() calculation - it's really giving me a headache

Struggling to grasp a CSS calculation that evenly spaces children in a flexed container with a defined gap between them? You're not alone! I'm looking to streamline this process by creating a SASS(SCSS) function for the calculation, but I can&ap ...

Steps to create a hover effect similar to that of a website (increasing grid size on hover)

Looking to create a hover effect for my boxes similar to the one on this website: I've examined the code of the website above and searched extensively for a similar feature without any luck. Could anyone offer assistance with this, please? ...

Creating a dynamic workspace with a portable viewing window

I am looking to create a spacious workspace for arranging objects, but I also want to incorporate a customizable "viewport" that will display a specific area of the workspace for the user. This viewport should be resizable and movable within the workspace, ...

Creating a Javascript countdown timer that does not involve displaying the

I stumbled upon this code on a website, but there's one tweak I'd like to make. Unfortunately, I can't seem to figure it out myself, so I'm reaching out for some help. What I want to achieve is removing the year from the date so that th ...

Can a dynamic react component be inserted into a standalone HTML document without the need for file downloads or server support?

I am implementing React for a Single Page Application (SPA) project where users can customize a component's font size, color, etc., and then embed this customized component into their website. I'm looking for a way to bundle the component and pre ...

showing a message in a pop-up alert box

$(document).ready(function(){ $(document).on('click', '#btnSave', function () { var fname = $("#fname").val(); var lname = $("#lname").val(); var email = $("#email").val(); var mobile = $("#mobile").val(); if(fname == ""){ alert(&apos ...

The issue of nested row padding overflowing into the parent row has been identified within Bootstrap 5

I encountered an issue in Bootstrap 5 where I have a nested row inside a column that is itself nested in an outer row. The inner row has a padding of 5 and the column has a padding of 1, with added colors and borders for better visualization. <div class ...

PHP must be loaded before Javascript code will be executed

I am in the process of creating a high-tech "smart home" webpage, but I am facing an issue with the code execution order. Despite trying to use sleep() and other tactics, I can't seem to display the success message before running the script that reboo ...

Role="presentation" containing a <form> element within

<nav> <ul class="nav nav-pills pull-right"> <li role="presentation"> <form action="/logout" method="POST" id="logout-form"> <a href="#" onClick="document.getElementById('logout-form&ap ...

In making reference to a particular subpage within a parent page

My website is designed with all inner pages linked to one master file. I'm looking to change the title font size on just one specific page, without affecting the rest of the pages that use the master file. Is there a way to target a specific page titl ...

CSS animation triggers filter transition malfunction in Google Chrome

My HTML contains several <a> tags that have different images as backgrounds. I want these <a> elements to move across the page in an animated manner and have a grayscale filter applied to them. When hovered over, they should return to their ori ...

A difference in the way content is displayed on Firefox compared to Chrome, Edge, and Safari

Recently, I encountered an issue with a tool I had developed for generating printable images for Cross-Stitch work. The tool was originally designed to work on Firefox but is now only functioning properly on that browser. The problem at hand is whether th ...

Disabling buttons in Angular and displaying error messages for mat-input

I'm facing an issue with a text box and a button. The user inputs a URL into the textbox, and there's a method called isUrlFormatValid() to validate the URL format. The problem is that when the page first loads, the isUrlFormatValid() method aut ...

Ways to incorporate a dynamic HTML form that allows for input elements to be added both horizontally and vertically while maintaining connected lines

Looking to design a form that showcases HTML elements in both vertical and horizontal positions, with lines connecting them as seen in this example: https://i.sstatic.net/jB12f.png. Can anyone offer guidance on how to achieve this? Thanks! ...