Differences in heights of the Bootstrap carousel in a responsive mobile view

My website features numerous carousel sliders that function perfectly on PC, but appear unappealing on mobile devices. The issue at hand is as follows:

Imagine each carousel slide contains a maximum of 4 images, all within a col-md-3.col-12 div.

<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
    <div class="row">
      <div class="col-md-3 col-12">
            <img class="img-fluid" src="https://via.placeholder.com/20x20" alt="First slide" style="height:20px">
      </div>
      <div class="col-md-3 col-12">
            <img class="img-fluid" src="https://via.placeholder.com/20x20" alt="First slide" style="height:20px">
      </div>
      <div class="col-md-3 col-12">
            <img class="img-fluid" src="https://via.placeholder.com/20x20" alt="First slide" style="height:20px">
      </div>
      <div class="col-md-3 col-12">
            <img class="img-fluid" src="https://via.placeholder.com/20x20" alt="First slide" style="height:20px">
      </div>
    </div>


    </div>

    <div class="carousel-item">
      <div class="col-md-3 col-12">
            <img class="img-fluid" src="https://via.placeholder.com/20x20" alt="First slide" style="height:20px">
      </div>
      </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>

On the initial slide, each of the 4 divs has a height of 20px. However, in mobile view, the div.col-12.col-md-4 elements are positioned vertically, causing them to stack up and resulting in a total slide height of 80px.

In the subsequent slide, with only 1 div.col-12.col-md-4, the slide is 20px in height.

Here is the first slide:

https://i.sstatic.net/jcnac.png

And here is the next slide:

https://i.sstatic.net/Yt6mL.png

Due to these height variations, the page jumps when the slide changes. What could be a viable solution to address this problem?

One potential solution could be to add a min-height:80px to the carousel to offset the height discrepancies in different slides (specifically in mobile view). However, this may not be the most elegant approach.

Another option could be to disable the carousel in the mobile view, so all items are displayed without sliding (if possible, and if so, how can this be implemented?).

Here is the JSFiddle for reference:

https://jsfiddle.net/3uq4ozd2/1/

Answer №1

If you want to ensure consistent Bootstrap Carousel slide heights, consider adjusting the height to match the tallest slide:

function equalizeSlideHeights() {
    $('.carousel').each(function(){
      var items = $('.carousel-item', this);

      // reset the height
      items.css('min-height', 0);

      // set the height
      var maxHeight = Math.max.apply(null, 
          items.map(function(){
              return $(this).outerHeight()}).get() );

      items.css('min-height', maxHeight + 'px');
    })
}

$(window).on('resize orientationchange', equalizeSlideHeights);

For more information, check out:

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

Retrieve data from a website's API endpoint using JSON format and display it on a

