The art of incorporating CSS style files in Next.js

Just starting out with Next.js and I could use some help.

I'm having trouble getting all of the styles and images to work properly even though I've declared the css files in _app.tsx. Some styles are not being applied and images from 'public/images' are not being imported. Can you help me figure out what's causing this issue? Do I need to reorganize my folder structure? The version of Next.js I'm using is "13.1.1".

Thank you in advance for any assistance!

Here is the folder structure for the project I'm working on:

  • public
    • fonts
    • images
  • src
    • pages
    • styles
      • global.css
      • layout.css

This is how my _app.tsx file is set up:

import '@/styles/layout.css';
import '@/styles/common.css';

export default function App({ Component, pageProps }: AppProps) {
 ...
}

Answer №1

Make sure to appropriately name your CSS file as layout.module.css. It is recommended to use the .module.css extension for your CSS files. For more information, you can refer to the nextjs documentation.

Answer №2

Next.js can be frustrating at times because it doesn't seem to respect the idea that "static content is static content." However, when it comes to CSS, you can still use traditional CSS files in a separate directory without having to deal with the complexities of "CSS modules" within JavaScript:

  1. If you don't already have a globals.css file in your app directory, create one.
  2. Include import "globals.css" in your global page.tsx file if it's not there already.

(These steps are usually taken care of if you used next create to set up your project directory)

  1. If you don't already have a separate folder for your .css file(s), create one.
  2. In globals.css, add a standard CSS @import url(...) for your file(s).
  3. That's it.

Now it's just a matter of regular web development work: design the CSS the way you want, especially now that SASS is no longer necessary thanks to CSS variables being supported since 2018, and nested rules being added in late 2023.

(For organization purposes, consider creating a master file like style.css so you only need one import in globals.css and can avoid touching that file again. If you have multiple CSS files, simply include @import statements in your master file, following the familiar structure of regular web development)

Answer №3

Just delving into the world of Nextjs and discovered that traditional CSS files won't work; instead, you have to utilize CSS modules.

Remember to change the file extension of each CSS file (except for global styles) to .module.css rather than .css. Next, import the file into the component you wish to style:

import styles from 'style/file.module.css'

To style an element, define a class in the CSS file: .paragraph{ color:green }

Then, apply it in the component:

        <p className={styles.paragraph}>I am styled with CSS modules</p>

Reference!

Consider exploring Tailwindcss, a standard CSS framework for your styling needs.

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

The Pin property in GSAP is behaving unexpectedly when used in NextJS

I recently tried to learn gsap and wanted to create a smooth animation where I could scroll down a page and have a long word scroll horizontally and get pinned while scrolling using the scrollTrigger plugin of gsap. I followed a tutorial but ran into an is ...

Having difficulty adjusting the text to match the image alignment

Having trouble aligning the text tag inside the H1 above the image. It keeps appearing in the wrong place, either on the left or right. I've tried including the h1 inside the container, but it just disappears. <!doctype html> <html> <h ...

Having trouble loading SVG component in Next.js with SVGR integration

I recently obtained an SVG react component that was automatically converted from the SVGR playground page. I integrated it into my project and followed the installation instructions for @svgr/webpack and configured the setup as advised. However, upon loadi ...

What is the method to adjust the font size for section, subsection, and other sections in Doxygen?

I am looking to customize the font size of \section and \subsection in doxygen's html output. Additionally, I want to include numbering for the sections and sub(sub)sections in the format: 1 Section1 1.1 Subsection1.1 1.1.1 Subsubsection ...

Ways to showcase a collection of divs in a dual-column layout while minimizing any unnecessary vertical spacing

Is there a way to create two columns without changing the HTML structure? The content within each child element is dynamic, so there needs to be dynamic vertical spacing as well. I'm experimenting with different CSS properties for both the parent and ...

How can I line up elements in different divs when the width is adjusting based on the window size?

Trying to align a search input and a result element in separate containers when the window size changes. Looking for a solution without using Javascript. One window size: https://i.sstatic.net/LiQtY.png A smaller window: https://i.sstatic.net/dgj4I.png C ...

