I need hide the carousel on mobile devices

I have a carousel built in Bootstrap 4 and I want to hide it on mobile devices. How can I do it?

<!--Carousel Wrapper-->
<div id="carousel-example-3" class="carousel carousel-fade white-text" data-ride="carousel" data-interval="false">
    <!--Indicators-->
    <ol class="carousel-indicators">
        <li data-target="#carousel-example-3" data-slide-to="0" class="active"></li>
        <li data-target="#carousel-example-3" data-slide-to="1"></li>
        <li data-target="#carousel-example-3" data-slide-to="2"></li>  
    </ol>
    <!--/.Indicators-->
    <!--Slides-->
    <div class="carousel-inner" role="listbox">
        <!-- First slide -->
        <div class="carousel-item active view hm-black-light" style="background-image: url(img/service1.jpg); background-repeat: no-repeat; background-size: cover;">
        </div>
        <!-- /.First slide -->
        <!-- Second slide -->
        <div class="carousel-item view hm-black-light" style="background-image: url(img/service2.jpg); background-repeat: no-repeat; background-size: cover;" ">
        </div>
        <!-- /.Second slide -->
        <!-- Third slide -->
        <div class="carousel-item view hm-black-light "  style="background-image: url(img/service5.jpg); background-repeat: no-repeat; background-size: cover; ">
        </div>
        <!-- /.Third slide -->
    </div>
    <!--/.Slides-->
</div>
<!--/.Carousel Wrapper-->

Upvotes: 2

Views: 2666

Answers (1)

WebDevBooster
WebDevBooster

Reputation: 14964

To completely hide the carousel on mobile devices you add the classes d-none d-md-block to the parent element like shown in the code snippet below.

The d-none (display:none) will hide the entire thing from the smallest screen size onwards and the d-md-block class (display:block) will show it from the medium (md) screen size and up.

Here's the code snippet (click the "run code snippet" button below and expand to full page):

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<p class="mt-3">The following carousel is hidden on screens that are smaller than medium ('md'):</p>

<div id="carouselExampleIndicators" class="carousel slide d-none d-md-block" data-ride="carousel">
    <ol class="carousel-indicators">
        <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
    </ol>
    <div class="carousel-inner">
        <div class="carousel-item active">
            <img class="d-block w-100" src="https://placeimg.com/1080/500/animals" alt="First slide">
            <div class="carousel-caption d-none d-md-block">
                <h5>My Caption Title (1st Image)</h5>
                <p>The whole caption will only show up if the screen is at least medium size.</p>
            </div>
        </div>
        <div class="carousel-item">
            <img class="d-block w-100" src="https://placeimg.com/1080/500/arch" alt="Second slide">
        </div>
        <div class="carousel-item">
            <img class="d-block w-100" src="https://placeimg.com/1080/500/nature" alt="Third slide">
        </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>

By the way, there was invalid HTML in your code. You might want to fix that.

Upvotes: 2

Related Questions