I came across a URL that displays [{"outlet_group_id": "29","outlet_group_name": "Parco","outlet_group_code": "0000010"}] and I have the following code snippet <script> $(document).ready(function() { $.getJSON('http://sampl.elinkurl.com/ ...

The autoplay feature of the Bootstrap video carousel is not functioning as expected

I have implemented a bootstrap video carousel, but I am facing issues with autoplay functionality. The videos do not play automatically when the page loads or when the items on the carousel change. Any suggestions on how to achieve this? My requirement i ...

The issue with jquery's marginTop functionality appears to be malfunctioning

Is there an easy way to vertically center text of unknown height within a div of known height? I've experimented with the css display: table-cell option without success, so I'm currently using jQuery to detect the height and apply a top margin if ...

Increasing the values of jQuery variables

I've written this code to assign different ids to variables, as demonstrated below: var numgrid = jQuery('.panel-grid').length; for (var i = 0; i < numgrid; i++) { var currentId = "#pg-"+id+"-"+i; if (jQuery(currentId)[0]) ...

Discovering and updating a DOM element with a rejuvenating touch: Angular JS

Although not very experienced with Angular JS, here's what I currently have and what I aim to achieve: <div ng-app="MyApp" ng-controller="appController"> <div class="input-group"> <input class="form-control enableEnter" type=" ...

Determine the necessary adjustment to center the div on the screen and resize it accordingly

Currently, I am in a situation where I must develop a piece of code that will smoothly enlarge a div from nothing to its final dimensions while simultaneously moving it down from the top of the screen. Each time this action is triggered, the final size of ...

Created a custom JQuery Slider from scratch - Disappointed with the results

Despite having experience in J2ee/C/C++, I am new to Javascript and JQuery. I managed to create my own JQuery slider, complete with source code: <!DOCTYPE html> <html> <head> <title>Experimentation</title> <style ...

Replacing a JSON vault using XHR: A step-by-step guide

Can someone assist me in replacing the JSON data from a JSON file on a website using JavaScript XHR? setagaya.json: {"trains":[{"operation_number":1,"line_id":26007,"station_id":784,"track_number":1,"up":true},{"operation_number":2,"line_id":26007,"stati ...

The end of the /if in the small template system fails to correctly detect the desired condition

If I write the following line on its own, he cuts it without any reason. So...: Greetings, I'm in need of assistance regarding an issue that is currently beyond my understanding. I'm facing a challenge with a small TPL-System which requires so ...

Reasons why Custom CSS won't function simultaneously with Bootstrap 5

I recently designed a dashboard using bootstrap 5. While working on this project, I found myself in need of a custom CSS style in addition to the default bootstrap styles. However, I encountered an issue where my custom CSS would not apply properly unles ...

Is a Bootstrap 3 Calendar Available?

I am attempting to design a calendar using Bootstrap 3.3.7 that features consistent square dates, similar to this example: https://i.sstatic.net/dYWbj.jpg It is important to me that each date is displayed in a square format. Here is what I have come up w ...

React Router version 6.4.5, automatic redirection upon loading the page

Seeking assistance with setting up redirects. Below are snippets of the code. index.js const router = createBrowserRouter([ { //Defining App as the root element... path: "/", loader: () => { }, element: <App/>, ...

Transforming this JavaScript code to be less intrusive by implementing an event listener for an unidentified ID

As I work on enhancing the functionality of this user interface, I've encountered a challenge with two tabs that require a proper event listener to ensure my JavaScript functions smoothly. This obstacle has been hindering my progress, but as I delve i ...

Increase the size of the box-shadow so that it extends beyond the boundaries

Can we create a wider box-shadow in CSS than the actual HTML element it is being applied to, while maintaining the same height as the element? Increasing the spread of the shadow typically increases the height as well. In the provided code snippet, the max ...

Displaying images with text below on the navigation bar, complemented by a sleek dividing line

I have a design in mind where I want to display 3 images at the top of a navbar, followed by centered text directly underneath each image. The spacing of the images seems to be even, but I'm not sure if this is just a coincidence or if I need to speci ...

Execute JavaScript after authentication instead of forwarding the user to another page

Short version: How can we efficiently handle a second AJAX call after an ASP.NET Membership Authentication response? Long version: Let's say we have a web-based Paint program created using ASP.NET MVC. A user is painting a picture when suddenly their ...

Creating an element in react-draggable with two sides bound and two sides open - here's how!

At the moment, I am facing an issue where the element is restricted from moving outside of the container with the class of card-width-height. My goal is to have the right and bottom sides bounded within the container while allowing the element to move beyo ...

Tips for determining the offset of mouse coordinates while utilizing a scrollbar

I'm currently developing a whiteboard application using Raphael JS. One issue I've encountered is that when the paper size exceeds the viewable portion of the web page, right and bottom scrollbars are added. This causes problems with obtaining ac ...

Suggestions for a JavaScript tool that automatically crops images

Is there a tool available, either browser-based or in Java, that can analyze an uploaded image, identify different characters within it, and crop them out into separate images? For instance, if this image contains three unique runic symbols, I would like ...

Tips for choosing the injected HTML's List Element (li) while ensuring it remains concealed

How do I hide the list item with iconsrc="/_layouts/15/images/delitem.gif" image when this div is injected by a first party and I am a third party trying to conceal it? $("<style> what should be inserted here ???? { display: none; } </style>") ...