Part 10: Elevating Your Next.js Application with Component-Scoped Styles

[Pages] Styling

[Pages] Styling

In our previous exploration, we added a global CSS file to style our Next.js application. While global styles are excellent for consistency, they can become cumbersome as your app grows. This is especially true when you want different styles for similar elements in various components. Let's dive into how we can use component-scoped styles to keep our CSS organized and maintainable.

The Problem with Global CSS

As your application expands, having all styles in a global CSS file can lead to unintended styling conflicts. For instance, rules applied to ul and li elements in a navigation bar might inadvertently affect list elements elsewhere on your site, like on the HomePage. This can result in lists displaying incorrectly, such as appearing inline when they should have bullet points.

Introducing Component-Scoped Styles

To avoid these conflicts, we can use component-scoped styles, which apply CSS rules only to specific components. Next.js offers several options for component-scoped styles, including styled-jsx and CSS Modules. We'll focus on styled-jsx for this tutorial, a library that enables scoped styling directly within your components.

Implementing Styled-JSX

Step 1: Remove Global Styles

First, comment out or remove the styles for the navigation bar from your global CSS file to avoid conflicts.

Step 2: Add Scoped Styles to Components

Let's implement styled-jsx in the NavBar component:

// File: components/NavBar.js
function NavBar() {
  return (
    <nav>
      <ul>
        <li>
          <Link href="/">Home</Link>
        </li>
        <li>
          <Link href="/about">About</Link>
        </li>
      </ul>
      <style jsx>{`
        ul {
          list-style-type: none;
          padding: 0;
        }
        li {
          display: inline;
        }
        li:not(:first-child) {
          margin-left: 0.75rem;
        }
      `}</style>
    </nav>
  );
}

export default NavBar;

How Styled-JSX Works

  • Scoped Styles: The styles defined within the <style jsx> block only apply to the elements within the NavBar component.

  • Automatic Class Generation: Styled-jsx automatically generates unique class names for your components to ensure styles don't clash with others on the page.

Step 3: Verify Styling

With the styles scoped to the NavBar, you can now add a list to your HomePage without affecting the navigation bar:

// File: pages/index.js
function HomePage() {
  return (
    <>
      <Head>
        <title>My Blog</title>
      </Head>
      <main>
        <h1>My Blog</h1>
        <ul>
          <li>One</li>
          <li>Two</li>
          <li>Three</li>
        </ul>
      </main>
    </>
  );
}

export default HomePage;

Benefits of Component-Scoped Styles

  • Isolation: Ensures styles are applied only to the intended components, preventing unwanted side effects.

  • Maintainability: Makes it easier to manage styles as your application grows, since styles are co-located with component logic.

  • Reusability: Facilitates component reuse without worrying about conflicting styles.

Conclusion

By leveraging styled-jsx, we've effectively scoped styles to individual components, enhancing both the maintainability and scalability of our Next.js application. While this tutorial focused on styled-jsx, you can explore other CSS-in-JS solutions or CSS Modules, all of which integrate smoothly with Next.js. This approach not only keeps your styles organized but also ensures they are applied precisely where needed.

Whether you choose styled-jsx or another solution, component-scoped styles provide a modern, efficient way to manage CSS in React applications. Happy coding, and enjoy the newfound flexibility in styling your components!

Last updated