⚠️ This lesson is retired and might contain outdated information.

Style Next.js Application's using CSS-in-JS and styled-jsx

Thomas Greco
InstructorThomas Greco
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 4 years ago

Next.js truly is an incredible framework as it provides users with out of the box support for css-in-js as well as styled-jsx. In this video, we'll take a look at these 2 choices and see how their usage differs from one another.

Instructor: [00:00] What if we wanted to implement some personal styles either within this application or with future applications? Luckily, Next.js provides us with a few different solutions for accomplishing this. Next.js comes with built-in support for CSS in JS. To use CSS in JS, we just need to add a style prop to whichever element that we wish to style.

[00:30] Inside these double curly brackets, we're then going to name CSS properties using CamelCase code. These properties are then given string values and are separated by a comma instead of the semicolon that's used in CSS. I'm going to set the text decoration to none, the font size to 18 pixels, and the color to white.

[01:01] If we save it and we visit our application, we see that this is already showing up for us. Taking a look at the console, if we click on this a tag for the button, we'll see that the styles we've defined have been embedded inline inside of our a tag. This is great, but this isn't the only thing that Next provides us with. Let's take a look at its built-in support for Style JSX.

[01:33] To use Style JSX, we need to open up a set of style tags inside of our component. We also need to append the JSX attribute onto our style tags. Inside these style tags, we need to open up a set of brackets, and then, use JavaScript's template strings. Inside here, we'll be able to define any CSS.

[02:00] Here, I'm defining a CSS class named, "Postlink." I'm going to give it the same properties that we just defined with CSS in JS. Whereas CSS in JS had a different syntax, you'll notice that all these styles adhere to the regular CSS properties. There's no more string values, and there's no more commas separating values.

[02:26] Now, if we go back to our page...Oh! We need to add this Postlink class to our a tag. As you can see, I'm using the React class name attribute. Now when I save that, we see that our styles do show up. If we take a look at the dev tools once again, we'll see that our styles are no longer inlined like they were when we used CSS in JS.

[03:01] Instead of doing this, Next is actually going to create its own style tags for each selector and place them in the head of the document. By navigating to the head of our document, we'll be able to find this style tag that Next created. Inside here, we can see the styles that we just defined within our component using that custom CSS selector.

egghead
egghead

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today