Issue with the carousel feature displaying multiple images in Bootstrap 4.3

I attempted to create a Carousel using bootstrap that displays multiple images in a row and slides one image at a time, similar to this:

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

I followed this post. Instead of using bootstrap-3.3.6 and jquery-2.2.2, I used bootstrap-4.3.1 and jquery-3.3.1. However, my images are displayed in a vertical order rather than a horizontal order. What am I missing? How can I arrange the images in a horizontal order?

$(document).ready(function () {
  $('.fdi-Carousel .carousel-item').each(function () {
    var next = $(this).next();
    if (!next.length) {
      next = $(this).siblings(':first');
    }
    next.children(':first-child').clone().appendTo($(this));

    if (next.next().length > 0) {
      next.next().children(':first-child').clone().appendTo($(this));
    }
    else {
      $(this).siblings(':first').children(':first-child').clone().appendTo($(this));
    }
  });
});
#outerCarousel {
  width: 100%;
}
.carousel-control-prev-icon,
.carousel-control-next-icon {
  background-color: tomato !important;
}
.carousel-inner.onebyone-carousel {
  margin: auto;
  width: 90%;
}
.onebyone-carousel .active.carousel-item-left {
  left: -33.33%;
}
.onebyone-carousel .active.carousel-item-right {
  left: 33.33%;
}
.onebyone-carousel .carousel-item-next {
  left: 33.33%;
}
.onebyone-carousel .carousel-item-prev {
  left: -33.33%;
}
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
          integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  </head>
  <body><main role="main"><div class="container"><div class="row"><div id="outerCarousel" class="carousel fdi-Carousel slide" data-ride="carousel" data-interval="5000" data-pause="false"><div id="innerCarousel" class="carousel fdi-Carousel slide" data-ride="carousel" data-interval="0" data-pause="false"><div class="carousel-inner onebyone-carousel"><div class="carouse... (length: 4724 characters)

Thank you.

Answer №1

Initially, the code you provided seemed to have several bugs. It appears that many online carousel examples are simply copied and pasted without a thorough understanding of the code. This often results in the addition of more problematic code to address visible issues, so caution is advised.

There are numerous unnecessary <div> elements and CSS styles that are supposed to rectify these buggy behaviors, but instead, they complicate the debugging process.

I recommend referring to the official documentation to gain insight into the structure of these components before gradually incorporating ideas from Stack Overflow answers.

For demonstration purposes only, I have removed most of the unnecessary parts of your code.

To achieve responsiveness, adjustments need to be made to the JavaScript code to ensure proper scaling of the carousel for different column sizes. As for the CSS, it may not be straightforward to work around that aspect.

Answer №2

$(document).ready(function() {
  $('.fdi-Carousel .carousel-item').each(function() {
    var next = $(this).next();
    if (!next.length) {
      next = $(this).siblings(':first');
    }
    next.children(':first-child').clone().appendTo($(this));

    if (next.next().length > 0) {
      next.next().children(':first-child').clone().appendTo($(this));
    } else {
      $(this).siblings(':first').children(':first-child').clone().appendTo($(this));
    }
  });
});
#innerCarousel {
  width: 100%;
}

.carousel-control-prev-icon,
.carousel-control-next-icon {
  background-color: tomato !important;
}

.carousel-inner.onebyone-carousel {
  margin: auto;
  width: 90%;
}

.onebyone-carousel .active.carousel-item-left {
  left: -33.33%;
}

.onebyone-carousel .active.carousel-item-right {
  left: 33.33%;
}

.onebyone-carousel .carousel-item-next {
  left: 33.33%;
}

.onebyone-carousel .carousel-item-prev {
  left: -33.33%;
}

.carousel-item img {
  width: 33%;
}
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>

<body>
  <main role="main">
    <div class="container">
      <div class="row">
        <div id="innerCarousel" class="carousel fdi-Carousel slide" data-ride="carousel" data-interval="5000" data-pause="false">
          <div class="carousel-inner onebyone-carousel">
            <div class="carousel-item active">
              <img src="http://placehold.it/100/4287f5/000&amp;text=1">
            </div>
            <div class="carousel-item">
              <img src="http://placehold.it/100/f57b42/000&amp;text=2">
            </div>
            <div class="carousel-item">
              <img src="http://placehold.it/100/42f58a/000&amp;text=3">
            </div>
            <div class="carousel-item">
              <img src="http://placehold.it/100/a442f5/000&amp;text=4">
            </div>
            <div class="carousel-item">
              <img src="http://placehold.it/100/d1f542/000&amp;text=5">
            </div>
            <div class="carousel-item">
              <img src="http://placehold.it/100/f5429e/000&amp;text=6">
            </div>
            <div class="carousel-item">
              <img src="http://placehold.it/100/42cef5/000&amp;text=7">
            </div>
          </div>
          <!-- .carousal-inner -->

          <a class="carousel-control-prev" href="#innerCarousel" 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="#innerCarousel" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
          </a>
        </div>
        <!-- .carousel -->
      </div>
      <!-- .row -->
    </div>
    <!-- .container -->
  </main>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

</html>

The code has been improved with various modifications in HTML and CSS. These changes are aimed at displaying carousel images in a horizontal layout. Here is a summary of the alterations that were made:

  1. Adjusted the width of images in the carousel using CSS
  2. Eliminated the div.col-md-4 element
  3. Removed the #outerCarousel container

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 width of each column in this dataset is variable and unknown

