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

相关文章

Css3如何实现鼠标移上变长特效?(图文+视频)
css3怎么实现鼠标悬停图片时缓慢变大效果?(图文+视频)
jquery如何实现点击网页回到顶部效果?(图文+视频)
css3边框阴影效果怎么做?(图文+视频)
css怎么实现圆角边框和圆形效果?(图文+视频教程)
Css3如何实现旋转移动动画特效