4dwk_q
Last Updated: February 25, 2016
·
2.544K
· kenzic
Ba7fbe2d859cefde482f5e96fb63e1ed

Inheritance in LESS

Less 1.4 adds inheritance in the form of @extend. Similar to a @mixin
but more efficient in its output. A mixin will produce the same chunk
of code everytime its used but extend groups your selectors
together.

.btn {
    color: red;
    background: green;
    border: 1px solid red;

    span {
        background: url('smile.png') no-repeat;
    }
}


.go-btn {
    &:extend(.btn);

    color: green;
}

compiles to:

.btn,
.go-btn {
    color: red;
    background: green;
    border: 1px solid red;
}
.btn span {
    background: url('smile.png') no-repeat;
}
.go-btn {
    color: green;
}

Passing in all extends nested elements

.btn {
    color: red;
    background: green;
    border: 1px solid red;

    span {
        background: url('smile.png') no-repeat;
    }
}

.go-btn {
    &:extend(.btn all);

    color: green;
}

Compiles to:

.btn,
.go-btn {
    color: red;
    background: green;
    border: 1px solid red;
}
.btn span,
.go-btn span {
    background: url('smile.png') no-repeat;
}
.go-btn {
    color: green;
}
Say Thanks
Respond

1 Response
Add your response

6938
D0788c57eb9d98743be2bb20c46101cb

Very nice and smooth ouput, though I would have preferred that the "all" option was the default behavior for this. If @extend is meant to mimic inheritance, the "children" should inherit all its parent's properties by default, shouldn't they?

over 1 year ago ·