0

(I think I should mention this, I've only recently started using Sass/SCSS)

http://jsfiddle.net/DriftingSteps/t6kLncfm/

You can see how <strong> is inheriting the properties of the global <a> as well as the properties from nested <a> tag.

a {
    color: #09f;
    text-decoration: none;
    &:hover {
        text-decoration: underline;
        opacity: 0.6;
    }
}

ul {
    font-size: 0.85em;
    a {
        font-family: Georgia, Times, serif;
        font-style: italic;
        color: #0a3;
    }
    strong {
        @extend a;
    }
}

I have been going through http://sass-lang.com/ and I know I'm missing something. What am I doing wrong? Is there a way to inherit properties from the nested <a> only, without the use of classes on either ul a and ul strong? Or is there a better way to do this?

1
  • 1
    I answered on similair question couple of days ago, it should be useful to you. Commented May 15, 2015 at 18:55

2 Answers 2

3

You could use an extend-only selector (%), and extend both ul a and ul strong from that:

a {
    color: #09f;
    text-decoration: none;
    &:hover {
        text-decoration: underline;
        opacity: 0.6;
    }
}

ul {
    font-size: 0.85em;
    a {
      @extend %a;
    }
    strong {
        @extend %a;
    }
}

%a {
    font-family: Georgia, Times, serif;
    font-style: italic;
    color: #0a3;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Good idea to use the extend-only selector here, even if I'm unsure OP really needs @extend.
Yes, I see why @extend isn't necessary in this particular condition, but I believe I should be using the extend-only selector. Thanks to both of you :)
2

You don't have to use that class and you don't have to apply it to your HTML, you can just define it and refer to it when inheriting:

a, .a {
    font-family: Georgia, Times, serif;
    font-style: italic;
    color: #0a3;
}
strong {
    @extend .a;
}

Demonstration

And of course, in this case you don't really need extend:

a, strong {
    font-family: Georgia, Times, serif;
    font-style: italic;
    color: #0a3;
}
strong {
    // other stuff
}

It seems to me that the real use case of extend isn't deep localized selectors defined together, but rather the extension of a selector defined elsewhere.

2 Comments

I see why extend isn't a better option in this case, but what about cases in which a and strong have conflicting properties (like different background colors)? Use the extend-only selector? Thank you for your time.
@DriftingSteps In that case you can just define three rules: one of a, one for strong, and one with the common properties. In the case of local selectors, I find this clearer than using @extend which means you declare then override.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.