惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
J
Java Code Geeks
S
SegmentFault 最新的问题
L
LangChain Blog
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
IT之家
IT之家
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
H
Help Net Security
阮一峰的网络日志
阮一峰的网络日志
Jina AI
Jina AI
N
Netflix TechBlog - Medium
A
About on SuperTechFans
博客园 - 叶小钗
美团技术团队
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net

Next.js Blog

How we closed 1,500 GitHub issues in one month How Turbopack chunks your JavaScript August 2026 Security Release Update: August Next.js Security Release Upcoming Next.js August Security Release Building App-like Experiences with Next.js 16.3 Making navigations instant in v0 Next.js 16.3 July 2026 Security Release Next.js Security Release and Our Next Patch Release Turbopack: What's New in Next.js 16.3 Next.js 16.3: AI Improvements Next.js 16.3: Instant Navigations Next.js Across Platforms: Adapters, OpenNext, and Our Commitments Next.js 16.2: AI Improvements Next.js 16.2 Turbopack: What's New in Next.js 16.2 Building Next.js for an agentic future Inside Turbopack: Building Faster by Building Less Next.js 16.1 Next.js Security Update: December 11, 2025 Security Advisory: CVE-2025-66478 Next.js 16 Next.js 16 (beta) Next.js 15.5 Next.js 15.4 Next.js 15.3 Building APIs with Next.js Next.js 15.2 Composable Caching with Next.js
Styling Next.js with Styled JSX
2019-03-28 · via Next.js Blog
Back to Blog

Thursday, March 28th 2019

Posted by

Styled JSX is a CSS-in-JS library that allows you to write encapsulated and scoped CSS to style your components. The styles you introduce for one component won't affect other components, allowing you to add, change and delete styles without worrying about unintended side effects.

Getting started

Next.js includes Styled JSX by default, so getting started is as simple as adding a <style jsx> tag into an existing React element and writing CSS inside of it:

In this example, we're including styles for the component's container element and a paragraph. Even though we are using generic selectors, the styles don't affect elements with the container class name or <p> tags in other components. This is because Styled JSX ensures the styles are scoped to this component only (by applying additional unique class names to styled elements).

By adding just a single jsx attribute to a <style> element, you can write standard CSS that gets auto prefixed and automatically scoped to the component. <style jsx> elements should be placed inside the root element of the component.

Adding global styles

Most projects need some global styles to style the body element or provide css resets. Styled JSX allows us to add global styles using <style jsx global>. For example:

This applies a 20px font-size to all <p> tags in this specific page.

To apply global styles to all pages in our app, a good approach is to first create a layout component with the global styles, then wrap all pages with it.

Using a layout component provides the flexibility to apply a certain set of styles to some pages while still allowing a different style for others:

In Next.js, we can load the layout once for all pages by creating a custom App component within pages/_app.js, importing the Layout component, and then adding it to the render method as follows:

Writing styles in external files

We can also write styles in external files outside of the component.

For example, we can move our global styles from the Layout component into a separate file as follows:

We can then import the styles back into the Layout component:

One-off global selectors

The styles that we add to a component using <style jsx> affect only the elements inside that component, but not child components.

At times, we may need to override a certain style of a child component. To do this, Styled JSX provides :global(), giving access to one-off global selectors.

For example, let's say we have a <Widget> component that contains a button with the class name btn. If we want to change the colors of this button only when the widget is imported on the homepage, we can do so like this:

Dynamic styles

Compared to other solutions, being able to adapt the styling of a component based on its props is a big advantage of CSS-in-JS libraries.

With Styled JSX we can do so like this:

If the Alert component is passed a type prop with a warning value like:

the component will have an orange background. Without specifying the type prop, the background would fallback to the default grey color.

Note that we placed the dynamic style into a separate <style jsx> tag. This isn't required, but it's recommended to split up static and dynamic styles so that only the dynamic parts are recomputed when props change.

An alternate approach to adapting styles based on props is applying different class names based on the prop value as shown below:

Creating a site theme

A theme can be a simple object where we include the most common variables we might need in our app:

We then import this theme file in our components and replace hardcoded values with variables:

In this blog post, we covered how to get started with Styled JSX. To learn more about additional features, be sure to check it out on GitHub.