When utilizing the @extend feature in Sass, it applies all selectors to the styling block

I am encountering unexpected behavior while utilizing sass. I am eager to uncover a solution or rationale behind this issue.

When creating code with a loop and using @extend to incorporate a set of styles, Sass is generating the code with all possible combinations of selectors on every block of styles, instead of only on the intended style blocks. Below is a concise example illustrating this current behavior:

@mixin breakpoint-step($i) {
  $screen-activations: (
    'xs',
    'sm'
  );
  %styling {
    flex: 1 1 calc(#{$i} / 12 - 0.625rem);
  }
  @each $activation in $screen-activations {
    .ss-#{$activation} .span-#{$activation}-#{$i} {
        @extend %styling;
    }
  }
}

@include breakpoint-step(1);
@include breakpoint-step(2);
@include breakpoint-step(3);

This results in:

.ss-xs .span-xs-1, .ss-sm .span-sm-1, .ss-xs .span-xs-2, .ss-sm .span-sm-2, .ss-xs .span-xs-3, .ss-sm .span-sm-3 {
  flex: 1 1 calc(1 / 12 - 0.625rem);
}

.ss-xs .span-xs-1, .ss-sm .span-sm-1, .ss-xs .span-xs-2, .ss-sm .span-sm-2, .ss-xs .span-xs-3, .ss-sm .span-sm-3 {
  flex: 1 1 calc(2 / 12 - 0.625rem);
}

.ss-xs .span-xs-1, .ss-sm .span-sm-1, .ss-xs .span-xs-2, .ss-sm .span-sm-2, .ss-xs .span-xs-3, .ss-sm .span-sm-3 {
  flex: 1 1 calc(3 / 12 - 0.625rem);
}

The anticipated result should be:

.ss-xs .span-xs-1, .ss-sm .span-sm-1 {
  flex: 1 1 calc(1 / 12 - 0.625rem);
}

.ss-xs .span-xs-2, .ss-sm .span-sm-2 {
  flex: 1 1 calc(2 / 12 - 0.625rem);
}

.ss-xs .span-xs-3, .ss-sm .span-sm-3 {
  flex: 1 1 calc(3 / 12 - 0.625rem);
}

For reference, visit the SassMeister gist:

Any insights into why this phenomenon is happening?

Answer №1

While I was able to find a workaround for this issue, the root cause remains elusive.

Check out the solution I devised:

$range: 3;

@for $i from 1 through $range {
    %style-#{$i} {
        flex: 1 1 calc(#{$i} / 12 - 0.625rem);
    }
}

@mixin breakpoint-setting($i) {
  $screen-sizes: (
    'xs',
    'sm'
  );
  @each $size in $screen-sizes {
    .screen-#{$size} .column-#{$size}-#{$i} {
        @extend %style-#{$i}; // custom hack
    }
  }
}

@for $i from 1 through $range {
  @include breakpoint-setting($i);
}

The output is as follows:

.screen-xs .column-xs-1, .screen-sm .column-sm-1 {
  flex: 1 1 calc(1 / 12 - 0.625rem);
}

.screen-xs .column-xs-2, .screen-sm .column-sm-2 {
  flex: 1 1 calc(2 / 12 - 0.625rem);
}

.screen-xs .column-xs-3, .screen-sm .column-sm-3 {
  flex: 1 1 calc(3 / 12 - 0.625rem);
}

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

CSS3 animations can sometimes result in incorrect element sizing due to the animation-fill-mode property

After experimenting with CSS3 animation, I encountered an unusual issue. Adding animation-fill-mode to the "parent" <div> caused the width of the "child" <div> to be less than 100% upon completion of the animation. Can anyone shed light on why ...

What is the reason for background images not loading when using `display: none`?

I'm really struggling to understand this puzzle: When the page loads, will mypic.jpg be downloaded by the browser? #test1 { display: none; } #test2 { background-image: url('http://www.dumpaday.com/wp-content/uploads/2017/01/random-pic ...

Using `justify-content: space-around` on a nested element within a class will not produce the desired spacing effect

