What causes my CSS styles to vanish upon refreshing the page in Next.js?

After setting up a CSS folder within my app directory and importing the files into components for use, I noticed that the styles are applied initially. However, upon refreshing the page, they all disappear.

I attempted to resolve this issue by going back and saving the CSS files again, which worked temporarily. But every time I refresh the page, the styles vanish once more.

Answer №1

When working with Next.js, it is not possible to load CSS directly inside individual page components. Instead, CSS can only be loaded within the pages/_app.js file.

import '../styles.css'

// This default export is necessary in a new `pages/_app.js` file.
export default function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

If you need to use CSS inside specific components, they must be CSS modules.

For example: components/layout.js

import styles from './layout.module.css';

export default function Layout({ children }) {
  return <div className={styles.container}>{children}</div>;
}

In components/layout.module.css

.container {
  max-width: 36rem;
  padding: 0 1rem;
  margin: 3rem auto 6rem;
}

For more information, visit here.

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

How can I align two inner boxes vertically in the most efficient manner?

.dd{ height:100px; width:100%; border:1px soild red; background:#000; } .dd .d1{ height:20px; width:20px; border:1px solid green; display:inline-block; } .dd .d2{ height:20px; width:20px; border:1px solid green; display:inl ...

Click event in jQuery triggers element movement

I'm having a tough time figuring this out. I initially thought it was the negative margin causing the issue, but even after removing it, the problem persists. Check out the link below to see what I mean when you click on one of the list items at the ...

Validate a field in a Formik form using React JS

Currently, I am utilizing Formik along with Yup for a project that is in progress. I am in the process of creating a multi-step form. The goal is to display the second step inputs when the user clicks on the next button, only if the inputs from step 1 are ...

Is it possible to shift the div inline-block using the CSS :after selector?

I'm looking to adjust the placement of the disk icon so that it aligns more with the baseline of the text without increasing the vertical space between lines. Below is the CSS code I've worked on so far: .openPage:after { content: ' &a ...

Puppeteer App Error: An error has been detected on the client side

I am facing an issue using Puppeteer with NEXT.JS while attempting to capture a screenshot. Everything runs smoothly on localhost, but in production, the captured image comes back with the following error message: Application error - a client-side exceptio ...

The error message "Next.js: rt.toLowerCase is not a function" indicates a problem

Upon creating a fresh next.js application using version 14.2.1, I encountered the following error: uncaughtException: TypeError: rt.toLowerCase is not a function at C:\Users\Mohamed\Desktop\next_projects\ecommerce\ecommer ...

Alert: Font preload was not utilized within a short timeframe after the window's load event

I've been working on optimizing the loading speed of fonts on my website, so I added the following: <link rel="preload" href="{{ '/css/fonts/bebasneue-webfont.woff' | prepend: site.baseurl | prepend: site.url }}" as="font" type="fo ...

Move your cursor over an image within a div container

After not finding any helpful answers on this topic, I've decided to ask for help again. My issue involves having an image inside a div. <div id ="gratterlendis"> <a href ="?page_id=8"><img src="img/gratterlendis.png" align="right" wid ...

Develop a NextJS application that continuously retrieves items from an API in a loop every 3 seconds

I'm currently utilizing Prismic to develop a Next.js/React application. Through the SSR API, I'm retrieving a list of items [slice.items]. Within my component, I'm attempting to update the item every 3 seconds. However, issues arise when ...

Tips for achieving a scrollable table in the shadcn/ui framework?

I am attempting to create a layout with a fixed TableHeader and a scrollable TableBody. While the current code accomplishes this, the content in the TableBody is being squished into the first column. How can I resolve this issue? export default function ...

The issue with the Next.js navbar is that the highlighting does not disappear when a link from a different <nav> group is clicked

Currently, I am working on setting up a navbar with active link highlighting using Next.js (and React-bootstrap). I have been following a guide on how to achieve this from this source. While some aspects of the highlighting are functioning as expected, ot ...

Creating a consistent height for Bootstrap 5 Carousel items with varying sizes

I am currently utilizing Bootstrap 5 carousel to display a collection of user-uploaded images that vary in height and width. I am seeking to create a carousel with responsive height and scaled/filled images. Below is the HTML + CSS code snippet I am using ...

Perpetual spinning of image grid

Are there any jQuery libraries available for continuous image rotation within an array? I have a collection of 30 partner logos that I would like to rotate continuously using either a jQuery library or plain CSS code. These images and information will be l ...

display a loading spinner for number input fields in HTML5

In my HTML5 project, I am currently utilizing a numeric control (input type="number"). The default behavior displays the spinner (up and down arrows) only on hover. Is there a way to make the spinner permanently visible using CSS or another method? ...

How does the use of <ol> with list-style-type: disc; differ from that of <ul>?

Why create a separate <ul> when visually both unordered lists and ordered lists look the same? <head> <title>Changing Numbering Type in an HTML Unordered List Using CSS</title> <style> ol { list-s ...

Comparing input size attribute to div width attribute

I have a situation in my html page where I need an input component and a div component to be the same width. The input has a size attribute of 30, but using the style attribute with "width: 30ch" or "width: 30em" for the div doesn't seem to work as ex ...

Utilizing jQuery to apply a class to an element and modify the CSS parameters with custom values simultaneously

Unsure if this idea is feasible, but I'll attempt to explain my requirements. I am interested in accomplishing something like the following: A CSS class with variables X, Y, and Z left undefined: .r {border-radius:Xpx;-webkit-border-radius:Ypx;-moz ...

Creating a PDF from slides using tools like mkd2pdf or wkhtmltopdf has never been easier. This

I am exploring the possibility of creating PDF files using mkd2pdf, a tool based on wkhtmltopdf. Mkd2pdf has the ability to generate PDFs from markdown content and utilizes a CSS file for styling purposes. For example: h1 { page-break-before: always; } ...

The Angular Material table does not adapt to different screen sizes

I developed an Angular application using Angular Material that features a table with 22 columns. However, when I expand the browser window, some columns are hidden. Additionally, on mobile browsers, not all columns are displayed. I have attempted the follo ...

Incorrect Pixel Width with Relative Positioning in Internet Explorer 11

I'm encountering a positioning problem that seems to be specific to Internet Explorer (11). I'm attempting to absolutely position a div tag in relation to another element. The problematic code snippet appears as follows: <div id="FacebookWra ...