Issue with Hidden Horizontal Scrollbar Functionality in JQuery Mobile Not Resolving

http://jsbin.com/UREQUbu/1/edit I have a header with 9 buttons. I managed to get the horizontal scrollbar to work, but I want it hidden yet still scrollable. I came across a JQuery plugin that worked perfectly for scrolling on my laptop, but unfortunately ...

How to Style Background with CSS on the Server-Side? (Including Links and Hover Effects)

I'm facing a dilemma in choosing between an ASP.NET control and an HTML element for creating a button with a CSS-defined background that changes on hover... 1. Initially, it seems the ASP.NET ImageButton control is not suitable for this purpose becau ...

Set the div element to be horizontally aligned

Is there a way to make this display horizontally instead of vertically, from left to right rather than top to bottom? I attempted using display:flex but only the extra-text class is affected and not the outer div. https://i.stack.imgur.com/2DNoC.png .m ...

The position of the HTML class shifts when the window is resized

I am in the process of creating a basic website layout that resembles this design. However, I am facing an issue where the webpage does not adapt well to different window sizes. Specifically, the elements with the classes .gameInfo and .playerList overlap ...

The scrollbar fails to reach the bottom of my table, preventing me from viewing the entire content

I'm currently facing a challenge with a table on my webpage that displays data but isn't scrolling all the way to the bottom. This code was implemented by previous developers, and as someone who isn't well-versed in CSS, I'm struggling ...

Designing a dynamic webpage featuring various elements using CSS and HTML

I'm just starting out with HTML and CSS for website design and I want to create a layout similar to the one shown below: https://i.sstatic.net/u5ill.png In the image above, you can see that the page includes a header with a logo, and two boxes that ...

For Flexbox front end design, the left side should be contained within a container while the right side should seamlessly stretch across the

https://i.sstatic.net/3RYY6.png I'm facing a challenge in designing a website with a unique layout that has left me puzzled. I've created a codepen example to showcase what I'm trying to achieve. Specifically, I need to implement a Hero Ba ...

Creating a draggable element in JavaScript using React

I've been attempting to create a draggable div element, but I'm encountering some perplexing behavior. The code I'm using is directly from w3schools and it functions correctly for them, however, in my implementation, the div always shifts to ...

SWR: Enhance user experience with real-time UI updates upon changing data

Is there a way to make the UI update instantly without waiting for SWR to sync with the database? I attempted to refer to the documentation, but even after following it, the UI does not update automatically (I have to switch back and forth in my browser t ...

Is it possible to have the navigation bar (bootstrap) appear on top of my slider as the default setting, without taking up additional space?

I'm encountering a rather simple issue that's giving me some trouble. I'm currently working on a website design using Bootstrap, and initially, there were no image sliders included by default. After integrating an image slider, I noticed th ...

Choosing a CSS Grid layout

New to web development, I have a design dilemma. Working on a game that teaches students how to multiply, I am unsure about the best way to display the multiplication process. This picture illustrates my issue: https://i.sstatic.net/F8ZGs.png Given that I ...

Tips for resolving the issue of receiving a warning about passing "onClick" to a "<Link>" component with an `href` of `#` while having "legacyBehavior" enabled in Next.js

My current project is displaying a lot of warnings in the browser console and I'm unsure about the reasons behind it. The warnings include: "onClick" was passed to with href of /discovery/edit but "legacyBehavior" was set. The l ...

“Tips for positioning text in the middle using HTML and CSS”

I'm trying to customize the text "TITLE" in my HTML and CSS code, #headerf { background-color: #717571; overflow-x: hidden; position: fixed; z-index: 99999; } .headert { position: fixed; z-index: 9999999; width: 100%; height: 60px; ...

The POST request did not yield an HttpResponse object; instead, it returned None

When I submit a selection from a dropdown form to views as a POST request, and then use this selection to query data from Django, I encounter an issue while trying to map Django models data to Highcharts following this approach. The error message "the view ...