This particular piece of code is functioning correctly: aside { /* Customize styles for the aside element */ flex: 0 0 40px; display: flex; flex-direction: column; justify-content: space-around; align-items: center; background-color: #2800da; ...

The upper 50% is malfunctioning because the parent height is set to auto

Having a bit of trouble with vertically centering a child div within its parent. I've been using this mixin: @mixin vertical-align { position: relative; top: 50%; -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); ...

"Utilizing the ng-class directive in AngularJS to apply multiple

I have been attempting to utilize the ng-class directive in AngularJS based on a certain condition: if a JSON value is 1, it should display as green; if it's 2, red; and if it's 0, white. However, I am unable to achieve the desired result for som ...

Minimizing load time to create a seamless user experience

After working diligently on a website, we successfully reduced the total content for a page load from 13.7MiB's to 2.4, but unfortunately, the page still loads at a snail's pace. This Joomla site is loaded with redundant DOM elements (2000+ for ...

Having issues with jQuery Overlay functionality on Internet Explorer 8

Sorry if this question has already been addressed elsewhere, but I wasn't able to locate any information on it. I am currently using jQueryTools Overlay which is functioning properly in Chrome. However, I am encountering an issue in IE-8 where my ove ...

Tips for integrating Sass into a React application with bundle.js and bundle.css

I'm currently working on a React project that uses bundle.js/bundle.css, which are linked in the index.html like this: <script src="/bundle.js"></script> <link rel="stylesheet" href="/bundle.css" /> Ini ...

Maintaining dual sets of files for website design projects

When working on website design and programming projects, I often wonder if it is common practice for experienced developers to maintain two sets of files - one for development and another for production like in jQuery. For instance, I have a project where ...

The jquery and css3 slider smoothly slides in one direction, but struggles to move in the opposite

I have successfully implemented a feature in my code where an item slides to the left and gets deleted. However, I am facing an issue where after deleting the element, a new element is created on the right side of the screen but it appears in the center wi ...

A distinct vertical line separating two buttons

I'm currently working on an Angular 2 app using Angular material. I have two buttons labeled "sign in" and "sign up", and I'm trying to add a vertical line between them. Despite looking at various examples online, I haven't been successful i ...

Ensure the top element remains fixed until the page scrolls (once the content surpasses it)

To ensure that all page content (highlighted in red as "Test" in the image) is placed at the top 100% and requires scrolling to view. The top header (identified in black in the image) should be fixed at the top of the page. When the content reaches the ...

django project: template with missing node_modules in static folder

I have obtained a template example that I would like to use in my Django project. I have organized all of my static files within the static folder and made the necessary modifications in settings.py. However, I am encountering an issue with the following l ...

Developing a dynamic row that automatically scrolls horizontally

What is the best method for creating a div box containing content that automatically slides to the next one, as shown by the arrows in the picture? I know I may need jquery for this, but I'm curious if there's an alternative approach. oi62.tinyp ...

Reorder HTML elements after deleting an element

For my school project, I am developing a website similar to Google Keep that allows users to create and share notes. I have utilized Bootstrap's container, row, and cards to display the existing notes. Everything seemed to be working fine until I enco ...

Adjust the horizontal position of a span element from left to right based on its class

Welcome to my first question here, with many more to come in the future. I have an issue with positioning that I'm not sure is possible to solve. The problem arises with an unordered list (<ul>) containing floated <li> elements. For the m ...

What is the best way to eliminate a vertical scroll on a webpage

Struggling to achieve a responsive layout with a dynamic background image, but still dealing with unwanted scrolling. I've experimented with overflow:hidden and different background properties without success. <div class="wrap"> <div cla ...

CSS - How can text and images be effectively combined for a visually appealing design?

We're tasked with designing text in the following format: Text in English XXX Text in English XXX Text in English. The XXX placeholders need to be replaced with images that have the same height as the English text. Is there a better solution for th ...

Placeholder disappears when text color is changed

I have been struggling for 3 hours trying to change the placeholder color to graytext, but it's just not working. Here is the code I've been using: <div class="form-group"> <label for="email">Email:</label ...

Displaying an iFrame with a height that excludes the header section

My website contains an iframe with a height of 100% and a div positioned above it with a height of 70px. The issue is that the iframe overflows the page, causing the scrollbar to extend beyond the visible area. I am looking for a solution to adjust the ifr ...