Next.js Tutorial #6 - Adding Styles

Net Ninja · Beginner ·🌐 Frontend Engineering ·5y ago
Skills: HTML & CSS80%

Key Takeaways

Adds global styles and CSS modules to a Next.js project using globals.css and CSS modules

Full Transcript

all right then so we've created so far a layout including a navbar and a footer and some content in between for each page but currently it looks horrible so what i'd like to do is add some css to this to make it look at least semi-decent now you can add css to next project in various different ways you can use global style sheets for global common styles across the whole website you can use something called styles jsx which lets you write css in a react component or we can use css modules which next has built-in support for whereby each component can have its own unique scoped style sheet now what we'll be doing is using a mix of a global style sheet and css modules so first of all let's create the global style sheet and in fact we don't need to create it because it already exists and it's inside the styles folder right here and it's called globals.css so these are the global styles that come along for the ride when we create a new boilerplate next application what i'm going to do though is delete all that because i don't want any of those and we're just going to paste in our own styles so i'm going to grab these from my repo over here woohoo and remember the link to that repo is down below so you can copy and paste them as well and the reason i'm copying and pasting these in is because i don't want to spend a lesson boring you with css when that's not what you're here to learn so i'm just pasting them in but i will walk you through them very quickly so remember anything inside this global.css file is going to apply to anything on the whole website so the first thing i'm doing at the top is importing a google font called nunito with these different weights all right so then what i do is inside the body is apply a background of this light gray color the text is a dark gray color and then the font family of everything is nonito which is this font i imported okay so now every anchor tag has this color text and a text decoration of not to remove the underline this content class right here remember is inside the layout i think yep this thing right here so this has a class of content and we start it right here we get a max width of 960 pixels and a margin of zero top and bottom auto left and right and that centralizes all of the content on the page in a central column of this width then the nav which is in the layout remember the nav bar if i open this up is this right here so we're styling that we give this a bit of margin some padding we display as flex and we justify the content as flex end meaning everything goes to the right so all of the links are going to go over to the right and we align items to the flex end as well meaning they sit at the bottom of the container now the border bottom of the nav is one pixel solid and a light gray then the anchor tags inside that would give a margin left of 12 pixels the logo which is the h1 currently in the navbar so this div right here we say give this a margin right of auto so what that does is instead of letting that appear over on the right with the rest of the links because of this flex end it applies a margin right to it and puts it way over to the left so the logo will sit on the left and all the links on the right and then finally we have some styles for the footer display as block text align center bit of padding but imagine top the color is a medium gray of the text and then we have a border top as well so if we save this now we should see those styles take effect and that looks a bit better now so we've styled the general layout here using some global styles so we've not done anything page specific here just the layout okay so now oops i don't think the styles of the footer at the bottom have been applied let me just do a hard refresh because it's not sitting in the middle footer and let's go to the layout we have the footer there inside the footer okay that's why i'm going to change this from a div to a footer like so because that's what we're targeting and now if we save it okay that looks a bit better cool all right so how are these styles loaded in well if we go to where the root component is inside pages this app.js file over here we can see that at the top we import the global css file and that means that everything is going to have those styles applied to it because all of our pages are rendered here all right so they apply everywhere so we have to import those global styles right here for them to work so that's the global styles done what about page specific styles well for that we're going to be using css modules so css modules allow us to write a style sheet for each page component or section and then we import that style sheet into whatever component needs it and next is automatically going to scope those styles to that component by adding random characters next to the class names and selectors and that means that those styles will only apply to that component so you don't have to worry about class name conflicts between different components and their style sheets because those components can each have their own scoped styles right so right now we can already see if we open up the styles folder we have this home module right here and this module is being imported into the home page so this thing right here at the top so we import styles from this thing right here and then we can use this inside this component to add class names so if we take a look at this file first of all notice it's called home then dot module.css so first of all modules must have this naming convention so whatever the file is called dotmodule.css now inside here let's just use one of these classes i'm just going to use this class right here and in fact i'm going to add to it i'm going to say background is going to be red so it's very obvious that we're using it and i'm going to save this now what if i want to use this class inside one of my components well we're already importing it into this component right here we say import styles from this file so what i want to do is now apply this container class right here to for example this div well the way i do that is by saying class name first of all and then setting it equal to something dynamic so curly braces and then inside we use styles which is this right here that we import dot and then the class name so that remember is container i think yep so we'll say styles dot container like so and what that does is apply that class to this div so if we take a look we can now see that this is all red all right now if we inspect this then we can see right here that this class has a random set of characters at the end of it right here and it says home in front of it and this is what scopes these classes to the components because if we use this inside the about component it would be called something else this class and it sculpts it to the component that it's used in so if we use the same class names in different components it doesn't matter they're not going to clash with each other because they're edited they're changed to something unique to that component right and we can see inside the head that those styles are going to be imported and it's this one right here we can see the background red and the selector has those random characters on as well so next does all of this in the background for us and it makes it really easy then to scope styles and now if i wanted to what i could do is create another component another css module rather for my about component and have styles for the about page in there and if i have a class name of container again it doesn't matter because they're going to be scoped to those files they're not going to clash with each other all right then so what i'm going to do is actually again delete all of this bump and i'm just going to paste in my own styles which again i'm going to grab from my repo over here so let me just grab these things and i'm going to paste them in over here into home.module.css so we have three classes a title and some text and then a button so what i'm going to do is use these three classes inside our home components so let me get rid of this first of all we don't want that to have a class name of title anymore but i do want this to have a class name equal to styles first of all dot title and then i'm going to just copy this and paste it down here like so and like so i'm going to change this to text because we have a class called text and this as well and then finally this anchor tag i'm going to give a class name equal to styles.btn that was the third class we had so let me just spell this correctly if we save this and preview now we should see that looks a lot better okay cool all right then so finally what i'm going to do is create one more file and this is going to be called ninjas dot module dot css and this is going to be for this ninjas component over here later on i'm not going to place anything inside this at the moment i'm just creating it so that later on when we flesh out this ninja's index component we can use that css module inside this component i'm not going to create one for the about component because i don't really think that needs any more styles if we take a look at this it's very basic i don't want to apply any more styles to that now there is one more thing i want to mention about these css modules right here and that is that the selectors must be pure selected meaning you need to use class selectors and not element selectors for example you couldn't just write a selector for paragraph elements like this they need to be classes which we can then apply inside the components if i can find the component that we use them in this one over here like this so that styles next up we're going to take a look at how to create a custom 404 page

Original Description

Hey gang, in this Next.js tutorial we'll see how to add global styles & also CSS modules to our Next project. 🐱‍💻 🐱‍💻 Course Files: + https://github.com/iamshaunjp/nextjs-tutorial 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - https://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Helpful Links: + Full React Course - https://www.youtube.com/watch?v=j942wKiXFu8&list=PL4cUxeGkcC9gZD-Tvwfod2gaISzfRiP9d + HTML & CSS Course - https://www.youtube.com/watch?v=hu-q2zYwEYs&list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G + Modern JavaScript course - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc + Get VS Code - https://code.visualstudio.com/ + Next.js docs - https://nextjs.org/docs/getting-started 🐱‍💻 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

Up next
How to Speed Up Your WordPress Website with WP Rocket ⚡Tutorial 2026
Matt Tutorials
Watch →