Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LESS - Can you chain multiple selectors together to save writing multiple classes?

Tags:

less

Instead of writing this:

.class-one .class-two h2, .class-one .class-two h3, .class-one .class-two h4,
.different-class-one .different-class-two h2, .different-class-one .different-class-two h3, .different-class-one .different-class-two h4
.another-class-one .another-class-two h2, .another-class-one .another-class-two h3, .another-class-one .another-class-two h4
{
  background-color: @orange;
}

Is there a way to define a set of classes as the main selector, then change the final selector?

Something like:

.class-one .class-two h2 / h3 / h4,
.different-class-one .different-class-two h2 / h3 / h4,
.another-class-one .another-class-two h2 / h3 / h4
{
  background-color: @orange;
}
like image 568
CMSCSS Avatar asked Jun 03 '13 08:06

CMSCSS


1 Answers

Use a mixin:

.header-template() {
    h2, h3, h4 {
        background-color: @orange;
    }
}

.class-one .class-two,
.different-class-one .different-class-two,
.another-class-one .another-class-two {
    .header-template();
}

(after declaring the @orange variable, the previous compiles to)

.class-one .class-two h2,
.different-class-one .different-class-two h2,
.another-class-one .another-class-two h2,
.class-one .class-two h3,
.different-class-one .different-class-two h3,
.another-class-one .another-class-two h3,
.class-one .class-two h4,
.different-class-one .different-class-two h4,
.another-class-one .another-class-two h4 {
  background-color: #ffa500;
}
like image 98
Rob W Avatar answered Sep 22 '22 22:09

Rob W