javascript - CSS conflict in react using swiper.js library - Stack Overflow

I created two sliders using swiper.js in react. The css rules for both the sliders are different and ha

I created two sliders using swiper.js in react. The css rules for both the sliders are different and had to target same css classes that library provided. When I integrate both the ponents in react, css conflict happens. How do I resolve this ?

eg: 1st ponent slider css rule

.swiper-container {
  position: relative;
  width: 100%;
  padding-top: 50px;
  padding-bottom: 150px;
}

2nd ponent slider css rule

.swiper-container {
  max-width: 500px;
  border-radius: 15px;
  overflow: hidden;
  padding-left: 10px;
  padding-right: 10px;
  margin-bottom: 16px;
  margin-left: 0;
  margin-right: 0;
}

The 2nd ponent css rule will override the first ponent css rule which I dont want that happen Any way to solve this ?

I created two sliders using swiper.js in react. The css rules for both the sliders are different and had to target same css classes that library provided. When I integrate both the ponents in react, css conflict happens. How do I resolve this ?

eg: 1st ponent slider css rule

.swiper-container {
  position: relative;
  width: 100%;
  padding-top: 50px;
  padding-bottom: 150px;
}

2nd ponent slider css rule

.swiper-container {
  max-width: 500px;
  border-radius: 15px;
  overflow: hidden;
  padding-left: 10px;
  padding-right: 10px;
  margin-bottom: 16px;
  margin-left: 0;
  margin-right: 0;
}

The 2nd ponent css rule will override the first ponent css rule which I dont want that happen Any way to solve this ?

Share Improve this question asked Oct 31, 2020 at 6:30 ronak patelronak patel 4181 gold badge10 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

what you can do is adding a container class for each slider and use that container class to prefix your css rules.

as an example:

// import Swiper core and required ponents
import SwiperCore, {A11y, Navigation, Pagination, Scrollbar} from 'swiper';

import {Swiper, SwiperSlide} from 'swiper/react';

// Import Swiper styles
import 'swiper/swiper.scss';
import 'swiper/ponents/navigation/navigation.scss';
import 'swiper/ponents/pagination/pagination.scss';
import 'swiper/ponents/scrollbar/scrollbar.scss';

// install Swiper ponents
SwiperCore.use([Navigation, Pagination, Scrollbar, A11y]);

export default () => {
    return (
        <>
            <div className="container-1">
                <Swiper
                    spaceBetween={50}
                    slidesPerView={3}
                    navigation
                    pagination={{clickable: true}}
                    scrollbar={{draggable: true}}
                    onSwiper={(swiper) => console.log(swiper)}
                    onSlideChange={() => console.log('slide change')}
                >
                    <SwiperSlide>Slide 1</SwiperSlide>
                    <SwiperSlide>Slide 2</SwiperSlide>
                    <SwiperSlide>Slide 3</SwiperSlide>
                    <SwiperSlide>Slide 4</SwiperSlide>
                    ...
                </Swiper>
            </div>
            <div className="container-2">
                <Swiper
                    spaceBetween={50}
                    slidesPerView={3}
                    navigation
                    pagination={{clickable: true}}
                    scrollbar={{draggable: true}}
                    onSwiper={(swiper) => console.log(swiper)}
                    onSlideChange={() => console.log('slide change')}
                >
                    <SwiperSlide>Slide 1</SwiperSlide>
                    <SwiperSlide>Slide 2</SwiperSlide>
                    <SwiperSlide>Slide 3</SwiperSlide>
                    <SwiperSlide>Slide 4</SwiperSlide>
                    ...
                </Swiper>
            </div>
        
        </>
    );
};

CSS

.container-1 .swiper-container {
    position: relative;
    width: 100%;
    padding-top: 50px;
    padding-bottom: 150px;
}
.container-2 .swiper-container {
    max-width: 500px;
    border-radius: 15px;
    overflow: hidden;
    padding-left: 10px;
    padding-right: 10px;
    margin-bottom: 16px;
    margin-left: 0;
    margin-right: 0;
}

You don't need to use same class for multiple ponent.

Do like this to void conflicts.

.s1 for first swiper slider and .s2 for second one.

var s1 = new Swiper('.s1', {
    slidesPerView: 4,
    spaceBetween: 10,
    pagination: '.sp1',
    paginationClickable: true,
});
var s2 = new Swiper('.s2', {
    slidesPerView: 4,
    spaceBetween: 10,
    pagination: '.sp2',
    paginationClickable: true,
});
.s1 {
 background: pink;
 margin-bottom: 30px; 
}

.s2 {
 background: lime;
}


.swiper-slide {
  border: 1px solid #115599;
}
<link rel="stylesheet" href="https://unpkg./swiper/swiper-bundle.css">
<script src="https://unpkg./swiper/swiper-bundle.js"></script>

<div class="swiper-container s1">
        <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 class="swiper-slide">Slide 10</div>
        </div>
        <!-- Add Pagination -->
        <div class="swiper-pagination sp1"></div>
    </div>

    <!-- Swiper -->
    <div class="swiper-container s2">
        <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 class="swiper-slide">Slide 10</div>
        </div>
        <!-- Add Pagination -->
        <div class="swiper-pagination sp2"></div>
 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744293530a4567148.html

相关推荐

  • javascript - CSS conflict in react using swiper.js library - Stack Overflow

    I created two sliders using swiper.js in react. The css rules for both the sliders are different and ha

    7天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信