I have nearly 30 classes and I want to apply this classes to my button element. I don't want to add class attribute for every button element. Is there any way to create a new button class like;
.button{ .rounded-corner .corner .button-effective //another 20 classes }
You can't declare a CSS class an then extend it with another CSS class. However, you can apply more than a single class to an tag in your markup ... in which case there is a sophisticated set of rules that determine which actual styles will get applied by the browser.
The @extend directive lets you share a set of CSS properties from one selector to another. The @extend directive is useful if you have almost identically styled elements that only differ in some small details.
HTML elements can be assigned multiple classes by listing the classes in the class attribute, with a blank space to separate them.
You will have to use a CSS preprocessor to do this.
placeholder
%rounded-corner {} %corner {} %button-effective {} .button { @extend %rounded-corner; @extend %corner; @button-effective; /* Some other styles. */ } .box { @extend %rounded-corner; }
Compiles to:
.button, .box { /* rounded-corner styles */ } .button { /* corner styles here */ } .button { /* button-effective styles here */ } .button { /* Some other styles. */ } /* `.box` is NOT defined here because it only uses placeholders. So it is placed where the placeholder is defined. */
Note: with placeholders, the CSS selector is added to wherever the placeholder is defined. Not where the selector is defined.
extend
.rounded-corner {} .corner {} .button-effective {} .button { @extend .rounded-corner; @extend .corner; @extend .button-effective // Continue with other classes. }
Compiles to:
.rounded-corner, .button {} .corner, .button {} .button-effective, .button {}
mixin
@mixin rounded-corner {} @mixin corner {} @mixin button-effective {} .button { @include .rounded-corner; @include .corner; @include .button-effective // Continue with other classes. }
Compiles to:
.button { /* rounded-corner styles here */ /* corner styles here */ /* button-effective styles here */ }
LESS has a similar sytanx to SASS and also has extend and mixin, though LESS is a little more forgiving if you want to add one class' style to another. While I believe still considered a mixin in LESS, you can add one class style to another like the following without having to use a keyword.
.rounded-corner {} .corner {} .button-effective {} .button { .rounded-corner; .corner; .button-effective; // Continue with other classes. }
Compiles to:
.button { /* rounded-corner styles here */ /* corner styles here */ /* button-effective styles here */ }
It will be possible in CSS4:
:root { --toolbar-theme: { border-radius: 4px; }; --toolbar-title-theme: { color: green; }; } .toolbar { @apply --toolbar-theme; @apply --toolbar-title-theme; }
For now, you need to use Sass/Less preprocessors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With