微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CSS – LESS类继承

我正在使用 twitter bootstrap的样式,并且有关于继承的问题.

我想制作两个可以从引导继承标签样式的标签类.

.label-new {
    .label; .label-important;
}
.label-updated {
    .label; .label-warning;
}

然而,以上将导致LESS分析错误“NameError:.label-important is undefined”,因为.label-important和.label-warning是在引导中使用以下命令定义的:

.label,.badge {
  // Important (red)
  &-important         { background-color: @errorText; }
  &-important[href]   { background-color: darken(@errorText,10%); }
  // Warnings (orange)
  &-warning           { background-color: @orange; }
  &-warning[href]     { background-color: darken(@orange,10%); }
}

有什么特殊的语法来继承.label-important / warning吗?

提前致谢.

解决方法

我认为你只能继承.label,它应该给.label-new-important和.label-new-warning(和等同的更新).如果这不是需要的,并且您想要新添加的扩展名,那么您可能需要直接为.label定义颜色,以隔离和定义所需的内容.像这样:
.label { 
  // New (red) 
  &-new           { background-color: @errorText; } 
  &-new[href]     { background-color: darken(@errorText,10%); } 
  // Updated (orange) 
  &-updated       { background-color: @orange; } 
  &-updated[href] { background-color: darken(@orange,10%); } 
}

编辑(如果您不想重新定义颜色,但使用mixin)

为了避免重新定义颜色,那么您需要更改原始定义,并执行以下操作:

首先,删除原来的.label和.badge定义,然后再更明确地定义它们,如下所示:

.label-important,.badge-important {
  // Important (red)
  { background-color: @errorText; }
  { background-color: darken(@errorText,10%); }
}

.label-warning,.badge-warning {
  // Warnings (orange)
  { background-color: @orange; }
  { background-color: darken(@orange,10%); }
}

.label-new {
    .label-important;
}

.label-updated {
    .label-warning;
}

原文地址:https://www.jb51.cc/css/216612.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。