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
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


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


    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.



    Become a Member to view code

    You must be a Pro 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
    orLog In




    Instructor: 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.

    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.

    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.

    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.

    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.

    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.

    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.