question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@extend with pseudo-class has unexpected priority

See original GitHub issue

Consider the following scss

.foo:hover{
    color:red;
}

.foo.bar.baz{
    color:blue;
}

p.extended{
    @extend .foo;
    @extend .bar;
    @extend .baz;
}

I would like p.extended elements to always have the blue color with no red :hover state, since I am extending .foo .bar and .baz which have a higher priority than just .foo:hover

However in the resulting css

.foo:hover, p.extended:hover {
  color: red;
}

.foo.bar.baz, p.extended {
  color: blue;
}

This means that p.extended elements will still have the red :hover state, but elements with class="foo bar baz" will not.

I’m not sure if there is a way of fixing without getting really hacky this but suggestions welcome.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
davidkpianocommented, Apr 30, 2015

The @extend directive is working as expected here. Here’s the breakdown:

  • @extend .foo;
    • when extending selector has a pseudoclass of :hover, set color: blue (from .foo:hover)
    • when extending selector has a class of .bar and .baz, set color: red
  • @extend .bar;
    • when extending selector has a class of .foo or matches p.extended (since that extended .foo), and has a class of .baz, set color: blue
  • @extend .baz
    • when extending selector has a class of .foo or matches p.extended, and has a class of .bar or matches p.extended (since that extended .bar), set color: blue
  • remove redundant p.extended.foo.bar, p.extended.foo.baz, p.extended.bar.baz selectors since p.extended extends .foo, .bar, .baz

You’re doing some complicated extending here. Try extending only %placeholder selectors:

%foo-hover {
  color: blue;
}

%foo-bar-baz {
  color: red;
}

.foo:hover {
  @extend %foo-hover;
}

.foo.bar.baz {
  @extend %foo-bar-baz;
}

p.extended {
  @extend %foo-bar-baz;
}

Or something similar. I do not know what you’re intending to do, or how you’re using this in real life, but hopefully the above example will help guide you.

1reaction
mikemonteithcommented, Apr 30, 2015

Thanks David. My ‘real life’ example was trying to extend selectors from a third-party CSS that I’m using as if it was SCSS. I don’t want to have to change it by adding %placeholder selectors. I have another work-around that I can use.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sass extend with pseudo selectors - Stack Overflow
In fonts I have this rule which I would like to reuse @extend. //fonts.scss .icon-ab-logo, { font-family: 'icomoon'; speak: none; font-style: ...
Read more >
Sass Style Guide | CSS-Tricks
Use Your Regular CSS Formatting Rules / Style Guide · List @extend(s) First · List @include(s) Next · List “Regular” Styles Next ·...
Read more >
How to create your own ad filters - AdGuard Knowledgebase
AdGuard extends CSS and lets filters developers handle much more complicated cases. However, to use these extended rules, you need to be fluent...
Read more >
Configuring Variants - Tailwind CSS
Targets the disabled pseudo-class. For more information about how variants work, read our documentation on responsive variants, dark mode variants, and hover, ...
Read more >
Styling - Vanilla Extract
All the styling APIs in vanilla-extract take a style object as input. ... CSS properties can be set just like when writing a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found