SASS inheritance - omiting the base class

青春壹個敷衍的年華 提交于 2019-12-25 07:12:59

问题


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;
}


来源:https://stackoverflow.com/questions/38520491/sass-inheritance-omiting-the-base-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!