skip to Main Content

I am facing an issue while using SwiperJS inside a grid. When I place the Swiper wrapper inside a with display: grid, the wrapper overflows by 100%. I have provided a link to my JSFiddle example. If you remove the CSS, you will see that everything is normal. However, when I add the grid, that’s when the problem occurs.

Thank you.

jsfiddle

<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.css"
/>

<div class="item-grid">
  <div>
    <div class="swiper mySwiper">
      <div class="swiper-wrapper">
        <div class="swiper-slide">Slide 1</div>
        <div class="swiper-slide">Slide 2</div>
        <div class="swiper-slide">Slide 3</div>
        <div class="swiper-slide">Slide 4</div>
        <div class="swiper-slide">Slide 5</div>
        <div class="swiper-slide">Slide 6</div>
        <div class="swiper-slide">Slide 7</div>
        <div class="swiper-slide">Slide 8</div>
        <div class="swiper-slide">Slide 9</div>
      </div>
      <div class="swiper-pagination"></div>
    </div>
  </div>
  <div></div>
</div>

<script src="https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.js"></script>

<script>
  var swiper = new Swiper(".mySwiper", {
    slidesPerView: 6,
    spaceBetween: 30,
    pagination: {
      el: ".swiper-pagination",
      clickable: true,
    },
  });
</script>

.item-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 24px;
}

2

Answers


  1. You can use flex instead of grid.

    .item-grid {
       display: flex;
    }
    .item-grid > div {
       width: 50%;
    }
    

    Use this style.

    Login or Signup to reply.
  2. You can set the width of the grid item containing the swiper to 0 and its min-width to 100%.

    .item-grid {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-gap: 24px;
    }
    
    .item-grid> :first-child {
      width: 0;
      min-width: 100%;
    }
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.css" />
    <div class="item-grid">
      <div>
        <div class="swiper mySwiper">
          <div class="swiper-wrapper">
            <div class="swiper-slide">Slide 1</div>
            <div class="swiper-slide">Slide 2</div>
            <div class="swiper-slide">Slide 3</div>
            <div class="swiper-slide">Slide 4</div>
            <div class="swiper-slide">Slide 5</div>
            <div class="swiper-slide">Slide 6</div>
            <div class="swiper-slide">Slide 7</div>
            <div class="swiper-slide">Slide 8</div>
            <div class="swiper-slide">Slide 9</div>
          </div>
          <div class="swiper-pagination"></div>
        </div>
      </div>
      <div>content</div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.js"></script>
    <script>
      var swiper = new Swiper(".mySwiper", {
        slidesPerView: 6,
        spaceBetween: 30,
        pagination: {
          el: ".swiper-pagination",
          clickable: true,
        },
      });
    </script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search