Avoiding the stretching of images in a bootstrap carousel is a top priority

I'm facing an issue with my Bootstrap 4 carousel that takes up a large portion of the page. The images are loaded through an ajax request once received.

Since all the photos are taken on a phone, they are either in landscape or portrait style.

The problem is that the bootstrap carousel either stretches the image too far, causing it to extend beyond the limit of my div, or distorts the image quality by skewing it. Is there a way to set a limit to the image and adjust it dynamically so that each unique photo fits my div?

CSS for the div containing my carousel:

#right_curtain {
    z-index: 1;
    position: fixed;
    left: 20%;
    width: 80%;
    height: 100%
    }

HTML for the carousel:

<div id="right_curtain">

<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" style ="z-index: 0">

    <ol class="carousel-indicators">
    </ol>
    <div class="carousel-inner">
    </div>
    <a class="carousel-control-prev" href="#carouselExampleIndicators" 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="#carouselExampleIndicators" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div>

Here's the JavaScript code that appends an image to the carousel:

for (i = 0; i < count; i++) {
    buttonJSON += '<button type="button" class="list-group-item list-group-item-action" id="buttonList'+i+'" onclick=listClick(this) data-value ="'+json[i].id+'"><b>'+json[i].department.replace("_"," ")+'</b><br><b>ADDRESS</b>: '+json[i].address+'<br><b>TYPE</b>: '+json[i].type+'<br><b>USER</b>: '+json[i].user+'</button>';
    if(i ===0){
        $('.carousel-indicators').append($('<li data-target="#carouselExampleIndicators" class="active"> data-slide-to="'+i+' "></li>'));
        $('.carousel-inner').append($('<div class="carousel-item active"> <img class="d-block w-100" src="'  + json[i].link + '" alt="'+json[i].id+'"  data-value ="'+json[i].id+'" ></div>'));
    }else{
        $('.carousel-indicators').append($('<li data-target="#carouselExampleIndicators" data-slide-to="'+i+'"></li>'));
        $('.carousel-inner').append($('<div class="carousel-item"> <img id = "image'+i+'"class="d-block w-100" src="'  + json[i].link  + '" alt="'+json[i].id+'" data-value ="'+json[i].id+'"   ></div>'));
    }
}

Answer №1

If you want to improve the display in CSS, you could consider incorporating the following code:

#right_curtain .carousel-inner img {
  object-fit: cover;
}

While this approach may not be fully compatible with IE11, personally I prioritize other browsers over IE11: https://caniuse.com/#search=object-fit

Answer №2

Transform this

#right_curtain {
  z-index: 1;
  position: fixed;
  left: 20%;
  width: 80%;
  height: 100%
}

into this

#right_curtain {
  z-index: 1;
  position: fixed;
  width: auto;
  height: 100%;
}

By making this change, the image will fill the entire height of its containing div while maintaining its original image quality and size. This adjustment should provide the desired outcome.

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

The onClick event for "history.go(0)" is functional in Internet Explorer, but it does not work in Mozilla Firefox. What can be done to address this problem specifically for Mozilla browser?

After experimenting with different browser compatibility, I have discovered a way to clear all fields when clicking the "New" button. By using the code onClick="history.go(0)", the fields are successfully emptied in IE but unfortunately fail in Mozilla. ...

What is the method for retrieving an attribute's value from an object that does not have key-value pairs?

My current project involves working with dynamoose and running a query that produces the following output: [ Document { cost: 100 }, lastKey: undefined, count: 1, queriedCount: undefined, timesQueried: 1 ] When I use typeof(output), it returns O ...

Having trouble calling REST API in node.js, whereas it works perfectly fine when called from the browser?

