What could be causing the responsiveness issue with my react slick carousel?

https://i.sstatic.net/Qv0Pg.jpg

import React from "react";
import Slider from "react-slick";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
import Link from "next/link";

function SampleNextArrow(props) {
    const { className, style, onClick } = props;
    const updatedStyle = {
        ...style,
        display: "block",
        background: "grey",
        padding: "3px",
        // borderRadius: "50%",
        width: "25px",
        height: "25px",
      };
    return (
      <div
        className={className}
        style={updatedStyle}
        onClick={onClick}
      />
    );
  }
  
  function SamplePrevArrow(props) {
    const { className, style, onClick } = props;
    const updatedStyle = {
      ...style,
      display: "block",
      background: "grey",
      padding: "3px",
    //   borderRadius: "50%",
      width: "25px",
      height: "25px",
    };
  
    return (
      <div
        className={className}
        style={updatedStyle}
        onClick={onClick}
      />
    );
  }

export default function SimpleSlider({ slides, slidesToShow, centerPadding }) {

    var settings = {
        // className: "center",
        dots: true,
        infinite: true,
        speed: 2000,
        arrows: true,
        autoplay: true,
        autoplaySpeed: 3000,
        slidesToShow: slidesToShow,
        pauseOnHover: true,
        centerMode: true,
        centerPadding: centerPadding,
        nextArrow: <SampleNextArrow />,
        prevArrow: <SamplePrevArrow />,
        responsive: [
            {
              breakpoint: 1024,
              settings: {
                slidesToShow: 3,
                slidesToScroll: 3,
                infinite: true,
                dots: true,
                arrows: false,
              }
            },
            {
              breakpoint: 600,
              settings: {
                slidesToShow: 2,
                slidesToScroll: 2,
                initialSlide: 2,
                arrows: false,
              }
            },
            {
              breakpoint: 480,
              settings: {
                slidesToShow: 1,
                slidesToScroll: 1,
                arrows: false,
              }
            }
          ],
        appendDots: (dots) => (
            <ul
                style={{
                    display: "inline-block",
                    margin: "0 5px",
                    padding: "5px",
                    borderRadius: "25%",
                    cursor: "pointer",
                }}
            >
                {dots}
            </ul>
        ),
    };
    

    return (
        <>  
            <Slider  {...settings} className="mb-10 rounded-xl relative">
                {slides.map((slide) => (
                    <div className="h-[16rem] py-[1rem] px-[0.25rem]" key={slide.id}>
                        <Link href={slide.link}>
                            <img
                                className="w-full rounded-xl hover:scale-110"
                                src={slide.backdrop_path}
                                alt={`slide ${slide.id}`}
                            />
                        </Link>
                    </div>
                ))}
            </Slider>
        </>
    );
}

I'm facing an issue implementing the react slick carousel in my Next JS project. The responsiveness is not working as expected. When I resize the screen to a mobile view (e.g., 480px), it disappears from its correct position and displays all slides at once when I increase the size.

Here is how I call the component:

<Test2
        slides={slideData}
        slidesToShow={1} 
        centerPadding="300px" 
        />

