Prevent images in Bootstrap carousel from being cropped while adjusting device height and width

I am attempting to implement a responsive bootstrap carousel similar to the one showcased in the airbnb example. My challenge lies in ensuring that the carousel remains responsive across all device sizes. I have tried setting fixed height and width for both the carousel container and image, but encountered cropping issues when viewport dimensions are too small.

The objective is to replicate the functionality of the Airbnb carousel where images do not get cropped, regardless of the viewport size. Users may need to scroll vertically, but the entire image with correct aspect ratio should be visible without cropping.

For landscape images (e.g., 1000x200), they should span the full width with black bezels at the top and bottom. Likewise, portrait images (e.g., 200x1000) should stretch to the full height with black bezels on the sides. Here is my code, also available on jsfiddle. You can view the rendered version on this link:

@media screen and (min-width: 500px) {
  .commonattr {
    min-width: 100%;
  }
}

@media screen and (min-width: 768px) {
  .commonattr {
    min-width: 400px;
  }
}

@media screen and (max-height: 400px) {
  .carouseltest {
    height: 60vh;
  }
}

.cardcontainer {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 100%;
}

.carouselcontainer {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  object-fit: contain;
  /* max-width: none; */
  width: max-content;
}

#carouselExampleIndicators {
  /* min-width: 300px; */
  width: 100vw;
  margin: 0 auto
}

#carousel-bg {
  background-color: black;
}

.carousel-item {
  z-index: 2;
  object-fit: cover;
}

.carouseltest {
  z-index: 0;
  background: linear-gradient(to top, rgba(0, 0, 0, 1)0%, rgba(0, 0, 0, 0.8)10%, rgba(0, 0, 0, 0)30%);
  box-sizing: border-box;
  /* height: 600px; */
  display: flex;
  justify-content: center;
  align-items: center;
  height: 80vh;
}

.theimage {
  z-index: -1;
  width: 100%;
}

@media screen and (min-width: 420px) {
  #carouselExampleIndicators {
    background: white;
    display: flex;
    width: 25vw;
    height: 100%;
    flex-direction: column;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  }
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<div class="cardcontainer">
  <div class="carouselcontainer commonattr">
    <div class="carouselslidecontainer commonattr">
      <div id="carouselExampleIndicators" class="carousel slide commonattr" data-ride="carousel" data-interval="false">
        <ol class="carousel-indicators">
          <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active w-100"></li>
          <li data-target="#carouselExampleIndicators" data-slide-to="1" class="w-100"></li>
        </ol>
        <div class="carousel-inner" role="listbox" id="carousel-bg">
          <div class="carousel-item item active">
            <div class="carouseltest">
              <img class="theimage" src="images/8.jpg" alt="First slide">
            </div>
          </div>
          <div class="carousel-item item ">
            <div class="carouseltest">
              <img class="theimage" src="images/600x100.png" alt="Second slide">
            </div>
          </div>
        </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>

    </div>

  </div>
</div>

Screenshots of our problematic pages:

We aim to achieve a layout similar to the Airbnb Example:

These images are sourced from the Airbnb website.

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

Moving the words from textArea to each 'ol' element using JavaScript

When a word is entered in the textarea, it should be assigned to a specific class within an 'ol' tag. Each subsequent word will be placed in the next class sequentially. If there are no more words, the remaining classes should remain empty. <! ...

How to align text in the center of a card using Angular Bootstrap

I have a column div that I need to center the text in Here is my code: <div class="card mb-4"> <div class="card-header"> <div class="card-title-wrap bar-info"> ...

Employing jQuery for adding a class to the initial TD in every table row

Currently, I am dealing with a table structured like this: <table width="100%" border="0" cellspacing="0" cellpadding="0" id="tablecontent"> <tr class="tablerow"> <td>This is the td I want to add a class to.</td> <td c ...

Tips for creating a div that gracefully fades out its background image when hovered over, while keeping the internal content unaffected

I am looking to create a hover effect on a div element that has a background-color, background-image, and text. What I want is for the background-image to slowly disappear when the div is hovered over, while keeping the text and background color visible. I ...

