Unusual SASS results observed while utilizing extends functionality

Having issues with my SASS files:

_android.scss:

$width: 111px;
@import 'child';

.android .child {
    @extend %child;
}

_ios.scss:

$width: 999px;
@import 'child';

.ios .child {
    @extend %child;
}

_child.scss:

%child {
    width: $width;
}

app.scss:

@import 'ios';
@import 'android';

After running sass, I get:

$> sass app.scss
.ios .child, .android .child {
   width: 999px; }

.ios .child, .android .child {
   width: 111px; }

This is not what I expected. I was hoping for:

.ios .child {
    width: 999px;
}

.android .child {
    width: 111px;
}

What am I doing wrong here?

Answer №1

The reason for this behavior is that @extends are processed in the order they are encountered in the Sass file, grouping all selectors where they are used together at that point (@extends tends to be a bit greedy), and if you include the @extends placeholder more than once, it will result in duplicate outputs.

Let's walk through the process:

  1. Firstly, ios.sass is loaded, defining $width and including %child.
  2. Sass encounters the placeholder due to the inclusion, collects all instances of the placeholder within the code, groups them with their respective selectors, and applies the current value of width.
  3. Then, android.sass is loaded, resetting $width to a new value and including %child again.
  4. Sass repeats the grouping process each time an @extends is encountered, resulting in duplicate groupings with different $width values.

This explains why you see both selectors grouped and repeated twice with varying $width values in the output.

Solution:

To resolve this issue, using a mixin would be ideal. When a mixin is called, it is evaluated immediately, and the resulting styles are directly injected into the selector.

@mixin custom-width($width-in: 1000px) {
    width: $width-in;
}

.ios .child {
    @include custom-width(111px);
}

.android .child {
    @include custom-width(999px);
}

Now, considering the mixin example:

  1. The mixin is defined but not applied until called like a function.
  2. ios.sass loads and defines a selector.
  3. Sass interprets the mixin call with a parameter, executes the mixin, and inserts the calculated width value into the selector.
  4. android.sass loads...
  5. Sass processes another mixin call by injecting a new width value into the corresponding selector.

Note that I have set a default value of 1000px in my mixin definition, which will be used if @include custom-width(); is utilized without a specific parameter.

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

What causes my animation to “reset” upon adding a new element using innerHTML?

One of the functionalities on my webpage involves a script that inserts a div called "doge" using innerHTML when a button is clicked. Additionally, there is a CSS keyframes animation applied to another div on the same page. However, whenever the button is ...

Is there a way to ensure a Bootstrap grid column takes up the entire remaining row space?

I have a dynamic set of elements that need to be inserted into a page, with some elements being labeled as "small" and others as "big". The challenge is that I do not know in advance the quantity or order of these elements. If an element is labeled "small ...

If the URL hash matches the ID of a specific div, then take action on the div's child element

Imagine I have the following HTML structure: <div class=".blog-item-holder"> <div id="AAAA"><div class="inner">AAAA</div></div> <div id="BBBB"><div class="inner">BBBB</div></div> <div id="CCCC">& ...

Encountered an issue with Angular while trying to import scss variables: Module parse failed due to an unexpected token at

Our project previously utilized a palette for importing styles, which functioned correctly in Angular 13. However, upon upgrading to Angular 14, the palette no longer works as expected. Below are the specific details of the issue: Error: Module parse faile ...

Formatting text over an image

The challenge I am facing involves having an image and text on it in two separate divs with a background color. The length of the text in one div is long while the text in the second div is short, but I want to ensure that the width of both divs are equal. ...

Is there a way to target a child element for hover effect in CSS without affecting the parent element?

Is it feasible to hover over a nested child element without activating a hover effect on the parent element? In the demonstration below, you'll notice that even when hovering over the child, the hover effect on the parent is still in place. I am int ...

What is the best way to align an image in the middle of an image slider within a Bootstrap

Struggling to align the image properly in a Bootstrap 5 image slider carousel: https://i.sstatic.net/upPza.png <div id="carouselExampleIndicators" class="carousel slide" data-bs-ride="carousel"> <div class=&qu ...

Guide to setting up a node.js server that serves HTML files with CSS styling

I am currently working on setting up a simple node.js server for my HTML using http and express. Everything is functioning properly, but the CSS is not displaying as expected. Here is the code snippet for my server setup: const express = require("express ...

Modify the color of the matSnackbar

I'm having trouble changing the snackbar color in Angular using Angular Material. I tried using panelClass in the ts file and adding it to the global css, but the color remains unchanged. Any suggestions on how to resolve this? I am still new to this ...

Ways to conceal a label and the input field when a radio input is switched on

In my HTML file, I have coded an application form using CSS to style it. Some of the tags have been removed for display purposes. <label>Member</label> <input type="radio" name="Answer" value="Yes"/>Yes<br/> <input type="radio" ...

Adjust div height to match the dynamic height of an image using jQuery

I'm facing an issue with my image setup. It has a width set to 100% and a min-width of 1024px, but I can't seem to get the 'shadow' div to stay on top of it as the window size changes. The height of the shadow div also needs to match th ...

Retrieving Blocked Images with Selenium: A Step-by-Step Guide

HTML: <html> <head> <body onload="document.getElementById('a').style.display='block';"> <div id="a" align="center" onclick="document.location.reload();" style="display: block; cursor: pointer;"> <img width="9 ...

The HTML dropdown menu is malfunctioning

I've been struggling to create a website with a dropdown menu. Despite following various guides and searching for solutions, the menu is behaving strangely. I've tried numerous tactics, so some lines of code may be unnecessary. The submenu is ap ...

Can CSS be used for creating unique color combinations?

I am facing a challenge where I have two div elements with different transparent, colored backgrounds that overlap each other. My goal is to find a way to customize the color in the area where these elements overlap. For instance, if I blend red and blue ...

The styles from the Next.js module are being overridden by the styles from mui/material

Currently, I am working with next.js and using scss modules for styling. However, I have encountered an interesting issue while using mui/material components. During development (npm run dev), everything functions as expected. The problem arises when I swi ...

When the cursor hovers, a drop-down menu appears simultaneously

Currently utilizing the Bigcommerce stencil theme, I have encountered an issue where both dropdown menus appear when I hover over a link. My goal is to have only one dropdown menu pop up when hovering over a specific link. https://i.sstatic.net/paVoY.png ...

The use of Angular's ngClass directive does not work within the link function

I have a straightforward directive that renders an element, and this is the template: <div class="nav-item"></div> The .nav-item class looks like this: .nav-item { height: 50; } Here's the directive in action: angular.module('m ...

Is there a way to retrieve all CSS styles associated with a specific element?

When I come across a site element that I really like and want to incorporate into my own site, what is a simple way to do so? It can be challenging to navigate through numerous CSS files to find all the necessary styles. ...

HTML table with scroll feature displays gaps between consecutive rows

I've created a scrolling table with some header line spacing showing up when scrolled. Do you know how to hide it? Check out this Codepen example Here's the HTML structure: <div class="table-container"> <table class=" ...

Adjusting grids in Bootstrap according to device orientation

I have a vision for a webpage layout that will feature two columns in landscape mode and switch to one column in portrait mode. Here is an outline of what I have in mind: Landscape: <body> <div class="container-fluid"> <div cl ...