CSS Variables Scope

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 108:- CSS Variables Scope

CSS variables, also known as custom properties, have a specific scope where they can be accessed and used. There are two types of scopes: local and global.

  1. Local scope: CSS variables defined inside a selector have a local scope and can only be used within that selector and its descendants. For example:

cssCopy code

div {  --main-color: blue; }

In this example, --main-color is defined inside the div selector, so it can only be used within that selector and any descendant selectors.

  1. Global scope: CSS variables defined outside of any selector have a global scope and can be used anywhere in the stylesheet. For example:

cssCopy code

:root {  --main-color: blue; }

In this example, --main-color is defined outside of any selector, so it can be used anywhere in the stylesheet.

It's important to note that when a CSS variable is used, the browser looks for the variable in the local scope first. If it's not found, the browser will then look for the variable in the global scope.

cssCopy code

div {  --main-color: blue; } p {  color: var(--main-color); }

In this example, --main-color is defined in the local scope of the div selector, but it's used in the p selector. Because --main-color is not defined in the local scope of the p selector, the browser will look for it in the global scope and use the value of blue.

Understanding the scope of CSS variables is important for creating modular and maintainable code, and for avoiding naming conflicts between variables used in different parts of the stylesheet.

13. Major Project 1

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

I am not able to access videos from second class and further. I have already completed first class

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?