Set a Variable in SASS Depending on the Selector

Set a variable in Sass depending on the selector

I think a mixin is the answer. (As I wrote, variables won’t work.)

@mixin content($color-default, $color-main) {
background: $color-default;
color: $color-main;
}

body.class-1 {
@include content(#444, #555);
}

body.class-2 {
@include content(#666, #777);
}

That SCSS compiles to this CSS:

body.class-1 {
background: #444444;
color: #555555; }

body.class-2 {
background: #666666;
color: #777777; }

If you wanted to group the color values together in your SCSS file, you could use variables in conjunction with the mixin:

$color-1: #444;
$color-2: #555;
$color-3: #666;
$color-4: #777;

body.class-1 {
@include content($color-1, $color-2);
}

body.class-2 {
@include content($color-3, $color-4);
}

Scss Variable based on class

I personally prefer using a function for that, but you could make it with a mixin too.

Create a Map with properties bind to as many values as themes you have.

Then, define a custom property for fetching this map and return the wanted value.

$theme: (
color: (#000, #FFF),
border: (dashed, solid),
// etc.
);

@function theme($property, $index) {
@return nth(map-get($theme, $property), $index);
}

.light.foo {
color: theme(color, 1);
}

.dark.foo {
color: theme(color, 2);
}

Demo SassMeister

Set a SASS variable depending

There are multiple ways to do this. The most obvious two which come to mind are mixins and loops:

Mixins

Just put everything you want into a single mixin, and then use it for every body class:

@mixin colored-content($color) {
.content a:hover, .content .some-selector {
color: $color;
}

/* Any other rules which use $color here */
}

.body-1 {
@include colored-content(green);
}

.body-2 {
@include colored-content('#FF0000');
}

.body-3 {
@include colored-content(darken(red, 20));
}

You can extend this example with any number of arguments (for example, $textColor and $bgColor), conditions or rules.

With this approach you will not have SCSS code repetitions, and any updates will be introduced easily.

Loop

Another way is to use a simple loop:

$body_themes: (
"body-1": green,
"body-2": #FF0000,
"body-3": darken(red, 2)
);

@each $body_class, $color in $body_themes {
.#{$body_class} {
.content a:hover, .content .some-selector {
color: $color;
}

/* Any other rules which use $color here */
}
}

It is even shorter, but imho it is less readable.

P.S. It is possible to combine mixins and loops, by the way :)

Use name of selector as value for a variable in SASS

Try something like this:

@mixin left-icon ($bgcolor, $bgurl, $iconmargin) {
##{$bgurl} {
background: $bgcolor url(images/#{$bgurl}.png) no-repeat left 5px center;
& h2, & p {margin-left: $iconmargin;}
}
}

@include left-icon(#de4c3f,eleven,10px);

Basically, you can add variables anywhere using Sass's interpolation syntax

Override SASS variable inside of a selector

Consider using a mixin instead of a variable for this application. In your selector, you can include the mixin and pass it a variable. You can even set a default value for the mixin variable, in this case your base background-color. Override it by passing the mixin a new value.

Sass

@mixin bg-color($primary: #6cc907) {
background-color: $primary;
}

div {
@include bg-color(); // Use default value

display: inline-block;
height: 300px;
width: 200px;

&.branded {
@include bg-color(#e43e94); // Override value
}
}

Codepen example



Related Topics



Leave a reply



Submit