Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swiper JS cutting off box-shadow

I have created a carousel using Swiper JS, however my box-shadow seems to be cut off due to the overflow: hidden; property which applied on the .swiper class (I'm assuming the properties are applied through the Swiper Object Initialization).

How can I fix this?

const swiper = new Swiper('.swiper', {
  // Optional parameters
  direction: 'horizontal',
  loop: false,

  // Navigation arrows
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },

  // Responsive breakpoints
  breakpoints: {
    600: {
      slidesPerView: 3,
      spaceBetween: 15
    }
  }
});
.product-block {
  background: #ffffff;
  border-radius: 4px;
  box-shadow: rgba(0, 0, 0, 0.16) 0px 1px 4px, rgb(51, 51, 51) 0px 0px 0px 3px;
}

.product-img img {
  max-width: 100%;
  height: auto;
  aspect-ratio: 4 / 3.5;
}

.product-description {
  padding: 7px;
  box-shadow: rgba(0, 0, 0, 0.16) 0px 1px 4px, rgb(51, 51, 51) 0px 0px 0px 3px;
}

.product-description h2,
h3,
h4 {
  font-size: 0.8rem;
  font-weight: 500;
  letter-spacing: 0.3px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.css">
<script src="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.js"></script>

<div class="swiper">
  <div class="home-wrapper-categories swiper-wrapper">
    <article class="product-block swiper-slide">
      <header class="product-img">
        <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="">
      </header>
      <footer class="product-description">
        <h2>Fudge cake</h2>
      </footer>
    </article>
    <article class="product-block swiper-slide">
      <header class="product-img">
        <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="">
      </header>
      <footer class="product-description">
        <h2>Fudge cake</h2>
      </footer>
    </article>
    <article class="product-block swiper-slide drop-shadow">
      <header class="product-img">
        <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="">
      </header>
      <footer class="product-description">
        <h2>Fudge cake</h2>
      </footer>
    </article>
  </div>

  <div class="swiper-button-prev"></div>
  <div class="swiper-button-next"></div>

</div>
like image 450
Parit Sawjani Avatar asked Sep 05 '25 03:09

Parit Sawjani


2 Answers

I figured it out. I added a some padding-bottom to my .swiper class and the box-shadow became more visible. This also works for the left, right and top padding since the overflow: hidden is also applied on those areas too.

const swiper = new Swiper('.swiper', {
  // Optional parameters
  direction: 'horizontal',
  loop: false,

  // Navigation arrows
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },

  // Responsive breakpoints
  breakpoints: {
    600: {
      slidesPerView: 3,
      spaceBetween: 15
    }
  }
});
.swiper {
  padding: 10px 10px 10px 10px !important;
}

.product-block {
  background: #ffffff;
  border-radius: 4px;
  box-shadow: rgba(0, 0, 0, 0.16) 0px 1px 4px, rgb(51, 51, 51) 0px 0px 0px 3px;
}

.product-img img {
  max-width: 100%;
  height: auto;
  aspect-ratio: 4 / 3.5;
}

.product-description {
  padding: 7px;
  box-shadow: rgba(0, 0, 0, 0.16) 0px 1px 4px, rgb(51, 51, 51) 0px 0px 0px 3px;
}

.product-description h2,
h3,
h4 {
  font-size: 0.8rem;
  font-weight: 500;
  letter-spacing: 0.3px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.css">
<script src="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.js"></script>

<div class="swiper">
  <div class="home-wrapper-categories swiper-wrapper">
    <article class="product-block swiper-slide">
      <header class="product-img">
        <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="">
      </header>
      <footer class="product-description">
        <h2>Fudge cake</h2>
      </footer>
    </article>
    <article class="product-block swiper-slide">
      <header class="product-img">
        <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="">
      </header>
      <footer class="product-description">
        <h2>Fudge cake</h2>
      </footer>
    </article>
    <article class="product-block swiper-slide drop-shadow">
      <header class="product-img">
        <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="">
      </header>
      <footer class="product-description">
        <h2>Fudge cake</h2>
      </footer>
    </article>
    <article class="product-block swiper-slide drop-shadow">
      <header class="product-img">
        <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="">
      </header>
      <footer class="product-description">
        <h2>Fudge cake</h2>
      </footer>
    </article>
  </div>

  <div class="swiper-button-prev"></div>
  <div class="swiper-button-next"></div>

</div>
like image 98
Parit Sawjani Avatar answered Sep 07 '25 20:09

Parit Sawjani


I ran into a similar problem where a card's box shadow keeps getting cut off (hidden behind another card). I use SCSS for loop to increment the z-index for every slide to solve it as follows:

enter image description here

// box shadow effect on every slide
& .swiper-slide {
    box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.7);
    -webkit-box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.7);
    -moz-box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.7);
}

// increment z-index for each slide
@for $i from 1 to 10 {
    & .swiper-slide:nth-child(#{$i}):hover {
      z-index: $i;
    }
}
like image 37
William Le Avatar answered Sep 07 '25 20:09

William Le