最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin2PV0评论

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>
 

Post a comment

comment list (0)

  1. No comments so far