Next.js Series #2-CSS Support

Search for a command to run...

No comments yet. Be the first to comment.
Bun, the hyper-fast JavaScript runtime known for its incredible speed and Zig-based architecture, is being completely rewritten in Rust. For a project that has over 22 million monthly downloads and ea

Next.js 16.3 is almost here, and it's packed with a ton of improvements and let's see them in this post 1.Instant Navigation For a while now, there’s been a valid, lingering debateServer Components vs

It’s official. React has merged its long-awaited Rust port of the React Compiler (formerly known as React Forget) into the main repository. What started as an experimental research project by Joseph S

If you follow modern technology trends, you have likely been led to believe that artificial intelligence is entirely built on Python. Every tutorial, machine learning framework documentation, and open

Just when the JavaScript ecosystem was catching its breath after last week’s TanStack Router compromise, the Mini Shai-Hulud supply chain worm has continued its relentless march. Developed by the thre

If you don’t have time to read but want to know what’s there in this post. Find the quick read 👇

Creating a Global Stylesheet in Next.js
Import styles from node_modules
Create Component-Level CSS
Sass support
CSS in JS
Create a stylesheet named style.css and then add the below snippets

These styles (style.css) will apply to all pages and components in your application. Due to the global nature of stylesheets, and to avoid conflicts open pages/index.js and then import the css like below

In development, expressing stylesheets this way allows your styles to be hot reloaded as you edit them — meaning you can keep application state.
In production, all CSS files will be automatically concatenated into a single minified .css file.
Since Next.js 9.5.4, importing a CSS file from node_modules is permitted anywhere in your application.
For global stylesheets, like bootstrap or nprogress, you should import the file inside pages/index.js. For example:

Next.js supports CSS Modules using the [name].module.css file naming convention.
CSS Modules locally scope CSS by automatically creating a unique class name. This allows you to use the same CSS class name in different files without worrying about collisions.
This behavior makes CSS Modules the ideal way to include component-level CSS. CSS Module files can be imported anywhere in your application.
Create a new component level css like below by creating a button component and then the css for it like Button.js and Button.module.css

Here you can see the component level css that is been imported to the Button.js file
CSS Modules are an optional feature and are only enabled for files with the .module.css extension. Regular stylesheets and global CSS files are still supported.
In production, all CSS Module files will be automatically concatenated into many minified and code-split .css files. These .css files represent hot execution paths in your application, ensuring the minimal amount of CSS is loaded for your application to paint.
Next.js allows you to import Sass using both the .scss and .sass extensions. You can use component-level Sass via CSS Modules and the .module.scss or .module.sass extension.
Before you can use Next.js’ built-in Sass support, be sure to install sass:
npm install --save-dev sass
Sass support has the same benefits and restrictions as the built-in CSS support detailed above.

If you want to configure the Sass compiler you can do so by using sassOptions in next.config.js.
const path = require('path')
module.exports = {
sassOptions: {
includePaths: [path.join(__dirname, 'styles')],
},
}
Next.js supports Sass variables exported from CSS Module files.
For example, using the exported primaryColor Sass variable:
/* variables.module.scss */
$primary-color: #64FF00
:export {
primaryColor: $primary-color
}
// pages/_app.js
import variables from '../styles/variables.module.scss'
export default function MyApp({ Component, pageProps }) {
return (
<Layout color={variables.primaryColor}>
<Component {...pageProps} />
</Layout>
)
}
It’s possible to use any existing CSS-in-JS solution. The simplest one is inline styles:
function HiThere() {
return <p style={{ color: 'red' }}>hi there</p>
}
export default HiThere
Next.js will bundle styled-jsx to provide support for isolated scoped CSS. The aim is to support “shadow CSS” similar to Web Components, which unfortunately do not support server-rendering and are JS-only.
