英文:
Swiperjs 100% width bug when inside grid
问题
I am facing an issue while using SwiperJS inside a grid. When I place the Swiper wrapper inside a <div> 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.
<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;
}
英文:
I am facing an issue while using SwiperJS inside a grid. When I place the Swiper wrapper inside a <div> 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.
<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;
}
答案1
得分: 0
你可以使用flex代替grid。
.item-grid {
display: flex;
}
.item-grid > div {
width: 50%;
}
使用这个样式。
英文:
You can use flex instead of grid.
.item-grid {
display: flex;
}
.item-grid > div {
width: 50%;
}
Use this style.
答案2
得分: 0
你可以将包含swiper的网格项的最小宽度设置为100%。
英文:
You can set the min-width of the grid item containing the swiper to 100%.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.item-grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 24px;
}
.item-grid> :first-child {
min-width: 100%;
}
<!-- language: lang-html -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论