Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "&" do in LESS CSS?

Trying to figure out what the & does in the below code?

  .controls-container  {
        .flex-control-nav  {
            li a  {
                &.flex-active  {
                    filter: none;
                    background: @color_links!important;
                }
            }
        }
    }

I know you can do something like &:hover, but not familiar with showing a class right after it?

like image 361
Jared Avatar asked Jan 08 '13 13:01

Jared


3 Answers

I know you can do something like &:hover, but not familiar with showing a class right after it?

It's the same logic here: chaining the inner selector to whatever is represented by the outer selector. Pseudo-classes like :hover and classes like .flex-active behave the same with &.

Compiling that to CSS will result in a rule with the selector

.controls-container .flex-control-nav li a.flex-active
like image 162
BoltClock Avatar answered Nov 12 '22 07:11

BoltClock


Compiler will replace "&" with current outer selector

This makes it possible to provide different styles for i.e. :hover, :active, :before etc. or any other case as yours.

In your case the inner-most selector would after compilation look like this in resulting CSS file:

.controls-container .flex-control-nav li a.flex-active { ... }
like image 40
Robert Koritnik Avatar answered Nov 12 '22 08:11

Robert Koritnik


The & appends the parent nesting structure where ever the & may occur. So as others have noted, putting the & in your example takes the full nested string of .controls-container .flex-control-nav li a and replaces the & in your code to (in this case) cause the .flex-active class to be conjoined to the a tag...

.controls-container .flex-control-nav li a.flex-active

...instead of a child of the a tag...

.controls-container .flex-control-nav li a .flex-active

If you just think of it as a "string replacement" for a selector, it will help you visualize. For instance this (note where I have put the &)...

.controls-container  {
    .flex-control-nav  {
        li a  {
            .flex-active & {
                filter: none;
                background: @color_links!important;
            }
        }
    }
}

...would produce this selector...

.flex-active .controls-container .flex-control-nav li a

...as it would append the nested structure after the class .flex-active.

like image 8
ScottS Avatar answered Nov 12 '22 06:11

ScottS