Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In

Autoplay

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

    Thomas GrecoThomas Greco

    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.

    nextNext.js
    ^4.0.0
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    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.

    Discuss

    Discuss