`

Answer №1

Here are the recommended modifications for your settings.

responsive: [
  {
    breakpoint: 1024,
    settings: {
      slidesToShow: 3,
      slidesToScroll: 3,
      infinite: true,
      dots: true,
      arrows: false,
    },
  },
  {
    breakpoint: 600,
    settings: {
      slidesToShow: 2,
      slidesToScroll: 2,
      initialSlide: 2,
      arrows: false,
      centerPadding: "50px", // Adjust this line to decrease centerPadding on smaller screens
    },
  },
  {
    breakpoint: 480,
    settings: {
      slidesToShow: 1,
      slidesToScroll: 1,
      arrows: false,
      centerPadding: "0px", // Modify this line to eliminate centerPadding on smallest screens
    },
  },
],

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjust the point color of landmarks in Face-api.js

I need to customize the pointColor and lineColor in my code, but I'm not sure how to do it. I am using the fast-api.js library and haven't come across any examples of customized Landmarks Styles. export const drawResults = async (image, canvas, r ...

Error in Knex with the function whereNotExists

I'm encountering some challenges with a Knex route that deals with PostgreSQL. My goal is to insert data into a database only if the item is not already present in the database. I have attempted to use 'whereNotExists' but it doesn't se ...

Styling <html:link> elements with CSS

I'm new to using struts and I'm struggling with applying my CSS styles to link tags. <html:link action="LoginLink" styleClass="loginButton"/> My goal is to add a button image to a link that directs to a login page. I've successfully ...

Creating my initial React component/application, encountering problems with axios returning repetitive responses

Here is my React component: import './App.css'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faQuoteLeft } from '@fortawesome/free-solid-svg-icons'; import { faTwitter } from '@fortawesome/fr ...

JQuery may be successfully loaded, but it is not functioning as intended

Just started dabbling in JQuery (I'm a newbie) but I'm having trouble getting it to work! Initially, it worked a couple of times but then suddenly stopped working (pretty strange). I made some changes and now it doesn't work at all. JQuery a ...

Adjusting the width of a div element horizontally in Angular 4 and

As someone new to Angular 4, I've been attempting to create a resizable div (horizontally) without success. My goal is to be able to click and drag a grabber to resize the text area horizontally. However, in the example provided in the link below, the ...

"Efficiently incorporating a responsive sprite background image on your website -

Hey there! I am dealing with a situation where I have two columns of content in a container. The first column contains text, and the second column is a span with a background sprite image. The issue arises when the screen resolution gets smaller - I want ...

Maintaining consistent height among various lists with varying item lengths

In order to display multiple lists side by side, I have two main objectives: I want the scroll to be synchronized across these lists. This means that when I view an item in one list, I should easily be able to see the corresponding item in the other lists ...

Reactjs, encountering a hitch in utilizing material UI: Incompatible hook call detected

As a newcomer to React, I decided to incorporate Material UI components into my project. After installing the components locally using npm install and importing them into my project, I encountered an error when trying to run start: Error: Invalid hook call ...

Updating the background image for a radio button

Is it possible to customize the background image within the active radio button from the default redmond to aristo? .ui-radiobutton-icon { background-image: url("/images/ui-icons_38667f_256x240.png.jsf"); } Can someone help with the correct syntax for t ...

Using flowtype, what is the process for transforming an enum's type to generic?

Consider three enums with the type of {[key: string]: Type}. This is a common pattern, and I wanted to create a generic to reduce repetition. Here's what I attempted: export interface KeyType<T> { [key: string]: T; } I tried using it like th ...

Enhancing font-awesome icons with custom CSS using styled components in React

I've been using styled components in my project, but ran into an issue when trying to add a Font Awesome icon and apply some custom CSS like font-size and color. Despite attempting the following approach, the CSS does not seem to be correctly applied. ...

What is the reason behind a node being registered as a Comment_Node when a space is added in the

I am currently working with a piece of test code that interacts with Jest and loads React components in the DOM. const actionDropDownErrorNode =(data,myStyles={})=>{ let actionDropDownErr = TestUtils.renderIntoDocument( <div><Actio ...

Utilizing various image galleries within Twitter Bootstrap Tabs

I have incorporated three different image galleries using the SlidesJS plugin and placed them within tabs of Twitter Bootstrap. To see a demo, click here. While the first gallery is displaying correctly within its tab, the images for the second and third ...

When the CSS class of a DIV is set to X in jQuery, the script will hide all other DIVs with a

Just starting to explore jQuery and still fresh on JS knowledge, I have the desire to craft a jQuery script. To begin with, here is my HTML snippet: <div id="user-controls"> <div class="choice" id="choice-all" onclick="showAll();">All< ...

Testing the Material-UI/pickers component with Jest jest jesting

Edit: Check out this non-functional repository with a slightly different approach In the process of testing, I found the need to mock the material UI date pickers (assuming they have their own tests and don't require additional testing). Initially, I ...

Deleting the detailPanel based on a specific condition leads to extra padding

Hello everyone, I am currently working on adding a detailPanel based on a certain condition. However, when I hide it, there is an empty space left behind as shown in this link. The code works perfectly fine when the detail panel is provided: get rowDetai ...

Single parallax movement determined by the position of the mouse cursor, with no margins

Recently came across this interesting code snippet [http://jsfiddle.net/X7UwG/][1]. It displays a parallax effect when moving the mouse to the left, but unfortunately shows a white space when moving to the right. Is there a way to achieve a seamless single ...

Arranging div elements with CSS styling

I always struggle with CSS layouts and positioning 'div' elements on the page. For instance, I have created the following structure: Check out my jsfiddle: http://jsfiddle.net/JJw7c/3/ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona ...

When a reducer in React returns the state, what is the resulting effect?

If my contactReducer is structured as shown below: import { GET_CONTACTS, DELETE_CONTACT, ADD_CONTACT, EDIT_CONTACT, GET_CONTACT, } from "../actions/types"; const initialState = { contacts: [ { id: 1, name: "John Doe", e ...