CSS Cascade Layers Tutorial #4 - Importing Into Layers

Net Ninja · Beginner ·🌐 Frontend Engineering ·2y ago

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 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

Learn how to use CSS Cascade Layers to organize and prioritize styles in a stylesheet, and how to import third-party libraries into a layer.

Key Takeaways
  1. Define the layers and their order in the stylesheet
  2. Import CSS files into layers using at-import and at-layer
  3. Apply a layer to a third-party library import
  4. Verify the correct application of styles by rearranging layers
  5. Use the at-layer rule to apply a layer to an import
💡 Using CSS Cascade Layers and importing third-party libraries into a layer can help keep styles organized and prevent conflicts.

Related Reads

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