International vs. Native Styling In Subsequent.js

No Comments

I’ve had an incredible expertise utilizing Subsequent.js to handle advanced front-end initiatives. Subsequent.js is opinionated about how one can manage JavaScript code, however it doesn’t have built-in opinions about how one can manage CSS.

After working inside the framework, I’ve discovered a sequence of organizational patterns that I imagine each conform to the guiding philosophies of Subsequent.js and train greatest CSS practices. On this article, we’ll construct a web site (a tea store!) collectively to exhibit these patterns.

Observe: You in all probability won’t want prior Subsequent.js expertise, though it could be good to have a primary understanding of React and to be open to studying some new CSS strategies.

Writing “Previous-Long-established” CSS

When first trying into Subsequent.js, we could also be tempted to think about using some form of CSS-in-JS library. Although there could also be advantages relying on the venture, CSS-in-JS introduces many technical issues. It requires utilizing a brand new exterior library, which provides to the bundle measurement. CSS-in-JS may also have a efficiency impression by inflicting extra renders and dependencies on the worldwide state.

Really helpful studying: “The Unseen Efficiency Prices Of Fashionable CSS-in-JS Libraries In React Apps)” by Aggelos Arvanitakis

Moreover, the entire level of utilizing a library like Subsequent.js is to statically render property every time doable, so it doesn’t make a lot sense to jot down JS that must be run within the browser to generate CSS.

There are a few questions we have now to contemplate when organizing fashion inside Subsequent.js:

How can we match inside the conventions/greatest practices of the framework?

How can we stability “international” styling considerations (fonts, colours, fundamental layouts, and so forth) with “native” ones (types concerning particular person elements)?

The reply I’ve give you for the primary query is to merely write good ol’ original CSS. Not solely does Subsequent.js assist doing so with no extra setup; it additionally yields outcomes which are performant and static.

To resolve the second downside, I take an strategy that may be summarized in 4 items:

Design tokens
International types
Utility lessons
Element types

I’m indebted to Andy Bell’s thought of CUBE CSS (“Composition, Utility, Block, Exception”) right here. In case you haven’t heard of this organizational precept earlier than, I beneficial trying out its official web site or function on the Smashing Podcast. One of many rules we’ll take from CUBE CSS is the concept that we must always embrace quite than concern the CSS cascade. Let’s be taught these strategies by making use of them to a web site venture.

Getting Began

We’ll be constructing a tea retailer as a result of, properly, tea is tasty. We’ll begin by working yarn create next-app to make a brand new Subsequent.js venture. Then, we’ll take away all the things within the types/ listing (it’s all pattern code).

Observe: If you wish to comply with together with the completed venture, you’ll be able to test it out right here.

Design Tokens

In just about any CSS setup, there’s a transparent profit to storing all globally shared values in variables. If a consumer asks for a shade to alter, implementing the change is a one-liner quite than a large find-and-replace mess. Consequently, a key a part of our Subsequent.js CSS setup might be storing all site-wide values as design tokens.

We’ll use inbuilt CSS Customized Properties to retailer these tokens. (In case you’re not aware of this syntax, you’ll be able to take a look at “A Technique Information To CSS Customized Properties”.) I ought to point out that (in some initiatives) I’ve opted to make use of SASS/SCSS variables for this goal. I haven’t discovered any actual benefit, so I normally solely embrace SASS in a venture if I discover I would like different SASS options (mix-ins, iteration, importing information, and so forth). CSS customized properties, against this, additionally work with the cascade and will be modified over time quite than statically compiling. So, for at the moment, let’s persist with plain CSS.

In our types/ listing, let’s make a brand new design_tokens.css file:

