CSS Cascade Layers Tutorial #4 - Importing Into Layers
Key Takeaways
CSS Cascade Layers and importing CSS files into layers using the at-import and at-layer rules, with a focus on organizing and prioritizing styles in a stylesheet.
Full Transcript
okay then so we've seen how to make layers and put our Styles inside different layers in order of priority in a star sheet now I want to talk about how we can import CSS files directly into layers so it might be for this project that you're working on the styles are split up into several different files so we have a components file for styling different UI components like buttons and cards we have a Content file for styling the typography the links and other content like images we have a layout file for all the layout Styles a reset file full of reset Styles we've got a theme file for all those theme variables and then we've got a utils file for all the utility classes so each Star sheet then could be imported into this main.css file so that we can just load this one main stylesheet into the HTML documents and the way we import CSS files is just by saying at import and then the path name of the file so in this case reset. CSS for example and we do that for each Star sheet but if we just import them like this it would be the same as if we're just writing them all out in order in this main file and there'd be no layers there'd just be a mix of all of these different CSS selectors in this kind of global scope right ideally we'd want to put each import into its own layer and we can do so we've already defined the layers and the Order of the layers up at the top of the file and now we can just tack on the layer rule after the Imports themselves to apply the import to a specific layer in parentheses so let's do this for all the different CSS files that we've got now all right then so I'm just going to duplicate this a few times and we'll change this one right here to theme not in capitals theme and we'll change this to theme as well we'll do the same down here this one's going to be layout this one's going to be layout and by the way they don't have to be in order right here because we've already dictated the order right up here they could be jumbled up here but I'm going to keep them in the same order so it's easy to look at all right so content right here and then content right here the next one is components the components and components and then reset oops not reset it should be utility and utility this is actually called utils should have kept it the same name really anyway now we have all of our different imported CSS files being applied to a specific layer and that is the same now as writing all the layers down here like layer reset and then putting the code in here it does the same thing we're just importing from other files now all right and we can see now in the browser everything looks good as it should do it's all styled correctly so what I'm going to do just to show you that this is still working in is I'm going to rearrange this I'm going to take components where we have the button class and I'm going to place it up here somewhere so let's cut that and let's place it before theme after resets like so and now we can see the Styles have changed so the button up here isn't working this doesn't look good Etc all right so this is working we've verified that by changing the order of these so ideally we want to keep this in the right order so let's grab that and paste it over here again so then we can also import third party line inter layers within our sty sheet as well for example we might be using the Bulma CSS library in our project but if we just link to it from our HTML file then it might cause conflicts with some of our Styles instead we could import the Bulma CSS file into the main style sheet and apply a layer to it so first up we Define the Bulma layer in the position within the stack that we want to use it now with a library like Bulma which contains utility classes layout classes typography classes Etc the position of the layer is a bit less obvious it could probably go somewhere in the middle after the reset and theme but maybe before the rest that way our own custom layout content and utility classes get priority over the Bulma ones but like I said it's not so obvious and it might be you want to place it somewhere else but anyway now we've defined its position we can import the Bulma file using at import and then we just paste in the file path or you to this import and then we can apply a layer to it by saying at layer and then inside parentheses whatever the layer was called in our case Bulma and now this third party library has its own layer within our stylesheet which is awesome so there we go my friends hopefully that gives you a head start when it comes to working with Cascade layers in [Music] CSS oh
Original Description
Learn all about CSS Cascade Layers & how to use them in this CSS series.
🚀🥷🏼Get early access to this entire course now on Net Ninja Pro:
https://netninja.dev/p/css-cascade-layers-crash-course
📂🥷🏼 Access the course files on GitHub:
https://github.com/iamshaunjp/cascade-layers-tutorial
💻🥷🏼 HTML & CSS Crash Course:
on YouTube - https://www.youtube.com/watch?v=hu-q2zYwEYs&list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G
on Net Ninja Pro - https://netninja.dev/p/html-css-crash-course
🔗🥷🏼 VS Code - https://code.visualstudio.com/
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: HTML & CSS
View skill →Related Reads
📰
📰
📰
📰
How I made a scroll-scrubbed video portfolio fast (Next.js 15 + GSAP + canvas)
Dev.to · Pratham Sharma
5 Reasons HTML Is About to Change Frontend Development
Medium · Programming
5 Reasons HTML Is About to Change Frontend Development
Medium · JavaScript
copilot browser tools make the frontend reviewable
Dev.to · Paulo Victor Leite Lima Gomes
🎓
Tutor Explanation
DeepCamp AI