I have the following snippet of LESS code to generate CSS. I have some different classes which I apply to different elements, and on hover or focus they should change color.
.social-icons {
color:#bbbbbb;
// Material design palette, 500
.blue {
&:hover, &:focus {
color: #2196F3;
}
}
.red {
&:hover, &:focus {
color: #F44336;
}
}
.green {
&:hover, &:focus {
color: #4CAF50;
}
}
.teal {
&:hover, &:focus {
color: #009688;
}
}
.purple {
&:hover, &:focus {
color: #9C27B0;
}
}
.lime {
&:hover, &:focus {
color: #CDDC39;
}
}
.orange {
&:hover, &:focus {
color: #FF9800;
}
}
}
It produces the following CSS, which is good:
.social-icons {
color: #bbbbbb;
}
.social-icons .blue:hover,
.social-icons .blue:focus {
color: #2196F3;
}
.social-icons .red:hover,
.social-icons .red:focus {
color: #F44336;
}
.social-icons .green:hover,
.social-icons .green:focus {
color: #4CAF50;
}
.social-icons .teal:hover,
.social-icons .teal:focus {
color: #009688;
}
.social-icons .purple:hover,
.social-icons .purple:focus {
color: #9C27B0;
}
.social-icons .lime:hover,
.social-icons .lime:focus {
color: #CDDC39;
}
.social-icons .orange:hover,
.social-icons .orange:focus {
color: #FF9800;
}
I'm new to LESS, so I was wondering; Is there a way to make it even a shorter block of code in LESS? I feel like that &:hover, &:focus {}
is used more than it needs to be. I tried nesting all .color classes in :hover,:focus
and using &
to refer to them, but it didn't work as I expected.