Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - LESS class inheritance

Tags:

css

less

I'm using the styles from twitter bootstrap and have a question about inheritance.

I'd like to make two label classes that'd inherit the label styles from bootstrap.

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

However, the above would result in LESS parse error "NameError: .label-important is undefined" because .label-important and .label-warning are defined using the following in bootstrap:

.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%); }
}

Is there any special syntax to inherit the .label-important/warning?

Thanks in advance.

like image 527
Cat Avatar asked May 08 '12 19:05

Cat


People also ask

Can CSS classes inherit?

Unfortunately, CSS does not provide 'inheritance' in the way that programming languages like C++, C# or Java do. You can't declare a CSS class an then extend it with another CSS class.

Which CSS properties are not inherited?

CSS properties such as height , width , border , margin , padding , etc. are not inherited.

How do I inherit a class property in CSS?

The inherit keyword specifies that a property should inherit its value from its parent element. The inherit keyword can be used for any CSS property, and on any HTML element.


1 Answers

I think you can only inherit the .label which should give .label-new-important and .label-new-warning (and equivalent for updated). If that is not desireable, and you want new added extensions, you probably need to define the colors again directly for the .label to isolate and define what you want. Like so:

.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%); } 
} 

EDIT (If you don't want to redefine the colors but use a mixin)

To avoid a redefining of the colors, then you need to change the original definition, and do this:

First, delete the original .label and .badge definitions, and then second, define them more explicitly like so:

.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;
}
like image 147
ScottS Avatar answered Sep 20 '22 10:09

ScottS