I need to organize a varying number of items into columns. The sizes of both the items and container are unknown, as well as how many columns will fit or what width they should be. I want the browser to arrange the items in as many columns as possible with ...

Is Firefox the only browser where the webpage is displayed off-center?

I've encountered a strange issue with my website that I can't seem to figure out. Despite searching extensively on Google and Stackoverflow, I haven't come across anyone else facing the same problem. During testing in different browsers lik ...

Using CSS styling to dictate how browsers display web content, specifically through setting font size measurements in EM

On different mobile devices and various browsers such as Internet Explorer and Mozilla, the font size in some divs does not appear consistent. Specifically, on a Samsung Galaxy S6. I am using EM units: I know it may not be ideal but I prefer this method a ...

Creating side by side input panels with Bootstrap version 3.1 on Internet Explorer 8

Although I have some familiarity with Bootstrap, my experience with it is limited. While attempting to create a static form using Bootstrap, I encountered difficulties getting panels to align side by side using the "col-md-6" class within rows. Despite my ...

Is it possible to have 3 divs with the height of the tallest

I have a unique template that I employ for crafting responsive websites. It relies on boxes with percentage widths, floats, and clears. A prime instance can be viewed via this link. Notice how the three boxes vary in height. While it's simple to set a ...

Animating SVG elements using CSS

After learning how to use the <animate> attribute, I successfully created a captivating animation featuring an elastic line. However, I am now determined to transform this into CSS for better control: my goal is to activate the animation only upon ho ...

Tips for adding an animated box shadow in CSS without adjusting the shadow's direction

A unique box with a rotating animation was created, however, applying a box-shadow caused the shadow to also rotate along with the box. <!DOCTYPE html> <html> <head> <title>Page Title</title> <style> body{ displa ...

How to easily incorporate images after text in Bootstrap 4 beta

I seem to be having trouble inserting an image in the "menu" section for the last item "Kava so sebou" after the text. I want the picture to be centered vertically and the row to maintain consistency with the other items above it. Any advice or suggestions ...

Having trouble implementing a CSS style for a Kendo-checkbox within a Kendo-treeview component

I am facing a The issue I am encountering is that while the CSS for k-treeview is being applied from the scss file, it is not being applied for the kendo-checkbox I attempted to resolve the problem by using the following code: <kendo-treeview ...

Tips for achieving consistent outcomes on both IE and Chrome when utilizing the `min` attribute in HTML input tags

The display of input fields is inconsistent between IE (version 11) and Chrome (version 46.0.2490.71) when using the min option in the HTML input tag. In Chrome, the input fields appear extra long, while in IE they display correctly (same as when min is no ...

"Utilizing Bootstrap to ensure content is aligned perfectly to the baseline

I've been struggling to align my contents to the baseline while working with the bootstrap framework. I tried following the instructions in the documentation, but it didn't work out as expected. <div class="row"> <div class="col-12 ...

I need to adjust my navbar in Bootstrap 5 so that one <li> item floats to the left while the remaining items float to the right

Currently embarking on my inaugural html/css venture while utilizing bootstrap. I do have some previous html experience from using Blogger in the past. Issue I'm encountering is that when expanding my navbar, I aim to have the first li item (a button ...

I am looking to reposition the navbar span tag so that it appears beneath the icon, utilizing both HTML and CSS

I'm currently working on a navbar design with 5 items, each containing an icon and a span tag. However, I'm facing difficulties in aligning the span tag below the icon for all 5 items in the navbar. Despite my best efforts, I have been unable to ...

Choosing nested elements using CSS

Looking to target a specific element within a shared class, I'm struggling to find the right method to pinpoint the exact entry of this element. What I need to do is hide the current photo (pic2.jpg) and replace it with another image (pic3.jpg) in the ...

Learn how to display two rows of div elements within a single div container

In my program, I am using a for loop to display multiple div elements in one row. The first div is showing up correctly, but I am having trouble inserting a new div that looks the same as the first one. How can I achieve this? Thank you in advance. "first ...

Issue with JQuery: Logo only becomes visible after scrolling

Recently, I added a jQuery script to modify the header as we scroll on our website. Everything is functioning smoothly except for one issue - the large logo on the left side doesn't appear when visitors first land on the page; it only shows up after t ...

Attempting to create a single function that can be utilized with numerous divs that share the same class in jQuery

Currently, I am working on creating a basic jquery gallery viewer. In my code, I have the following structure: <div class="photoset"> <img /> <img /> </div> <div class="photoset"> <img /> <img /> <i ...

Tips for stopping Infinite Scroll from loading pages when the tab is inactive

I'm currently developing a website using Bootstrap 4 and jQuery, which consists of 3 tabs, each containing masonry elements loaded on scroll with the Infinite Scroll plugin. I'm trying to figure out a way for the Infinite Scroll to only load cont ...

Assigning a class to a header upon loading the page from various sections at random

After scrolling, I used JavaScript to add a class to <header>. Here is the code snippet: $(window).scroll(function(){ var top = $(window).scrollTop(); if(top>=1){ $("header").addClass('bg-header'); } else ...

What is the best way to incorporate a transition effect into a flex item with a specified max-width, where the width adjustments are managed by a child div?

The example below demonstrates a situation where the child's width grows with a smooth transition, but the parent's width abruptly changes! What I aim for is to have both the child's width and the parent's width transition smoothly. I ...