Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend css class with another style?

Tags:

css

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 } 
like image 295
hellzone Avatar asked Jan 13 '17 13:01

hellzone


People also ask

How can I apply one class to another class in CSS?

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.

What does @extend mean in CSS?

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.

Can an element have 2 CSS classes?

HTML elements can be assigned multiple classes by listing the classes in the class attribute, with a blank space to separate them.


2 Answers

You will have to use a CSS preprocessor to do this.

SASS

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

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 */ } 
like image 68
hungerstar Avatar answered Oct 16 '22 18:10

hungerstar


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.

like image 32
plvice Avatar answered Oct 16 '22 20:10

plvice