Skip to content
Advertisement

Carousel images are stacking, how do I stop them from stacking?

I’m trying to pull the most recent four images from a database into a carousel. The images are stacking on top of each other as one single slide of the carousel. I want them to be on four separate slides.

The carousel code:

<div id="display" class="carousel" data-ride="carousel">
    <!-- Indicators -->
    <ul class="carousel-indicators">
        <li data-target="#display" data-slide-to="0" class="active"></li>
        <li data-target="#display" data-slide-to="1"></li>
        <li data-target="#display" data-slide-to="2"></li>
        <li data-target="#display" data-slide-to="3"></li>
    </ul>

    <!-- The slideshow -->
    <div class="carousel-inner">
        <?php
        $db = mysqli_connect("localhost", "root", "", "gallery") or die(mysqli_error($db));
        $q = "SELECT artefact_id, imagename FROM artefact ORDER BY artefact_id DESC LIMIT 4";
        $results = mysqli_query ($db, $q) or die(mysqli_error($db));
        $row = mysqli_fetch_array($results);
        print "<div class='item active'>
        <img src='images/{$row['imagename']}'>
        </div>n";

        while($row=mysqli_fetch_array($results))
            print "<div class='item'>
            <img src='images/{$row['imagename']}'>
            </div>n" 
        ?>
    </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#display" data-slide="prev">
        <span class="glyphicon glyohicon-chevron-left" aria-hidden="true"></span>
        <span class ="sr-only">Previous</span>
    </a>
    <a class="left carousel-control" href="#display" data-slide="next">
        <span class="glyphicon glyohicon-chevron-right" aria-hidden="true"></span>
        <span class ="sr-only">Next</span>
    </a>

I have included these:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="css/style.css">

I will leave an example of what I mean here.

Advertisement

Answer

Your class names for the items are wrong. According to https://getbootstrap.com/docs/4.5/components/carousel/ the class names should be carousel-item, not item.

Change <div class='item active'> to <div class='carousel-item active'>

and <div class='item'> to <div class='carousel-item'>

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement