Conflicting CSS files in Next.js

I recently started using Next.js and I am in need of a solution to my current problem along with some advice.

Problem:

-When accessing the empty URL (just localhost:8081), a login form is displayed, with an option to switch to the Register component without refreshing or changing the URL. After successfully registering, a message "Successful register" appears for 2.5 seconds before rendering the Login Component for user login. However, when entering the wrong username/password on the Login Component, the text color is blue instead of red as intended due to a CSS conflict between the separate CSS files for the Register and Login components. Both CSS files are imported into their respective Components files.

Page.js file:

"use client"
import React, { useState } from 'react';
import LoginForma from "./components/LoginForma/LoginForma";
import Register from './components/Register/Register';

const Login = () => {
  const [showRegistration, setShowRegistration] = useState(false);
  const [showLogin, setShowLogin] = useState(false);

  // Rest of the code...

The first and second type of users use the same CSS file which works fine, but there seems to be a mix-up between the CSS files of the Login and main Register components despite them being separate.

Tried adding !important after color: red to fix the issue temporarily, but looking for a better solution.

Folder structure:

Folder structure

-Register.jsx connects RegisterSportista.jsx and RegisterVlasnik.jsx. Both Register.jsx and LoginForma.jsx are called in Page.js file.

Answer №1

It seems like you may be using CSS for components in a less than optimal way. Have you considered looking into CSS modules?

https://nextjs.org/docs/app/building-your-application/styling/css-modules

By utilizing CSS modules, styles will be encapsulated for each component and will not interfere with others.

In your specific case, you should save LoginForma.css"; on the same level as your LoginForma component but rename it to LoginForma.module.css, then import it using

import styles from "./LoginForma.module.css";
After making these changes, when you need to apply a class from the styles file within your component, use it like this:
<div className={styles['class-name-from-file']}></div>

Answer №2

For optimal styling in your next JavaScript project, consider utilizing CSS modules or integrating popular libraries like Tailwind or Bootstrap. In my experience, I find Tailwind to be the preferred option.

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

Unleashing the power of eventListeners through exponential re-rendering upon state updates

Trying to implement an eventListener for "keydown" to update the state (a boolean named showMenu). I placed it inside a useEffect, but it's not functioning correctly and I can't pinpoint the issue. When I include showMenu in the dependency arra ...

Eliminate server-side functionality from the React project's boilerplate template

After cloning and installing the project from https://github.com/react-boilerplate/react-boilerplate, I realized that I only need the client-side portion as I intend to use a pre-existing server (express) for my application setup. Below is an excerpt f ...

Tips for making a central CSS gradient

I am looking to create a background gradient similar to the one shown in the image (Load more news) and also add a border style. Any suggestions on how to achieve this would be greatly appreciated. ...

Is there a way for the navbar to close automatically when I navigate to a new page?

Could you assist me with figuring out where and what to add in order to close the navbar when navigating to other pages? Currently, it only closes when I click on the close button itself. Below is the code for the navbar: import { useRef } from "rea ...

An error pops up on the console every time I attempt to modify the quantity of items in the shopping

I'm having trouble with my shopping cart code in JavaScript. When I try to log the variables priceElement and quantityElement in the console, nothing shows up even though the tutorial says it should. Additionally, I'm getting an error message &ap ...

Unable to retrieve data from Material UI DatePicker

As a newcomer to React, I'm currently working on creating a reusable date input component. However, I'm facing difficulties in retrieving the values from the component to the register. Here's a snippet of my form code: import React, { useSta ...

Unable to retrieve button value with material-ui package

My task requires me to retrieve the value of a button, as it contains an ID essential for further steps. Initially, I used a standard button with Bootstrap styling and everything functioned correctly. <button value={row.vacationRequestID} c ...

What is the reason behind JavaScript being able to alter pre-designed shiny UI components but not ones that are inserted through insert

Is it possible to use JavaScript to control attributes of a class of UI elements, including those added dynamically using insertUI? In this scenario, a colored box is inserted each time a button is pressed. The color is meant to change based on user input ...

The compilation of Tailwindcss encountered errors due to certain CSS files

I've encountered an issue while attempting to build my application. The compilation process has failed, specifically due to an error in the CSS Minimizer plugin referencing an unknown word on line 1356 of the main CSS file. I have searched for solutio ...

Having difficulty operating the development environment

Currently enrolled in a Udemy course and facing an issue with running a script. Attempting to execute the command npm run dev results in the following error message: npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! <a href="/cdn-cgi/l/email-protectio ...

How to troubleshoot problems with npm, yarn, or package.json

I'm encountering an issue when trying to execute npm start. Can someone help me identify what the problem is? npm ERR! code ENOENT npm ERR! syscall open npm ERR! path C:\Users\john\Desktop\final project\package.json npm ERR! e ...

The error message "No current user" occurs when using Next JS and AWS Amplify with the getServerSideProps function

Encountering a "No current user" error while attempting to make an API call from getServerSideProps in an AWS Amplify+ NextJs project. The API is a REST API protected with AWS Cognito authentication. export async function getServerSideProps({req}) { cons ...

What's the best way to adjust text color to black or white for maximum contrast when the background changes?

Currently, I am attempting to replicate the email element found on this website: .custom-blend-mode { mix-blend-mode: difference; } Although I have managed to make it work, it results in a change to the opposite color. Ideally, I would like it to remai ...

What is the best way to manipulate the size and orientation of an image upon hovering over

I'm struggling to figure out how to simultaneously rotate and scale a .png file when the user hovers over it. Currently, it only scales when I hover on the picture. I understand that the scale property overwrites the first transform but I can't s ...

"Spinning Effect in CSS with a Pulsating Twist

I am attempting to create a unique CSS animation that involves rotating an image and giving it a pulsing effect similar to the animation seen on Shazam's button. Here is the code that I have so far. While the image is successfully rotating, I am runn ...

Trouble with partial page displaying incorrect CSS class

I have created CSS classes for generating circles with different colors. When I apply the CSS class in a regular HTML file, it displays correctly as expected. However, when I try to use it in an AngularJS partial page, the circle does not render properly. ...

Implementing inline scrolling using CSS

My objective is to have each article added to the container div, "inlineExample", load to the right without any vertical overflow. The CSS should be able to handle each additional article dynamically as they are added to the right. <div id="inlineExamp ...

VS code showing live server as installed but failing to function properly

After installing the live server extension, I noticed that my browser is not updating when I save my HTML or other files. What could be causing this issue? ...

Diverse Range of Exports Available in React Component Library

I have been working on developing a component library consisting of multiple independent components. My objective is to enable users to easily import and use these components in their projects, similar to the following: import One from 'component-lib ...

What is the best way to modify the color of the active tab I have chosen?

I've designed a tab menu using Divs and CSS, but I'm facing an issue with changing the color of the selected tab. I tried adjusting the background color of the .tab class, but it seems to only work for part of the tab... You can view the problem ...