7

Is it possible to turn this:

.redstripe p:not(last-child) {
  border-bottom:1px solid red;
}

Into a mixin so that I can apply it to any element and assign a child tag to it like:

@mixin redstripe (this.$children):not(last-child) {
  border-bottom:1px solid red;
}

And then apply:

div {
  @include redstripe(p);
}

What is the correct way to implement this?

1
  • I have been looking but cannot find anything that shows how to pass a tag as a parametre.
    – HGB
    Jun 6, 2015 at 20:22

1 Answer 1

18

Here's a general purpose mixin like you've described.

DEMO

@mixin not-last-child($selector) {
  & #{$selector}:not(:last-child) {
    @content;
  }
}

We can pass it a selector string to use.

SCSS:

.thing {
  @include not-last-child('p') {
    color: red;
  }
}

CSS:

.thing p:not(:last-child) {
  color: red;
}

Sass Documentation

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.