Reading SASS basic features on their website, I stumbled upon the @extend
feature.
The example they give is the following:
.message {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
.success {
@extend .message;
border-color: green;
}
.error {
@extend .message;
border-color: red;
}
.warning {
@extend .message;
border-color: yellow;
}
That compiles to
.message, .success, .error, .warning {
border: 1px solid #cccccc;
padding: 10px;
color: #333;
}
.success {
border-color: green;
}
.error {
border-color: red;
}
.warning {
border-color: yellow;
}
Thus with this snippet of HTML
<div class="success">hello world</div>
you style your element with properties of .message
and .success
. However I feel this way of writing HTML (and CSS) very poor, in terms of semantic (you don't explicitly see from the markup above that the element has also the styling of .message
).
Shouldn't the snippet above be
<div class="message success">hello world</div>
?
I feel it to be more descriptive, and more easily reusable. For example I could assign the .success
class (as written in the above CSS but without the @extend
) to other elements which are not messages, and so using the class as a modifier, in a BEM-fashion.
So my question is, is the SASS example approach more desiderable than mine?