Exploring the concept of scoping variables in SASS

Imagine wanting to set a universal value for a variable, but also be able to override it for specific selectors. The SASS documentation claims this is achievable.

The documentation states that variables are only accessible within the level of nested selectors where they are defined. If defined outside any nested selectors, they can be accessed everywhere.

Therefore, one would assume that values like "green" and "red" assigned to $text-color would be limited to their respective nested selectors, with $text-color defaulting to "blue" when used elsewhere, such as within .foo.

$text-color: blue;

.green {
    $text-color: green;
    color: $text-color;
}

.red {
    $text-color: red;
    color: $text-color;
}

.foo {
    color: $text-color;
}

However, the SASS compilation results in:

.green {
    color: green;
}

.red {
    color: red;
}

.foo {
    color: red;
}

If anyone can provide some insight into this issue, Iā€™d greatly appreciate it.

Answer ā„–1

Although this question has been asked before, it is important to note that starting from version 3.4.0, variables are now block-scoped unless explicitly marked with the !global flag.

The changelog highlights the following:

All variable assignments within a document will now be local by default. If a global variable shares the same name, it will not be overwritten unless the !global flag is used. For instance, $var: value !global will assign globally to $var.
This change in behavior can be identified using
feature-exists(global-variable-shadowing).

Thus, in the case of the following scss code snippet:

$color: red;

.sel {
  $color: green;
  color: $color;
}

.sel2 {
  color: $color;
}    

The resulting css output will be as follows:

.sel {
  color: green;
}

.sel2 {
  color: red;
}

Answer ā„–2

The reason for this behavior is that when a variable is initially declared as global, all subsequent assignments to that variable also become global. To change it back to a local scope later on, you can use the following approach: $local-text-color: $text-color; followed by color: $local-text-color;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for making a dynamic active button using javascript

I'm trying to create a toggle button with eight buttons. When seven out of the toggle buttons are clicked, it should toggle between two classes and change its CSS styles. If the daily button is clicked, it should toggle the styles of the other seven b ...

I am looking to superimpose one rectangle over another rectangle

I am looking to create something similar using CSS and TypeScript/JavaScript: Could someone please guide me on how to achieve this? My attempt with a flex container looks like this: I am new to front-end development. Can anyone point out what I might be ...

How does Angular5 perform with Bootstrap4?

Currently working on an Angular5 application and considering integrating bootstrap into it. I imported Bootstrap4, which includes dependencies on JQuery and Popper.js. Another option I came across is utilizing CSS grid for more versatility compared to boo ...

Is there a way to set the <hr /> tag to display vertically?

Is there a way to make the <hr /> tag go in a vertical direction rather than appearing horizontally as it normally does? I am looking for a solution that can be used on a mobile site with broader compatibility across browsers, rather than relying on ...

Dynamic CSS sizing

Currently, I am in the process of creating a component and my goal is to achieve a specific layout without the use of JavaScript, preferably utilizing flexbox. Essentially, envision a messenger chat screen with a header and footer containing controls. htt ...

I am seeking advice on how to incorporate JavaScript to adjust slider values and add numerical values or prices. Any suggestions would

Seeking assistance with a unique project: I am currently developing a calculator that allows users to utilize sliders to select the best option for themselves across various categories. My experience with JavaScript is limited, so I decided to reach out h ...

Eliminating the 'white-space' surrounding concealed images

I am currently working on a project where I have a list of images that need to be hidden or shown based on the click event of specific <li> elements. While I have managed to achieve this functionality successfully, I am facing an issue with white spa ...

The image appears to be overlapping within the navigation bar

I'm having trouble understanding why the image is overlapping on this website. The image is enclosed in a link tag, and upon inspecting it, the link tag seems to be correctly positioned while the image appears slightly below. I can't seem to ide ...

Troubleshooting CSS loading issue on iPhone 5 Safari with Responsive Design

I am trying to figure out why my website (Link) is not displaying correctly with responsive design on iPhone5 Safari Browser, even though it works fine on desktop browsers like IE, Chrome, and Safari. It also displays correctly on HTC and Samsung Galaxy de ...

CodePen displaying CSS errorsI'm having trouble getting my CSS

I'm facing an issue with my CSS code and I need your help to figure out what's wrong. I believe it's a simple problem that someone can quickly identify just by looking at the code. Any assistance would be greatly appreciated! Here is the co ...

Enhance the grid system layout by inserting spaces between columns while maintaining the overall structure

I need to adjust the spacing between columns without disrupting the grid system layout. Adding margin to the cards results in only 3 cards per row instead of 4. Below is the code snippet that I'm struggling with. The challenge lies in controlling the ...

The carousel in Bootstrap v4 seems to vanish after the initial slide :/

(Please note that I am a student and still learning, so gentlemen, please don't be upset with the question. Thank you.) In short: I have integrated the Bootstrap v4 carousel (https://getbootstrap.com/docs/4.0/components/carousel/) into the header of ...

Flip the parent element's background image using CSS

I have encountered a challenging issue that I am hoping to find a solution for. Let's consider the following HTML structure: <div id="page"> <div id="menubar"></div> </div> Along with the CSS styling applied to it: #page ...

Does CSS give preference to max width specifications?

Within a div, I have two child div elements. The parent container utilizes a flexbox layout, with preset maximum widths for the children as shown below: <div class="wrap"> <div class="one">Hi</div> <div class="two">my secon ...

Div adjusts its height to match another div's, regardless of the latter's size

I'm currently working on a website where I have a list of news displayed in divs. Each div contains two inner divs: an image on the left and the title, date, and the first 30 words of the news on the right. I am looking to make the image div adjust it ...

Discovering and exchanging two div elements using jQuery

Currently tackling a responsive website design. When the screen is resized to mobile view, I must reorganize some div elements. Currently, here's how I'm handling it: $(window).resize(function() { var width = $(window).width(); if( width < ...

What is a simple way to ensure that my image retains its square shape on a responsive design without having to use numerous media queries?

I've been searching for solutions to my issue, but none of them have worked for me so far. I am working on a form where users can edit or add images, and I want each image to be displayed as a square regardless of the device being used (desktop, mobil ...

Having images that are too large in size and using a high percentage can

I'm encountering a strange issue. When I define the width and height of my images in pixels in my CSS file, all my jQuery animations and events work perfectly fine. However, when I switch to using percentages or 'auto' for the image dimensio ...

Apply a tint to the specified section

I'm looking to add a semi-transparent color overlay to my HTML section, but I'm facing an issue where it doesn't cover the entire section. Here's what I have so far: #main { padding-top: 50px; padding-bottom: 50px; backgr ...

When attempting to view an .html file, SVG images may not appear on

I've encountered a common issue that I can't seem to solve. After creating a website using HTML, CSS, and JS, I uploaded it to the hosting server via FileZilla. Everything is working perfectly on the live site. However, when I downloaded the fi ...