My goal is to invoke the WebServer [mongoose embedded webserver] that is currently running on another machine. Here is the code snippet: var express = require('express'); var http = require('http'); var router = express.Router(); /* ...

Switching the hierarchy of list items in React

I have a JSON structure with nested elements. const JSON_TREE = { name: "PARENT_3", id: "218", parent: { name: "PARENT_2", id: "217", parent: { name: "PARENT_1", i ...

The JQuery mobile navigation menu effortlessly appears on your screen

I am experiencing an issue with a JQuery mobile navigation that is designed for screens @979 pixels wide. The problem arises when the screen is resized to 979px - the menu pops up fully extended and covers the content of the web page. I suspect that this ...

While iterating over each item in the List<string> retrieved from an AJAX GET request in JavaScript

Trying to iterate through a list of strings and display them on the page, but facing an error as described in the title... "Uncaught TypeError: response.forEach is not a function" I've looked into for loops in JavaScript, but they seem to work wit ...

Fade-in effect applied to images upon exposure

Is there a way to fade in an image based on the percentage scrolled, rather than a set number of pixels? I want my website to be responsive and adjust to all screen resolutions. Or perhaps is there a method to have the image fade in when it enters the fiel ...

What is the best way to simultaneously fade in multiple elements using jQuery's .fadeIn() function?

Recently, I received assistance in understanding how to animate text using jQuery. A user recommended using the .fadeIn() function, which seems like it would suit my needs. However, a question has arisen: This is what I currently have: if (correct == "tr ...

"Utilizing AJAX for real-time search to target the final

Recently, I've been playing around with the AJAX Live Search feature that can be found on this site: http://www.w3schools.com/php/php_ajax_livesearch.asp The way it transfers the input value via AJAX to a php file for comparison is quite interesting ...

"Optimize Your Data with PrimeNG's Table Filtering Feature

I'm currently working on implementing a filter table using PrimeNG, but I'm facing an issue with the JSON structure I receive, which has multiple nested levels. Here's an example: { "id": "123", "category": "nice", "place": { "ran ...

Is it possible to implement a wsdl webservice in Phonegap?

Does anyone know the best way to call a wsdl webservice in Phonegap? Any suggestions on source code or tutorials that could be helpful? ...

Checking the efficiency of Graphql API

Currently, I am in the process of optimizing key features within my Node.js API which incorporates GraphQL. This API serves as a proxy, receiving requests from various sources and forwarding them to multiple APIs based on the request. I am interested in ...

What is the best way to save input field values when a reload occurs before submitting?

html: <form name="shippingForm" class="form-horizontal" role="form"> <div class="form-group"> <p class="control-p col-sm-2" for="Address">Address Line1<b class="h4-color">*</b>:</p> <div class="col-sm-10"& ...

Angular: presenting items in navigation bar post logging in

I am relatively new to AngularJS and I am experiencing difficulties in displaying the userInfo in my navbar and showing the Logout button only when I am logged in. 1) Currently, I am using ng-show to show the Logout button only when I am logged in, but t ...

Why can't we import Angular 4 as a library similar to AngularJS?

Why was AngularJS introduced as a script to import in an HTML page? However, in the newer version Angular 4, we need to use a web server to launch the application? Is it because AngularJS is not considered a framework but Angular 4 is? Thank you. ...

Arrange two absolute divs side by side without overlapping each other

I have a specific layout configuration: <div id="container"> <div class="left"> 1 </div> <div class="left"> 1 </div> <div class="right"> 2 </div> </div> My goal is to stack the left divs on top ...

Unresolved issue with Flask failing to retrieve URL argument from AJAX request

I'm having trouble with retrieving a submitted string from the server using Flask's Request module. The URL arguments don't seem to be picked up correctly, resulting in receiving "HELLO" instead of the actual string variable. Additionally, ...

Modernized Style - Additional Scrolling Feature for List Element

I've implemented code from Google's Material Design website for my project: https://material.io/components/web/catalog/lists/ Although it works well, the issue arises when more list entries are added and I have to scroll down to view them. The p ...

Highlighting an Element Using Selenium Webdriver at Specific Coordinates

I'm currently in the process of automating a web application built with HTML5 using Selenium Webdriver. I need help figuring out how to highlight the exact coordinates where I have clicked on the Canvas element. For example, if I click on the Canvas a ...

Utilizing SubHeaders in Material UI Select Menu for Enhanced Organization

I'm facing a challenge in setting up a lengthy list of Menu Items on a Select control that incorporates sticky SubHeaders. The issue arises when the items scroll, as they end up obstructing the subheaders. To tackle this problem, I examined the Mater ...