Using three.js to set the HTML background color as clearColor

How can I set the HTML background as clearColor using three.js? Here is the code snippet for three.js: // Setting up the environment var vWebGL = new WEBGL(); var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(75, window.innerWidth / ...

What is the best way to achieve two columns of the same height, with the right column nested inside the left column?

Is there a way to create two equal height columns with the right column inside the left column using pure CSS? I have explored different methods, but due to the lack of a parent selector in CSS, I haven't been successful so far. My goal is to have ex ...

Is it possible for me to utilize the functionality of the "for" attribute in the label?

After searching extensively online with no luck, I found myself wondering if anyone is aware of a resource similar to the one provided by caniuse.com that outlines browser support for the for attribute within a label tag? I am specifically interested in t ...

Utilize a dynamic URL within the HTML audio element

Currently, I am working with the use of the audio tag in HTML and AngularJS. I need to specify the source of the audio file I want to play, as shown below. <audio src="http://localhost/audio/221122/play/212223.mp3" audio player controls p ...

Implementing React inline styles by directly incorporating brackets into the HTML rendering

I'm working on a project in React using create-react-app and trying to apply an inline style. Based on my research, the following line of code should work fine: <div id="root" style={{ height: 'auto' }}></div> However, when I r ...

Prevent the event from spreading down to the child elements

I believed I had a solid understanding of the concept of event bubbling, but now I am starting to doubt my grasp on it. I designed a semi-modal dialog like so: <div class="context-menu"> <div class="menu"> … </div> < ...

Trouble encountered when attempting to download PDF using jQuery

I have encountered an issue while trying to download a PDF using Ajax response HTML. Here is the code I am using: var newPDFAction = function () { $.ajax({ type: "post", url: '{{ route("admin.getCustomerSalesRepTota ...

Avoid incorporating unicode characters in Angular 4

How can I prevent users from using unicode characters in a text field in my front-end developed with Angular 4 framework? For example: ½ -> U+00BD ('onehalf') ...

How can I target the first checkbox within a table row using jQuery?

I am seeking a way to determine if the first checkbox in a table row is selected, rather than checking all checkboxes within that particular row. Currently, I am using this code: var b = false; $j('#tb1 td input:checkbox').each(function(){ ...

The jQuery gallery is experiencing some functionality issues

Having an issue with the gallery on my website (currently on my computer, not yet uploaded to a server). Here is the script for the gallery (loaded from the server using PHP): $(document).ready(function() { $('.gallery').hide(); $(' ...

Choosing radio buttons within rows that contain two radio buttons using ngFor

This section showcases HTML code to demonstrate how I am iterating over an array of objects. <div class="row" *ngFor="let item of modules; let i = index;"> <div class="col-md-1 align-center">{{i+1}}</div> <div class="col-md- ...

Modifying the HTML text within jQuery is not within my control

I haven't worked with jQuery in a while, and I've forgotten a lot of things about it. I attempted to change the text of an element when clicked, but for some reason it's not working. <!DOCTYPE html> <html lang="en"> < ...

Animating an image with CSS steps to create a captivating shaking

My attempt at creating a basic animation is not producing the desired smoothness. .animate { animation: motion 1.5s steps(27) forwards; } @keyframes motion { 100% { background-position: -5778px; } } <div class="animate ...

I aim to align all the div elements to the right, except for the brand

I am working on customizing a navigation bar with links contained in a div tag. I would like to shift all the items to the right except for the navbar brand. My goal is to achieve a design similar to a PSD file, but I am encountering some issues with my co ...

Eliminate any spaces surrounding the text within the div tag

Is it possible to eliminate extra space at the end of text using CSS (or possibly JS)? I've experimented with various display properties, but none seem to be effective. I need to dynamically insert a blink-div at the conclusion of a multi-line sentenc ...

No adjustment in color upon switching themes

I've been struggling to achieve the desired outcome while working on a small coding task. The goal is to modify the color of the screen background, text, and button as illustrated in the uploaded image. I have three code files for this task - index, t ...