문제

I'm currently using LESS instead of CSS and I would like to know if there is a way to get the current selector + a class. There is a way of doing this with a pseudo-class but as far as I'm aware, not with a class.

Psuedo-Class:

li {
    a {
        &:hover {

        }
    }
}

The &:hover basically gets li a:hover. What I would like to know is how I would do this for a class. For example, if I wanted to get ul li.active I'd have to use the below:

ul {
    li {

    }
    li.active {

    }
}

I was hoping for something closer to the first segment of code but I couldn't see anything on the LESS features page. Is there something like this that I just overlooked?

도움이 되었습니까?

해결책

Just chain the class selector like you would with a pseudo-class:

ul {
    li {
        &.active {

        }
    }
}

You can do this with any one or more simple selectors that isn't a type or * selector, as well as with a pseudo-element. The & can occur in most places where a selector fragment can be injected into another without causing a syntax error; the examples given in the documentation with pseudo-classes and pseudo-elements are not the only ones. The documentation has an entire section on using & which can be found here.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top