Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LESS add class with pseudo selector

Tags:

css

less

I use LESS and here is my example:

.arrow{
   color: red;
}
.arrow:before{
   content: ">";
}

.button{
  background: blue;
  .arrow;
  &:before{
    border: 1px solid;
  }
}

And this is CSS after parsing:

.button{
   background: blue;
   color: red;
}
.button:before{
   border: 1px solid; // HERE IS NO  content: ">" !!!
}

How to add :before pseudo-element from .arrow class to my button?

like image 405
Sergey Kudryashov Avatar asked Aug 18 '14 08:08

Sergey Kudryashov


People also ask

Can you use multiple pseudo class selectors with an element?

Adding multiple pseudo elementsYou can combine several CSS pseudo-elements for one element. However, you cannot use CSS ::after two times or ::before two times. In the example below, the first letter of <div> is green and has x-large font size.

What are pseudo class selectors?

A pseudo-class is a selector that selects elements that are in a specific state, e.g. they are the first element of their type, or they are being hovered over by the mouse pointer.

What is a pseudo selector give an example?

A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected element(s). For example, the pseudo-class :hover can be used to select a button when a user's pointer hovers over the button and this selected button can then be styled.

Can CSS classes combine with pseudo classes?

Combining pseudo classes with CSS classesIt is possible to combine pseudo classes and regular CSS classes. This combination lets you style elements that have specific classes and also react to certain external factors.


1 Answers

You can use the extend option like below. It basically applies all properties of the arrow class to the button class also. The all keyword means the child classes are also extended.

LESS:

.button{
    background: blue;
    &:extend(.arrow all);
    &:before{
        border: 1px solid;
    }
}

Compiled CSS:

.arrow,
.button {
    color: red;
}
.arrow:before,
.button:before {
    content: ">";
}
like image 107
Harry Avatar answered Oct 26 '22 22:10

Harry