SASS inheritance - omiting the base class

2019-09-17 20:56发布

问题:

I can use this syntax for inheriting a class in SASS

Code

.message {
  border: 1px solid #ccc;
  padding: 10px;
  color: #333;
}

.success {
  @extend .message;
  border-color: green;
}

Output

.message, .success, .error, .warning {
  border: 1px solid #cccccc;
  padding: 10px;
  color: #333;
}

.success {
  border-color: green;
}

I want to do something similar, whereby .message is omitted from the output

Desired Output

.success, .error, .warning {
  border: 1px solid #cccccc;
  padding: 10px;
  color: #333;
}

.success {
  border-color: green;
}

Is this possible?

回答1:

Yes, using placeholder selector:

SASS

%message {
  border: 1px solid #ccc;
  padding: 10px;
  color: #333;
}

.success {
  @extend %message;
  border-color: green;
}

.error{
  @extend %message;
  border-color: red;
}

OUTPUT

.success, .error {
  border: 1px solid #ccc;
  padding: 10px;
  color: #333;
}

.success {
  border-color: green;
}

.error {
  border-color: red;
}

The problem is if you want message like a class, then you have to extend too:

.message{
  @extend %message;
}