Sultan Ahmed Siddiqui
Sultan Ahmed Siddiqui

Reputation: 43

How to change Image of Bootstrap Carousel on Mobile View?

/*Carousel CSS for Mobile View*/
@media (max-width: 425px){
  #banner-one img{
    background-image: url("/images/mobile_banner1.jpg") !important;
  }
  
  #banner-two img{
    background-image: url("/images/mobile_banner2.jpg") !important;
  }
  
  #banner-three img{
    background-image: url("/images/mobile_banner3.jpg") !important;
  }
  
  #banner-four img{
    background-image: url("/images/mobile_banner4.jpg") !important;
  }
}
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div id="banner-one" class="carousel-item active">
      <img class="d-block w-100" src="/images/banner-one.jpg" alt="First slide">
    </div>
    <div id="banner-two" class="carousel-item">
      <img class="d-block w-100" src="/images/banner-two.jpg" alt="Second slide">
    </div>
    <div id="banner-three" class="carousel-item">
      <img class="d-block w-100" src="/images/banner-three.jpg" alt="Third slide">
    </div>
    <div id="banner-four" class="carousel-item">
      <img class="d-block w-100" src="/images/banner-four.jpg" alt="Third slide">
    </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>

I have a Bootstrap Carousel in my project it works perfect in each and every view. But I want to change the carousel images of every slide in the Mobile View. I have tried everything but failed.

Upvotes: 1

Views: 3588

Answers (2)

Bourbia Brahim
Bourbia Brahim

Reputation: 14712

You can set both images , and play on their visibility by css .

see below snippet ( use full screen to switch between views ):

.desk-img {
  display: block;
}

.mobile-img {
  display: none;
}

@media (max-width: 700px) {
  .desk-img {
    display: none !important;
  }
  .mobile-img {
    display: block;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div id="banner-one" class="carousel-item active">
      <img class=" w-100  desk-img" src="https://via.placeholder.com/800x380?text=slide%201" alt="First slide">
      <img class=" w-100  mobile-img" src="https://via.placeholder.com/800x380?text=mobile slide%201" alt="First slide">
    </div>
    <div id="banner-two" class="carousel-item">
      <img class=" w-100 desk-img" src="https://via.placeholder.com/800x380?text=slide%202" alt="Second slide">
      <img class=" w-100  mobile-img" src="https://via.placeholder.com/800x380?text=mobile slide%202" alt="Second slide">
    </div>
    <div id="banner-three" class="carousel-item">
      <img class=" w-100 desk-img" src="https://via.placeholder.com/800x380?text=slide%203" alt="Third slide">
      <img class=" w-100  mobile-img" src="https://via.placeholder.com/800x380?text=mobile slide%203" alt="Third slide">
    </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>

Upvotes: 3

Pandu A
Pandu A

Reputation: 101

i think you can just use picture element,

<picture>
  <source media="(max-width: 425px)" srcset="mobile-img.jpg" />
  <source media="(max-width: 768px)" srcset="tablet-img.jpg" />
  <img src="desktop-or-default-img.jpg" class="d-block w-100" alt="..." />
</picture>

In this case, mobile-img.jpg will be used if the screen is below 425px, tablet-img.jpg will be used if the screen is below 768px-425px, and desktop-or-default-img.jpg will be used if the screen is at (any)px-768px.

Upvotes: 2

Related Questions