Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

postcss-for

antyakushev421.9kMIT2.1.1

PostCSS plugin that enables SASS-like for loop syntax in your CSS

postcss, css, postcss-plugin, for, loop, sass, from, to, iterate, nested

readme

PostCSS For Plugin

Build Status NPM version Dependency Status NPM Downloads

PostCSS plugin that enables @for loop syntax in your CSS.

Try it out!

You can try postcss-for directly from codepen. Just choose PostCSS as a preprocessor and pick desired plugin from the list.

lalala

Usage

postcss([ require('postcss-for') ])

Note, that unlike the Sass @for, postcss-for in the example below iterates from 1 to 3 inclusively.

@for $i from 1 to 3 {
    .b-$i { width: $(i)px; }
}
.b-1 {
    width: 1px
}
.b-2 {
    width: 2px
}
.b-3 {
    width: 3px
}

This plugin must be set before postcss-nested and postcss-simple-vars. Therefore dollar variable cannot be used as a loop range parameter. If you do want to use predefined range parameters though, consider using postcss-custom-properties with postcss-at-rules-variables, or look into this postcss-for fork.

More features

By keyword is available:

@for $i from 1 to 5 by 2 {
    .b-$i { width: $(i)px; }
}
.b-1 {
    width: 1px
}
.b-3 {
    width: 3px
}
.b-5 {
    width: 5px
}

Locality of variables in nested loops is supported:

@for $x from 1 to 2 { 
    @for $y from 1 to $x { 
        @for $z from $y to $x { 
            .c-$(x)-$(z)-$(y) { padding: $(x)em $(z)em $(y)em; } 
        }
    }
}
.c-1-1-1 {
    padding: 1em 1em 1em
}
.c-2-1-1 {
    padding: 2em 1em 1em
}
.c-2-2-1 {
    padding: 2em 2em 1em
}
.c-2-2-2 {
    padding: 2em 2em 2em
}

See PostCSS docs for examples for your environment.

changelog

2.1.1

  • Fixed :root selector support

2.1.0 by Travis Palmer

  • Added support for nesting @for loops while using variables in the range that are iterators of parent @for loops
  • Added tests for added functionality
  • Migrated and changed the wording on variables not allowed in range error to be only for non-local @for loop variables
  • Added example to README of nested loops

2.0.3

  • Bump simple-vars to major 2.0.0

2.0.2

  • Added support for negative values

2.0.1

  • Removed deprecation warning

2.0.0

  • Update dependencies
  • Bump major version

1.1.0

  • Added support for nested loops

1.0.1

  • Update examples and tests to contain valid CSS

1.0.0

  • Update according to the plugin guidelines
  • Bump to major release, since in 0.2.0 breaking changes were introduced and alas were not bumped.

0.2.0

  • Change to keyword to mean inclusively
  • Remove through keyword Sorry, these are the breaking changes.

0.1.0

  • Add by keyword

0.0.1

  • First version of a plugin