:root {
–green: #3FE79E;
–dark: #0F0235;
–off-white: #F5F5F3;

–space-sm: 0.5rem;
–space-md: 1rem;
–space-lg: 1.5rem;

–font-size-sm: 0.5rem;
–font-size-md: 1rem;
–font-size-lg: 2rem;

After all, this checklist can and can develop over time. As soon as we add this file, we have to jump over to our pages/_app.jsx file, which is the principle format for all our pages, and add:

import ‘../types/design_tokens.css’

I like to think about design tokens because the glue that maintains consistency throughout the venture. We are going to reference these variables on a world scale, in addition to inside particular person elements, guaranteeing a unified design language.

International Types

Subsequent up, let’s add a web page to our web site! Let’s hop into the pages/index.jsx file (that is our homepage). We’ll delete all of the boilerplate and add one thing like:

export default operate House() {
return <fundamental>
<h1>Soothing Teas</h1>

<p>Welcome to our fantastic tea store.</p>

<p>We’ve been open since 1987 and serve prospects with hand-picked oolong teas.</p>

Sadly, it should look fairly plain, so let’s set some international types for primary components, e.g. <h1> tags. (I like to think about these types as “cheap international defaults”.) We might override them in particular circumstances, however they’re a very good guess as to what we’ll need if we don’t.

I’ll put this within the types/globals.css file (which comes by default from Subsequent.js):

*::earlier than,
*::after {
box-sizing: border-box;

physique {
shade: var(–off-white);
background-color: var(–dark);

h1 {
shade: var(–green);
font-size: var(–font-size-lg);

p {
font-size: var(–font-size-md);

p, article, part {
line-height: 1.5;

:focus {
define: 0.15rem dashed var(–off-white);
outline-offset: 0.25rem;
fundamental:focus {
define: none;

img {
max-width: 100%;

After all, this model is pretty primary, however my globals.css file doesn’t normally find yourself really needing to get too giant. Right here, I fashion primary HTML components (headings, physique, hyperlinks, and so forth). There isn’t a must wrap these components in React elements or to always add lessons simply to offer primary fashion.

I additionally embrace any resets of default browser types. Often, I’ll have some site-wide format fashion to offer a “sticky footer”, for instance, however they solely belong right here if all pages share the identical format. In any other case, it should should be scoped inside particular person elements.

I at all times embrace some form of :focus styling to clearly point out interactive components for keyboard customers when centered. It’s greatest to make it an integral a part of the location’s design DNA!

Now, our web site is beginning to form up:

Utility Lessons

One space the place our homepage may definitely enhance is that the textual content at present at all times extends to the edges of the display screen, so let’s restrict its width. We’d like this format on this web page, however I think about that we’d want it on different pages, too. It is a nice use case for a utility class!

I attempt to use utility lessons sparingly quite than as a alternative for simply writing CSS. My private standards for when it is smart so as to add one to a venture are:

I would like it repeatedly;
It does one factor properly;
It applies throughout a spread of various elements or pages.

I feel this case meets all three standards, so let’s make a brand new CSS file types/utilities.css and add:

.lockup {
max-width: 90ch;
margin: 0 auto;

Then let’s add import ‘../types/utilities.css’ to our pages/_app.jsx. Lastly, let’s change the <fundamental> tag in our pages/index.jsx to <fundamental className=”lockup”>.

Now, our web page is coming collectively much more. As a result of we used the max-width property, we don’t want any media queries to make our format cellular responsive. And, as a result of we used the ch measurement unit — which equates to concerning the width of 1 character — our sizing is dynamic to the consumer’s browser font measurement.

As our web site grows, we will proceed including extra utility lessons. I take a reasonably utilitarian strategy right here: If I’m working and discover I would like one other class for a shade or one thing, I add it. I don’t add each doable class beneath the solar — it could bloat the CSS file measurement and make my code complicated. Generally, in bigger initiatives, I like to interrupt issues up right into a types/utilities/ listing with a couple of completely different information; it’s as much as the wants of the venture.

We are able to consider utility lessons as our toolkit of widespread, repeated styling instructions which are shared globally. They assist stop us from always rewriting the identical CSS between completely different elements.

Element Types

We’ve completed our homepage for the second, however we nonetheless must construct a bit of our web site: the net retailer. Our aim right here might be to show a card grid of all of the teas we wish to promote, so we’ll want so as to add some elements to our web site.

Let’s begin off by including a brand new web page at pages/store.jsx:

export default operate Store() {
return <fundamental>
<div className=”lockup”>
<h1>Store Our Teas</h1>


Then, we’ll want some teas to show. We’ll embrace a reputation, description, and picture (within the public/ listing) for every tea:

const teas = [
{ name: “Oolong”, description: “A partially fermented tea.”, image: “/oolong.jpg” },
// …

Observe: This isn’t an article about information fetching, so we took the simple route and outlined an array at first of the file.

Subsequent, we’ll must outline a part to show our teas. Let’s begin by making a elements/ listing (Subsequent.js doesn’t make this by default). Then, let’s add a elements/TeaList listing. For any part that finally ends up needing multiple file, I normally put all of the associated information inside a folder. Doing so prevents our elements/ folder from getting unnavigable.

Now, let’s add our elements/TeaList/TeaList.jsx file:

import TeaListItem from ‘./TeaListItem’

const TeaList = (props) => {
const { teas } = props

return <ul position=”checklist”>
{ =>
<TeaListItem tea={tea} key={tea.identify} />)}

export default TeaList

The aim of this part is to iterate over our teas and to indicate an inventory merchandise for each, so now let’s outline our elements/TeaList/TeaListItem.jsx part:

import Picture from ‘subsequent/picture’

const TeaListItem = (props) => {
const { tea } = props

return <li>
<Picture src={tea.picture} alt=”” objectFit=”cowl” objectPosition=”middle” format=”fill” />


export default TeaListItem

Observe that we’re utilizing Subsequent.js’s built-in picture part. I set the alt attribute to an empty string as a result of the photographs are purely ornamental on this case; we wish to keep away from bogging display screen reader customers down with lengthy picture descriptions right here.

Lastly, let’s make a elements/TeaList/index.js file, in order that our elements are simple to import externally:

import TeaList from ‘./TeaList’
import TeaListItem from ‘./TeaListItem’

export { TeaListItem }

export default TeaList

After which, let’s plug all of it collectively by including import TeaList from ../elements/TeaList and a <TeaList teas={teas} /> aspect to our Store web page. Now, our teas will present up in an inventory, however it received’t be so fairly.

Colocating Type With Parts By way of CSS Modules

Let’s begin off by styling our playing cards (the TeaListLitem part). Now, for the primary time in our venture, we’re going to wish to add fashion that’s particular to only one part. Let’s create a brand new file elements/TeaList/TeaListItem.module.css.

You might be questioning concerning the module within the file extension. It is a CSS Module. Subsequent.js helps CSS modules and contains some good documentation on them. After we write a category identify from a CSS module comparable to .TeaListItem, it should routinely get reworked into one thing extra like . TeaListItem_TeaListItem__TFOk_ with a bunch of additional characters tacked on. Consequently, we will use any class identify we wish with out worrying that it’s going to battle with different class names elsewhere in our web site.

One other benefit to CSS modules is efficiency. Subsequent.js features a dynamic import function. subsequent/dynamic lets us lazy load elements in order that their code solely will get loaded when wanted, quite than including to the entire bundle measurement. If we import the required native types into particular person elements, then customers may also lazy load the CSS for dynamically imported elements. For big initiatives, we might select to lazy load vital chunks of our code and solely to load essentially the most obligatory JS/CSS upfront. Because of this, I normally find yourself making a brand new CSS Module file for each new part that wants native styling.

Let’s begin by including some preliminary types to our file:

.TeaListItem {
show: flex;
flex-direction: column;
hole: var(–space-sm);
background-color: var(–color, var(–off-white));
shade: var(–dark);
border-radius: 3px;
box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.1);

Then, we will import fashion from ./TeaListItem.module.css in our TeaListitem part. The fashion variable is available in like a JavaScript object, so we will entry this class-like fashion.TeaListItem.

Observe: Our class identify doesn’t should be capitalized. I’ve discovered {that a} conference of capitalized class names inside modules (and lowercase ones outdoors) differentiates native vs. international class names visually.

So, let’s take our new native class and assign it to the <li> in our TeaListItem part:

<li className={fashion.TeaListComponent}>

You might be questioning concerning the background shade line (i.e. var(–color, var(–off-white));). What this snippet means is that by default the background might be our –off-white worth. However, if we set a –color customized property on a card, it should override and select that worth as an alternative.

At first, we’ll need all our playing cards to be –off-white, however we might wish to change the worth for particular person playing cards later. This works very equally to props in React. We are able to set a default worth however create a slot the place we will select different values in particular circumstances. So, I encourage us to consider CSS customized properties like CSS’s model of props.

The fashion nonetheless received’t look nice as a result of we wish to ensure that the photographs keep inside their containers. Subsequent.js’s Picture part with the format=”fill” prop will get place: absolute; from the framework, so we will restrict the dimensions by placing in a container with place: relative;.

Let’s add a brand new class to our TeaListItem.module.css:

.ImageContainer {
place: relative;
width: 100%;
peak: 10em;
overflow: hidden;

After which let’s add className={types.ImageContainer} on the <div> that incorporates our <Picture>. I take advantage of comparatively “easy” names comparable to ImageContainer as a result of we’re inside a CSS module, so we don’t have to fret about conflicting with the surface fashion.

Lastly, we wish to add a little bit of padding on the edges of the textual content, so let’s add one final class and depend on the spacing variables we arrange as design tokens:

.Title {
padding-left: var(–space-sm);
padding-right: var(–space-sm);

We are able to add this class to the <div> that incorporates our identify and outline. Now, our playing cards don’t look so dangerous:

Combining International And Native Type

Subsequent, we wish to have our playing cards present in a grid format. On this case, we’re simply on the border between native and international types. We may definitely code our format instantly on the TeaList part. However, I may additionally think about that having a utility class that turns an inventory right into a grid format could possibly be helpful in a number of different locations.

Let’s take the worldwide strategy right here and add a brand new utility class in our types/utilities.css:

.grid {
list-style: none;
show: grid;
grid-template-columns: repeat(auto-fill, minmax(var(–min-item-width, 30ch), 1fr));
hole: var(–space-md);

Now, we will add the .grid class on any checklist, and we’ll get an routinely responsive grid format. We are able to additionally change the –min-item-width customized property (by default 30ch) to alter the minimal width of every aspect.

Observe: Bear in mind to think about customized properties like props! If this syntax seems to be unfamiliar, you’ll be able to take a look at “Intrinsically Responsive CSS Grid With minmax() And min()” by Chris Coyier.

As we’ve written this fashion globally, it doesn’t require any fanciness so as to add className=”grid” onto our TeaList part. However, let’s say we wish to couple this international fashion with some extra native retailer. For instance, we wish to deliver a bit extra of the “tea aesthetic” in and to make each different card have a inexperienced background. All we’d must do is make a brand new elements/TeaList/TeaList.module.css file:

.TeaList > :nth-child(even) {
–color: var(–green);

Bear in mind how we made a –color customized property on our TeaListItem part? Nicely, now we will set it beneath particular circumstances. Observe that we will nonetheless use youngster selectors inside CSS modules, and it doesn’t matter that we’re choosing a component that’s styled inside a distinct module. So, we will additionally use our native part types to have an effect on youngster elements. It is a function quite than a bug, because it permits us to make the most of the CSS cascade! If we tried to duplicate this impact another method, we’d probably find yourself with some form of JavaScript soup quite than three traces of CSS.

Then, how can we preserve the worldwide .grid class on our TeaList part whereas additionally including the native .TeaList class? That is the place the syntax can get a bit funky as a result of we have now to entry our .TeaList class out of the CSS module by doing one thing like fashion.TeaList.

One possibility could be to make use of string interpolation to get one thing like:

<ul position=”checklist” className={`${fashion.TeaList} grid`}>

On this small case, this may be ok. If we’re mixing-and-matching extra lessons, I discover that this syntax makes my mind explode a bit, so I’ll typically choose to make use of the classnames library. On this case, we find yourself with a extra sensible-looking checklist:

<ul position=”checklist” className={classnames(fashion.TeaList, “grid”)}>

Now, we’ve completed up our Store web page, and we’ve made our TeaList part make the most of each international and native types.

A Balancing Act

We’ve now constructed our tea store utilizing solely plain CSS to deal with the styling. You will have seen that we didn’t should spend ages coping with customized Webpack setups, putting in exterior libraries, and so forth. That’s due to the patterns that we’ve used work with Subsequent.js out of the field. Moreover, they encourage greatest CSS practices and match naturally into the Subsequent.js framework structure.

Our CSS group consisted of 4 key items:

Design tokens,
International types,
Utility lessons,
Element types.

As we proceed constructing our web site, our checklist of design tokens and utility lessons will develop. Any styling that doesn’t make sense so as to add as a utility class, we will add into part types utilizing CSS modules. Because of this, we will discover a steady stability between native and international styling considerations. We are able to additionally generate performant, intuitive CSS code that grows naturally alongside our Subsequent.js web site.

    About Marketing Solution Australia

    We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

    Request a free quote

    We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

    Subscribe to our newsletter!

    More from our blog

    See all posts

    Leave a Comment