The component I created seems to be having trouble recognizing the Slickr CSS that was

I have included Sick carousel's CSS in the root component by importing it like this:

    import React from 'react'
    import PropTypes from 'prop-types'
    import { ThemeProvider } from 'styled-components'
    import { AppContext } from 'services/AppContext'
    import Layout from 'components/blocks/Layout'
    import { GlobalStyle, AppContainer } from 'shared/styles'
    
    import theme from 'theme'
    
    import 'slick-carousel/slick/slick.css'
    import 'slick-carousel/slick/slick-theme.css'
    
        function App({ Component, pageProps }) {
          return (
            <AppContext.Provider value={{ foo: 'bar' }}>
              <ThemeProvider theme={theme}>
                <AppContainer>
                  <GlobalStyle />
                  <Layout>
                    <Component {...pageProps} />
                  </Layout>
                </AppContainer>
              </ThemeProvider>
            </AppContext.Provider>
          )
        }
        
        App.propTypes = {
          Component: PropTypes.elementType.isRequired,
          pageProps: PropTypes.object.isRequired,
        }
        
        export default App

However, I am facing an issue where the styles are not taking effect on my carousel.

I attempted to import the CSS directly into the component using the carousel, but encountered an error in next.js stating that it must be imported in the root component or utilized as component-level CSS, which I am unsure how to achieve.

If you have any suggestions or advice, please share them with me. Thank you!

Answer №1

Encountered a similar issue with react-slick where CSS from slick-carousel was needed for the slider and navigation arrows.

Here are two possible solutions:

  • Solution 1: Move the CSS import to the root (pages/_app.js)
// _app.css
...
import 'slick-carousel/slick/slick.css'
import 'slick-carousel/slick/slick-theme.css'
...

// page/_app.js

...
import "./_app.css"
...

However, this loads slick-carousel's CSS on all pages which may not be ideal.

  • Solution 2 (Preferred): Use the link tag to embed the required CSS in your page's <head>
// I downloaded and minified the CSS files and placed them in the public folder
/public/static/css/slick.css
/public/static/css/slick-theme.css

// page/example.js

import Head from "next/head"

...
<Head>
// Embedding local CSS
<link rel="stylesheet" type="text/css" href="/static/css/slick.css" />
<link rel="stylesheet" type="text/css" href="/static/css/slick-theme.css" />

// Alternatively, we can use CDNs as well
</Head>

This approach ensures that styles are only loaded on pages where they are needed, potentially reducing load times and saving bandwidth.

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

React useEffect appended script not activating

I'm having trouble loading a script that should generate an iframe and display a weather widget upon trigger. I am able to append the script correctly, but it doesn't seem to be called and does not create the iframe. Even when I directly add the ...

When the React button is clicked, it displays a stylish blue border

Hey there, I'm currently working on a project using React and facing an issue with some buttons I've styled. Whenever I click on them, they show a strange blue border that I can't seem to get rid of. I've tried setting border:none !impo ...

Updating row color according to values obtained from the map function in ReactJs

I have been experimenting with various methods to change the color of table rows based on specific values within a map function. Despite trying solutions like the UseRef hook and browsing through stack overflow, I have yet to achieve success. {dat ...

Creating a custom variable assignment in Bootstrap within the custom.scss file

I've been searching for a solution to this problem for half a day now. My goal is to change the value of the $primary variable from the default $blue to the $orange variable. I tried following the instructions in the documentation: $primary: red; @imp ...

Activate the Keypress event to update the input value in React upon pressing the Enter

I am facing an issue where I need to reset the value of an input using a method triggered by onPressEnter. Here is the input code: <Input type="text" placeholder="new account" onPressEnter={(event) => this.onCreateAccount(event)}> < ...

AngularJS incorporating dynamic stylesheets

I am facing a challenge with my AngularJS application as it fetches data via an API and dynamically builds a webpage. Typically, I rely on ng-style for dynamic styling, but now I need to utilize the nth-of-type attribute which can only be applied in a CSS ...

Tips for customizing the CSS styling of the validation ng-class error

Seeking advice: Is there a way to customize the CSS style of the 'error' validation error for ng-class in Bootstrap v3? This is my current code: ng-class="(form.datos.$invalid) && ( form.datos.$touched || submitted) ? 'error&apos ...

resetting the page's scroll position using a hamburger icon

After adding a hamburger icon to my web project, I noticed that whenever I click on the icon, it scrolls back to the top of the page. I tried adjusting the margin and padding properties, but it still behaves the same way. Even after removing the animation ...

Obtain a duo of distinct states with the implementation of React.useState() function

Establishing initial state: const [token, setToken] = React.useState(""); Updating the state: const calculate = () => { setToken(() =>(`name:${uuid()}`)); //_______________________________________P console.log('in calculat ...

Tips for appending the id of an active element to a URL

My goal was to include the id value of the active element in a URL and then redirect to that URL with a button click. HTML <div class="icon" tabindex="0" id="company"> <img src="company.png"> </div> <button type="submit" onclick= ...

Best Practices for Implementing the 960 CSS Clear Class

I'm puzzled by the necessity of using the clear class in the 960 css framework. Every time I remove a div with the clear class, everything seems to function properly. Can you explain to me the significance and purpose behind this class? ...

Sonar activated - Self-hosted runner limitation: unable to use npm for test coverage alongside Docker integration

Whenever I attempt to execute the (action checkout) on a self-hosted runner with npm run coverage and then after sonarqube, I encounter this error:" docker: Error response from daemon: pull access denied for 6cd1db, repository does not exist or may re ...

Why is useEffect being executed twice?

For some reason, when I try to run useEffect for the first time page load, it ends up running twice. I can't seem to figure out why this is happening. Can someone please provide assistance? I'm currently using React 18 and I need help understand ...

"Aligning the logo and navbar links to appear on the same line

I'm looking to align the logo and the navigation bar links on the same line. The logo should be displayed alone when viewed on a mobile device. Here's how it currently appears: Currently shown on two lines. This is the HTML code for it: ...

I find it confusing how certain styles are applied, while others are not

Working on my portfolio website and almost done, but running into issues with Tailwind CSS. Applied styling works mostly, but some disappear at certain breakpoints without explanation. It's mainly affecting overflow effects, hover states, and list sty ...

Learn to import a specific image in Nextjs and obtain the precise file path (imagename) post-build process

I am having trouble with importing an image and getting the correct image name/URL in the image section while using the next-seo package. Can anyone provide guidance on how to achieve this? <NextSeo title="a certain title" description=&q ...

Seeking assistance with using JavaScript to filter posts from Contentful for a Next.js website

Currently, I am integrating a Next.js blog with Contentful and employing queries to display posts on the homepage. While I can easily use .filter() to feature certain posts based on a boolean value, I am struggling to figure out how to fetch posts that mat ...

How come React-Native isn't displaying successfully retrieved data using Axios?

I recently installed axios using the command below: npm i axios After writing the code below, I encountered an issue where React-Native doesn't display any data or throw any errors: import React, {useState, useEffect} from 'react'; import a ...

Contrasts between Next.js versus React applications when incorporating styled components

I am in the process of transitioning my app from SPA React to Next.js. One peculiar bug I have encountered is that in the SPA, I have max-width: 100%, while in Next.js it's set as width: fit-content. I'm unable to pinpoint the root cause of thi ...

Incorporate a corner box feature to bring attention to the typed.js functionality

I have successfully integrated typed.js into my project and now I am looking to replicate the highlighted text with an excel-like box in one corner. I've managed to get the text typing out while also adding an SVG for the box in HTML, but I'm hav ...