Next.js / Styling in Next.js

Introduction to styling in Next.js

In this tutorial, you will learn the basics of styling in Next.js. We will cover how to apply styles to your Next.js components and pages to make your application visually appeali…

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Learn how to style your Next.js applications using CSS-in-JS and built-in CSS and SASS support.

Introduction

This tutorial is designed to provide you with a solid understanding of how to apply styling in Next.js, a popular React.js framework. By the end of this tutorial, you will have gained the knowledge of how to style your Next.js components and pages using CSS-in-JS approach, global styles, and CSS modules.

You Will Learn:
- Understanding of CSS-in-JS in Next.js.
- How to apply global styles.
- How to use CSS Modules.

Prerequisites
- Basic understanding of Next.js.
- Basic knowledge of CSS.

Step-by-Step Guide

CSS-in-JS

Next.js uses a CSS-in-JS solution, styled-jsx, that allows you to write CSS in JavaScript. It's a powerful and flexible way to style your components. Here’s an example:

export default function Home() {
  return (
    <div>
      <h1>Hello, World!</h1>
      <style jsx>{`
        h1 {
          color: red;
        }
      `}</style>
    </div>
  )
}

The styles you define will be scoped to the component, preventing unwanted side effects in other parts of your application.

Global Styles

For global styles, you can create a styles folder in your project root, then create a global.css file. You can import this file in your _app.js:

import '../styles/global.css'

export default function App({ Component, pageProps }) {
  return <Component {...pageProps} />
}

In your global.css file, you can define styles that apply to all components:

body {
  margin: 0;
  padding: 0;
  font-family: Arial, sans-serif;
}

CSS Modules

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.

import styles from './button.module.css'

export default function Button() {
  return <button className={styles.error}>Destroy</button>
}

Code Examples

Example 1: CSS-in-JS

export default function Home() {
  return (
    <div>
      <h1>Welcome to Next.js!</h1>
      <p>Let's style with CSS-in-JS</p>
      <style jsx>{`
        h1 {
          color: blue;
          font-size: 2em;
        }
        p {
          color: green;
        }
      `}</style>
    </div>
  )
}

Example 2: Global Styles

First, create a new global.css file in your styles directory and add the following:

body {
  background-color: lightgray;
}

Then, import the global.css in _app.js:

import '../styles/global.css'

Example 3: CSS Modules

First, create a new file called button.module.css in your styles directory:

.error {
  color: white;
  background-color: red;
}

Then, import and use it in your component:

import styles from '../styles/button.module.css'

export default function Button() {
  return <button className={styles.error}>Error Button</button>
}

Summary

In this tutorial, you learned how to apply styling in Next.js using CSS-in-JS, global styles, and CSS modules. Now you can confidently style your Next.js applications.

Practice Exercises

  1. Exercise 1: Create a component and style it using CSS-in-JS.
  2. Exercise 2: Apply a global style to change the background color of your application.
  3. Exercise 3: Create a button component and style it using CSS modules.

Solutions

  1. Solution to Exercise 1:
export default function Home() {
  return (
    <div>
      <h1>Welcome to Next.js!</h1>
      <p>We are learning to style with CSS-in-JS</p>
      <style jsx>{`
        h1 {
          color: purple;
          font-size: 2em;
        }
        p {
          color: black;
        }
      `}</style>
    </div>
  )
}
  1. Solution to Exercise 2:

In your global.css file:

body {
  background-color: #F0F0F0;
}
  1. Solution to Exercise 3:

In your button.module.css file:

.success {
  color: white;
  background-color: green;
}

In your component:

import styles from '../styles/button.module.css'

export default function Button() {
  return <button className={styles.success}>Success Button</button>
}

Happy coding! Keep practicing and exploring more about Next.js styling.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Date Difference Calculator

Calculate days between two dates.

Use tool

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

Scientific Calculator

Perform advanced math operations.

Use tool

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

MD5/SHA Hash Generator

Generate MD5, SHA-1, SHA-256, or SHA-512 hashes.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help