Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add the styles from one class to another class with LESS

Tags:

less

I have 2 classes with different styles. Can I apply the styles from one class to another when within a media query with LESS?

So with the following CSS an element with a class of .one is blue but not bold. With a set media query I also want it to be bold and 2ems.

.one {
 color: blue;
}

.two {
 font-weight: bold;
 font-size: 2em;
}
like image 998
Evanss Avatar asked Jan 13 '15 12:01

Evanss


3 Answers

Try

.one {
 color: blue;
}

.two {
 font-weight: bold;
 font-size: 2em;
}
@media screen {
   .one{
       .one;
       .two;
   }
}

will create this css:

.one {
  color: blue;
}
.two {
  font-weight: bold;
  font-size: 2em;
}
@media screen {
  .one {
    color: blue;
    font-weight: bold;
    font-size: 2em;
  }
}
like image 177
Grim Avatar answered Nov 05 '22 23:11

Grim


:extend can do this. In the code below if .class2 is in a media query then for that width the styles from .class will be applied.

.class {
  some rules here 
}

.class2 {
    &:extend(.class);
    ...
}

Does LESS have an "extend" feature?

like image 27
Evanss Avatar answered Nov 06 '22 01:11

Evanss


You can use a mixin and a variable for the color

@default_color : "#000";
@color : blue;

.font-style(@c: @default_color) {
  color: @c;
  font-weight: bold;
  font-size: 2em;
}

.one {
 color: @color;
}

.two {
  .font-style()
}

@media (max-width:420px) { 
  .one {
    .font-style(@color)
  }
}

Codepen demo

In the demo above, resize the window to lesser width and you will find the text changing it's style.

like image 1
anpsmn Avatar answered Nov 06 '22 01:11

anpsmn