DEV Community

Paul
Paul

Posted on

2

Weird @extend side effects

I have this project that's using Bootstrap. Instead of importing the CSS and expanding the styles with my own, I'm importing the Sass styles and compiling to a single CSS file. It was upon doing so that I noticed something strange.

Bootstrap defines a button class like so (code snipped for brevity, because my focus is on the selectors):

.btn {
  // styles
}
Enter fullscreen mode Exit fullscreen mode

Some of my own styles were using the Sass directive @extend for a form input button:

.search-form .search-submit {
  @extend .btn;
  @extend .btn-secondary;
}
Enter fullscreen mode Exit fullscreen mode

Elsewhere, I had some link-specific styles that I wanted to make sure didn't get applied to the .btn class:

a:not(.btn) {
  // styles
}
Enter fullscreen mode Exit fullscreen mode

Once I compiled to CSS, however, I wasn't seeing those link-specific styles being applied. Instead, this is what was output to my stylesheet:

a:not(.btn):not(.search-form .search-submit) {
  /* styles */
}
Enter fullscreen mode Exit fullscreen mode

This was due to my use of @extend in on the submit button. So I copied Bootstrap's button class definitions into a mixin in my own stylesheet, and changed the input button's styles to this:

.search-form .search-submit {
  @include button;
  @include button-variant($secondary, $secondary);
}
Enter fullscreen mode Exit fullscreen mode

Once I made that change, the output to my stylesheet was as I expected:

a:not(.btn) {
  /* styles */
}
Enter fullscreen mode Exit fullscreen mode

Note: the button mixin is mine. The button-variant mixin already exists within Bootstrap's code.


This was originally published on eichefam.net.

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay