SASS compiler adds new css rules
I built a project and delivered it successfully with codekit in December. Now upon making updates and recompiling my sass code and changing none of my config settings there are a whole bunch of new css rules in my target file that I haven't written. This is leading to a bunch of strange errors all over the site.
For example the output CSS has the following (line 4802)
#scroll:hover:after, .hamburger:hover:before, body.blog #small-sort .close:hover:before {
transform: scaleY(1.1);
}
The input sass has no mention of .hamburger:hover:before anywhere. All the code that pertains to hamburger is bellow (line 720) and has no mention of the hover property at all.
.hamburger{
width: 36px;
height: 28px;
position: fixed;
right: 30px;
top: calc(50vh - 18px);
cursor:pointer;
z-index: 1000;
span{
display: block;
width: 100%;
height: 5px;
margin-bottom: 7px;
position: relative;
background: $color-light-blue;
@include transition(0.3s);
}
&:after{
@include transition(0.3s);
content:'';
position: absolute;
bottom: calc(50% - 3px);
background:$color-light-blue;
width: 100%;
height:5px;
transform:rotate(-45deg) scale(1.1);
z-index: 2;
opacity: 0;
cursor:pointer;
}
&:before{
@extend :after;
transform:rotate(45deg) scale(1.1);
}
&.open{
&:after, &:before{
opacity: 1;
}
span{
opacity: 0;
}
}
@media (max-width: 860px){
top: 35px;
right: 20px;
}
}
Config, sass file and css output file attached. files.zip
You’re using extend(). That is what’s generating this. The latest version of Libsass adopts the latest behavior for extend.
-Bryan
On Apr 22, 2020, at 17:57, studio-blueboat [email protected] wrote:
I built a project and delivered it successfully with codekit in December. Now upon making updates and recompiling my sass code and changing none of my config settings there are a whole bunch of new css rules in my target file that I haven't written. This is leading to a bunch of strange errors all over the site.
For example the output CSS has the following (line 4802)
#scroll:hover:after, .hamburger:hover:before, body.blog #small-sort .close:hover:before { transform: scaleY(1.1); } The input sass has no mention of .hamburger:hover:before anywhere. All the code that pertains to hamburger is bellow (line 720) and has no mention of the hover property at all.
.hamburger{ width: 36px; height: 28px; position: fixed; right: 30px; top: calc(50vh - 18px); cursor:pointer; z-index: 1000; span{ display: block; width: 100%; height: 5px; margin-bottom: 7px; position: relative; background: $color-light-blue; @include transition(0.3s); } &:after{ @include transition(0.3s); content:''; position: absolute; bottom: calc(50% - 3px); background:$color-light-blue; width: 100%; height:5px; transform:rotate(-45deg) scale(1.1); z-index: 2; opacity: 0; cursor:pointer; } &:before{ @extend :after; transform:rotate(45deg) scale(1.1); } &.open{ &:after, &:before{ opacity: 1; } span{ opacity: 0; } } @media (max-width: 860px){ top: 35px; right: 20px; } } Config, sass file and css output file attached. files.zip
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.
@bdkjones thanks so much, I knew I was missing something. I assume in this specific case then all i need to do is change the extend to the following?
@extend .hamburger:after;
I'm not really an expert with Sass extends, but from what I've been told they should be used sparingly. Prefer mixins if you're able to do the same thing with them.
If I may chime in, combined @extends are no longer doable. You need to extend a single selector. The problem is that the extend will be global. @extend :before means you're extending EVERY :before and ::before in your whole stylesheet. Every @extend will create exponentially long selectors. I can tell you from experience that doing that more than a few times will result in an enormous CSS file size or, worse, an infinite loop in the compiler.
There are two solutions.
- Use a placeholder selector (
%my-selector). Placeholder selectors are selectors you can extend but that don't actually appear in the final code. It also allows you to be more specific when naming them, which nearly eliminates the possibility of unwanted. For instance:
%mobile-menu-hamburger-before-after {
// Your common styles
}
.hamburger {
&::before {
@extend %mobile-menu-hamburger-before-after;
// Your specific ::before styles
}
}
- Create a mixin and include it in both places. This will create duplicated code, sure, but gzip (which your Web server probably uses) is very efficient at handling repeated chunks of text that are exactly the same thing, so the impact will be relatively minor.
Thanks @Ariane-B and @bdkjones. I have a much better understanding of the code I was writing now. It seems to me that in general extends are more trouble than they're worth.
That's a good way of summing it up.
Closing because this isn't a CodeKit issue. Thanks!