Having trouble importing CSS in ReactJS?

While working on my project based on next.js, I encountered a situation where loading CSS in other pages was successful:

import css from './cssname.css';

<div className={css.myclass}></div>

However, now that I am implementing a lightbox component from this link, I needed to import the necessary CSS file like this:

import "react-popupbox/dist/react-popupbox.css"

The problem arises when the CSS does not apply correctly in my code. Below is the complete code which I copied from the documentation's first example:

import React ,{Component} from 'react';
import { PopupboxManager, PopupboxContainer } from 'react-popupbox';
import "react-popupbox/dist/react-popupbox.css"

class lightbox extends Component{

    constructor(props){
        super(props)

        this.openPopupbox = this.openPopupbox.bind(this);
    }

    openPopupbox(){
        const content = (
            <div>
              <p>Work like you don't need the money.</p>
              <p>Dance like no one is watching.</p>
              <p>And love like you've never been hurt.</p>
              <span>― Mark Twain</span>
            </div>
          )
          PopupboxManager.open({ content })
    }

    render(){
        return(
            <div> 
                <button onClick={this.openPopupbox}>Click me!</button>
                <PopupboxContainer/>
            </div>
        )
    }
}

export default lightbox

Answer №1

When using NextJS, you can only import a CSS file in the pages/_app.js file. If this file does not already exist, create it and then import the CSS file there. For more information, you can refer to the documentation on Built-In CSS Support

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

CSS code that causes the animated photo banner to reset instead of continuously looping back to the first

I followed a tutorial and created this banner using HTML and CSS. However, I encountered an issue where instead of the banner continuing in loops, it keeps resetting. Since we haven't covered JavaScript yet in university, I'm looking for help to ...

What happens when there is a 1-second delay in loading CSS on an HTML page?

Lately, I've been working on creating HTML and CSS pages. However, whenever I load a page, I notice that there's always a brief half-second flash of the HTML content without any styling applied. Does anyone have an idea why this is happening and ...

Tips for retrieving data from multiple text boxes in a loop in React js and transmitting it to an API

I am in the process of developing a quiz application where I aim to create multiple question input fields based on the administrator's inputs. For example, if the admin provides 10 questions for the quiz, I will then generate a form within a loop fo ...

Executing a Site Under Maintenance feature in Full stack Next and Express

As I develop a full stack application using Next.js for the frontend and Express for the custom backend, including an admin dashboard, I am faced with the challenge of implementing a "Site in Maintenance" feature that can be controlled through the admin da ...

Issue during Firebase production: emptyChildrenSingleton isn't recognized

In my nextjs project, I am using React v18.1.0 and Firebase Realtime Database for handling notifications. The notifications work fine in development mode but fail in the production environment. The errors I encountered are as follows: ReferenceError: empt ...

Tips for stopping variables from leaking in JavaScript

I'm currently working on a JavaScript code for my task manager website. Each page has its own JS file, but I've noticed that the data saved in one file seems to leak over to the others. How can I contain these variables so that tasks don't s ...

Tips for crafting paragraphs that double as sieves

I'm trying to simplify and shorten this code. I have three HTML paragraphs acting as filters: "all", "positive," and "negative", referring to reviews. Each has a corresponding div for reviews: "allcont", "poscont", and "negcont". Clicking on any of th ...

Internet Explorer is unable to recognize the .less file format

I have created a less file that works perfectly in Chrome and Firefox, but it is not being recognized in IE 9. Does anyone have suggestions on how to make it run in IE 9.0? Below is a sample of the CSS code: @import "../core/variables.less"; @import "Mi ...

What is the best way to efficiently manage NPM dependencies in projects when faced with conflicting versions?

My goal is to create the playground app for react-native-navigation following the instructions provided here. However, a simple npm install does not work due to the peer dependencies specifying react: "*" and react-native: "*". As of to ...

What causes custom CSS properties to be overridden by Material UI CSS class properties?

I encountered an issue while attempting to utilize an npm package containing a Typography element from Material UI. The code is authored by me. Upon integrating it into a project, I noticed that the typography's CSS class properties were overpowering ...

NextJS rewrites work seamlessly in a live environment

I recently implemented a method to rewrite requests to my backend server during development: https://nextjs.org/docs/api-reference/next.config.js/rewrites rewrites: async () => [ ...nextI18NextRewrites(localeSubpaths), { source: '/api/:path*' ...

Transform objects into arrays

Is there a way to transform an object into an array of objects while adding new keys and values? This is my current object: { "0": "Ann_B", "1": "Billy_P", "2": "Carly_C", "3": "David_L" } I would like it to look like this: [ { "value": "Ann_B ...

Create objects in the gallery

I recently developed a React Material-UI component using Typescript: <Grid container direction="row" justifyContent="flex-start" alignItems="flex-start"> <Grid item xs={5}> <B ...

Guide to making a dynamic image map with interactive links using HTML and CSS

Seeking advice on how to create clickable areas within an image that lead to different pages on my website. Image maps seem like a good solution, but the issue is that they require specific coordinates which may not work well for responsiveness. I was hop ...

Can HTML/CSS be used to specifically target handheld mobile devices?

I am looking to optimize my video display in HTML by only showing it on desktop browsers. The difference in bandwidth between desktop and mobile devices is affecting the performance of mobile browsers, so I want to target only desktop users. Is there a way ...

React encountering CORS problem while utilizing AWS CDN

I am currently working on setting up REST calls from a deployed React application (hosted on AWS S3 CDN) to a resource on a Grails application. The URL for the React app is My CORS configuration mirrors the one in the AWS guide, with the exception of the ...

Using NextJS getServerSideProps in conjunction with a custom Authentication service utilizing a REST API

Attempting to implement my own authentication service with my nextJS app. My custom authentication service is a basic rest api that provides a JWT access token and refresh token upon successful login. Currently, I am storing the JWT refresh token as an ht ...

Retrieve information from an outside API within the server component, with the information being tied to a specific variable | nextjs 13

In attempting to retrieve data from an external API within a server component, it is generally considered good practice. However, in this specific scenario, my GET request relies on a variable named menu_id (located inside child.tsx) and will fetch differe ...

Chakra UI - The "Open Modal" button is disabled from being clicked repeatedly

Encountering an issue with Chakra UI modal dialog in Next.js. Attempting to utilize the code below in pages/index.tsx for displaying a modal dialog. import type { NextPage } from "next"; import { Modal, ModalOverlay, ModalContent, Moda ...

Ways to resolve the error message "TypeError: 'setOption' is not a function on type 'MutableRefObject' in React"

CODE export default function EChart({ option, config, resize }) { let chart = useRef(null) let [chartEl, setChartEl] = useState(chart) useEffect(() => { if (resize) { chartEl.resize() } if (!chartEl.cu ...