Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LESS mix-in for nth-child?

Tags:

css

mixins

less

I'm trying to make a LESS mixin that will give me this output:

.resource:nth-child(8n+1) { clear: left; }

I've got this so far:

.wrap-every(@n) {
    &:nth-child(@n + "n+1") {  // parse error on this line
        clear: left;
    }
}

.resource {
    .wrap-every(8);
}

But it's giving a parse error on the indicated line

ParseError: Unrecognised input

Is there a way to do this?

like image 598
recursive Avatar asked Aug 08 '13 05:08

recursive


People also ask

How do I choose a multiple nth child?

Definition and Usage The :nth-child(n) selector matches every element that is the nth child of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of the same type (tag name), of its parent.

How do you find the nth child of an element?

Use the querySelector() method to get the nth child of an element, e.g. document. querySelector('#parent :nth-child(3)') . The :nth-child pseudo class returns the element that matches the provided position.

How do you select the first 4 children in CSS?

The syntax for selecting the first n number of elements is a bit counter-intuitive. You start with -n, plus the positive number of elements you want to select. For example, li:nth-child(-n+2) will select the first 2 li elements.

How do you target the nth child in SCSS?

You can pass in a positive number as an argument to :nth-child() , which will select the one element whose index inside its parent matches the argument of :nth-child() . For example, li:nth-child(3) will select the list item with an index value 3; that is, it will select the third list item.


1 Answers

Less >= 1.4

you could do something like this:

.wrap-every(@n) {
  &:nth-child(@{n}n + 1) {
        clear: left;
    }
}

this should have the desired output. Without any hacks needed.

in older versins of Less

you can try simple string interpolation:

.wrap-every(@n) {
    @t: ~":nth-child(@{n}n + 1)";
    &@{t} {
        clear: left;
    }
}

and the output CSS in both cases should be something like this:

.resource:nth-child(8n + 1) {
  clear: left;
}
like image 169
Martin Turjak Avatar answered Oct 02 '22 19:10

Martin Turjak