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.

Adding a tag selector with & generates bad CSS

See original GitHub issue

Looks like the parser just does a blind concatenation for nested selectors. Sometimes it should be more intelligent. For example:

.foo {
    .bar {
        &select {
            // stuff
        }
    }
}

generates:

.foo .barselect {
    // stuff
}

expected:

.foo select.bar {
    // stuff
}

My current workaround:

.foo {
    .bar {
    }
    select {
        @extend .bar;
    }
}

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
nex3commented, Sep 11, 2018

@SilentSpammer Support for selector& is being tracked in https://github.com/sass/sass/issues/1425. That said, it still won’t satisfy your use-case, because & will always inject the full parent selector, so you’ll get select.foo .bar. Here’s how I’d handle that:

@mixin unify-parent($selector) {
  @at-root #{selector-unify(&, $selector)} {
    @content;
  }
}

.foo .bar {
  @include unify-parent("select") {
    // stuff
  }
}

The selector-unify() function returns a selector that matches only elements matched by both its arguments, which looks like what you’re trying to express here.

1reaction
jzilacommented, Nov 12, 2014

Fair enough. Feel free to close the bug. At least now there’s a search result on GitHub that can help people who experience this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is element-specific CSS selector bad? - html - Stack Overflow
CSS selectors are read right to left. So p.error has one additional step to .error . This may result in a smaller set...
Read more >
You Need to Stop Targeting Tags in CSS | frontstuff
You're using the existing DOM structure, you're unleashing the power of CSS operators, you're not adding unnecessary classes, and it works. But ...
Read more >
Specificity - CSS: Cascading Style Sheets - MDN Web Docs
It enables making CSS selectors very specific in what element is targeted without any increase to specificity. In creating third-party CSS to be ......
Read more >
Advanced CSS Selectors you never knew about - Medium
You can do it for any element (div, span, img, etc…), and any common attribute (id, name, value, etc…).
Read more >
Creating actions using CSS selectors - PostHog
Under the hood the toolbar generates a CSS selector matching the UI element. The toolbar is quite clever, but sometimes automatically generated selectors...
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