The vary mixin is the most flexible, scalable and modular Sass theming solution out there.

Quick start

Compatible with Sass 3.4+ and libsass 3.2+, vary covers every aspect of CSS theming, from using body classes to BEM modifiers and all in between, solving lots of consistency and maintenance issues.

But let's see how this works. The most common theming pattern is using a .is-foo body class, where foo is the entity for which the CSS variations are destinated to (e.g. user, admin). Each of these entities will have a different styling.

Body class example
$vary-map: (
  user: (
    'color-primary': blue,
    'border': '1px solid #ff0'
  ),
  admin: (
    'color-primary': pink,
    'border': '10px solid black'
  )
);

.foo{
	background: red;

	@include vary($create: parent) {
		background: vary-get('color-primary');
		border: vary-get('border');
	}
}
.foo{
	background: red;	
}

.is-user .foo{
	background: blue;
	border: 1px solid #ff0;
}

.is-admin .foo{
	background: pink;
	border: 10px solid red;
}

In order to relate the values with the entities, vary uses Sass maps. Build your $vary-map and you can start creating variations of any selector.

Withing the vary scope, you can request values like the primary color with vary-get. Then vary will loop through the entities in the map and create whatever has been specified for you (in this case a body class) per entity, each with the corresponding primary color and border.

Keeping the entity names in a single place across the codebase helps with consistency and scalability. If you ever need to create another variation, just add the values to the map and it will replicate accross the entire CSS.

What else?

vary is very powerful and full of possibilities. Here's a summary of what it can do.

Creation modes

vary has 5 creation modes at the moment:

Check out the demos page to see the creation modes in action.

Flexibility

It's nice that vary goes through every entity for you, but on some cases you may want something different for specific entities only or to exclude some from the list. Maybe you even have multiple maps to loop through.

Well, vary has your back. On the current version of vary you have support for:

It also allows you to have a centralised set of variables that can be accessed using the vary-get function. Check out the docs to know more about the functions and mixins included.

How to get it

vary is licensed under the MIT license and totally available on GitHub and it is currently mantained by me, Jaime Caballero.

To get a copy you can get the latest version from GitHub or use Bower:

bower install sass-vary --save

Once you get it, import it into your project and put these into your variables file.

$vary-libsass: true;

$vary-map: (
  user: (
    'color-primary': blue,
    'border': '1px solid #ff0'
  ),
  admin: (
    'color-primary': pink,
    'border': '10px solid black'
  )
);

Set up your $vary-map with the entities and colours relative to your project and you are ready to go!