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

How to make h1, h2, h3 versions for a class with scss?

I want to define a class and write different styles for its h1, h2, h3 versions:

.title {
  color: red;
    
  &h1 {
    font-size: 18px;
  }
    
  &h2 {
    font-size: 16px;
  }
}

I expect the CSS output like this:

.title { 
  color: red; 
}
h1.title { 
  font-size: 18px; 
}
h2.title { 
  font-size: 16px; 
}

This is the CSS output I actually get:

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

.title {
  color: red;
}
.titleh1 {
  font-size: 18px;
}
.titleh2 {
  font-size: 16px;
}

Those headlines should also contain the class .title.

>Solution :

Using this QA as a reference, you’ll need to use @at-root and a placeholder:

.title {
  color: red;

    @at-root {
        h1#{&} {
            font-size: 18px;
        }
        
        h2#{&} {
            font-size: 16px;
        }
    }
}

Gives me this output:

.title {
  color: red;
}
h1.title {
  font-size: 18px;
}

h2.title {
  font-size: 16px;
}
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