Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can i use css attribute selector within a SASS class to style itself

I am trying to style a class using css attribute selectors to apply to the class itself. My use case is to wrap them in media selectors to style a class when a breakpoint applies but I want to keep the example simple and work from first principles.

I have a sandbox for the following code here

Here’s my logic… In SCSS we can style a class itself using &

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i.e.

.myclass {
 & {
  border: ...
 }
}

and we can refine that to specific class combinations

.myclass {
 &.otherclass {
  border: ...
 }
}

we can also style classes using attribute selectors

  div[class*="md"] {
    border: 10px solid orange;
  }

Can we combine these to use them within a sass class to say "style classes according to an attribute selector within the class scope only to combine the & and div[class*="value"] specifiers?

i.e. for

      <div className="test1">
        <div className="xs md">I am styled using div[class*="md"] selector</div>
      </div>
      <div className="test2 xs md">
        I am not styled by my selector. Can I style myself using attribute
        selectors?
      </div>

with style

.test1 {
  & {
    background: lightblue;
  }

  div[class*="md"] {
    border: 10px solid blue;
  }
}

.test2 {
  & {
    background: lightgreen;
  }

  div[class*="md"] {
    border: 10px solid green;
  }
}

can I style .test2.md from within the class similar to using &?

thanks

>Solution :

Do you want to do this

.test2 {
  & {
    background: lightgreen;
  }

  &[class*="md"] {
    border: 10px solid green;
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading