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.

Parent Selectors should have targets

See original GitHub issue

When creating a Parent Selector rule, everything before the & gets prepended to the outermost scope.

The feature could be greatly improved by allowing for “parent targeting”, where you could apply the parent selector to a specific parent (in the case where you have deeply nested selectors).

The syntax for this feature is probably the biggest hindrance, so I would love to start a discussion on the possibilities.

Issue Analytics

  • State:open
  • Created 11 years ago
  • Reactions:6
  • Comments:118 (70 by maintainers)

github_iconTop GitHub Comments

3reactions
matthew-deancommented, Feb 1, 2013

I’m going to re-open this. We’ve seen variations on this idea (such as #1154), and I feel like there’s a possibility of a solution.

That is, there are times when people have a logical stack of elements, but don’t necessarily want to inherit the entire stack in their output.

Also, there are times when people want to inherit SOME of the stack, but not all of it.

I think we can all agree that we don’t want messy syntax, but I’d like to see a variety of ideas. As we’ve explored here, targeting by name seems problematic. But we could also target by level:

.main-content {
    .button {
        .button-icon {
            background-image: @button-icon;
            &{2}:hover { background-image: @button-icon-hover; }
            &{2}.focus { background-image: @button-icon-focus; }
            .ie8 &{3} { background-image: @button-icon-ie8; }  // In this case, &{3} is the same as & because it goes to top
        }
    }
}

Basically, step “up” the inheritance tree to the level we want to inherit / append to the current selectors.

Or, something like, maybe “breaking” the inheritance and starting over, without having to move the class outside of the block. I dunno, like:

.grandparent {
  .parent {
    /.child {   // Ordered logically, but outputs cleaner CSS
      background: white;
    }
  }
  background: blue;
}

or, borrowing from the top example:

.grandparent {
  .parent {
    &{0}.child {   // inherit NONE of the parents
      background: white;
    }
  }
  background: blue;
}

Both outputting:

.grandparent {
  background: blue;
}
.child {
  background: white;
}

Thoughts?

2reactions
matthew-deancommented, Jan 5, 2017

Since this issue is still open…

So far, trying to “select and insert” from parent selectors ends of being terribly complex. The “where” becomes quickly ambiguous.

I think the only workable solution if this feature were to happen would need to be something similar to “capturing” and “replacing” that’s used with regular expressions, where you define insertion points (in this example marked with ?) that you use to manipulate output later.

.grandparent? {
  .parent? {
    .child {
      background: blue;
      &(1, :hover) {     // all selectors, but replace 1st insertion point with ":hover"
        background: red;
      }
      &(2, .active) {     // all selectors, but replace 2nd insertion point with ".active"
        background: white;
      }
    }
  }
}

resulting in:

.grandparent .parent .child {
  background: blue;
}
.grandparent:hover .parent .child {
  background: red;
}
.grandparent .parent.active .child {
  background: white;
}

I’m not suggesting at all that that’s the right syntax, but any kind of “targeting” that’s been tried in this thread, either by index, or “scope level”, or named selector, just isn’t going to work without a precise target.

With insertion points, they have a left-to-right, top-to-bottom indexing order that is unambiguous. You can see where your thing will go, and can specify exactly what that thing is.

So any workable solution I believe will need to be some variation of this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A Use Case for a Parent Selector
I've occasionally used Javascript to add my own HTML just to have some targets to style where a CSS parent selector would have...
Read more >
Is there a CSS parent selector?
Parent selection is done via the pseudo-class :has() . For example, div:has(> .child) will select all <div> elements with a child having a...
Read more >
Meet :has, A Native CSS Parent Selector (And More)
Like with most pseudo-classes, selectors can be chained to target child elements of a target element or adjacent element. /* Select image ...
Read more >
Understanding the CSS parent selector :has()
The easiest way to describe :has() is as a parent selector. If we wish to target the parent element based on the content...
Read more >
CSS :has(.parent-selectors)
Parent selectors select parent elements, right? They actually select grandparents and any matching ancestors as well. I haven't been the only ...
Read more >

github_iconTop Related Medium Post

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