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 can I derive one css style form antoher

I have two styles defined like so in my css file:

.open_in_new {
  font-size: 18px;
  margin-top: 5px;
  color: mat-color($teal, 500);
}

.open_in_new_disabled {
  font-size: 18px;
  margin-top: 5px;
  color: mat-color($grey, 500);
}

So, I could use either of these in my html, e.g.:

<mat-icon class="open_in_new_disabled">open_in_new</mat-icon>

Is there any way to remove the redundancy in my style declarations? I am defining font-size: 18px and margin-top: 5px twice over.

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

Is there a way to declare .open_in_new_disabled as having all of the styles of .open_in_new excepting for a different colour?

>Solution :

Do you mean something like this?

.open_in_new,
.open_in_new_disabled {
  font-size: 18px;
  margin-top: 5px;
}

.open_in_new {
  color: mat-color($teal, 500);
}

.open_in_new_disabled {
  color: mat-color($grey, 500);
}

This (the first CSS ruleset) is called grouping, and you can read more about it here.

The other option is to use attribute selectors. For example:

[class*="open_in_new"] {
  font-size: 18px;
  margin-top: 5px;
}

.open_in_new {
  color: mat-color($teal, 500);
}

.open_in_new_disabled {
  color: mat-color($grey, 500);
}

The attribute selector (class, in this case) uses a partial match based on the class name. It’s saying something along the lines of "match every element with the classname which contains open_in_new".

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