13

I have this variable:

$color_pr1: #d6ad3f;

Now, I'm using Gumby and it uses it's own settings sheet where the following is set:

$header-font-color: #55636b !default;

Is it possible to use $color_pr1 instead? Like this?

$header-font-color: $color_pr1; ?

If now, am I thinking about this all wrong? I'd like to have my own set of colors etc and reuse those within my project.

3
  • Yeah you can do it. You should try before ask. Commented Apr 29, 2013 at 17:49
  • There's no issue in doing just that, try it and you'll see it works. But you should aim at keeping your code readable. Commented Apr 29, 2013 at 18:07
  • I did try.. It was the !default; bit behind the second assigned variable that was keeping Scout from compiling. Commented Apr 29, 2013 at 21:19

3 Answers 3

11

From the docs: http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#variable_defaults_

You can assign to variables if they aren’t already assigned by adding the !default flag to the end of the value. This means that if the variable has already been assigned to, it won’t be re-assigned, but if it doesn’t have a value yet, it will be given one.

For example:

$content: "First content";
$content: "Second content?" !default;
$new_content: "First time reference" !default;

#main {
  content: $content;
  new-content: $new_content;
}

is compiled to:

#main {
  content: "First content";
  new-content: "First time reference"; }

Variables with null values are treated as unassigned by !default:

$content: null;
$content: "Non-null content" !default;

#main {
  content: $content;
}

is compiled to:

#main {
  content: "Non-null content"; }
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, it was the !default; that was bugging me. Thanx! :) Yes, I did try it before asking of course. I'm using ScoutApp for compiling, and it just gave me an error before.
2

Use css calc() function:

$header-font-color: calc(#{$color_pr1});

Comments

2

You can define a map:

From the Sass Documentation

Users occasionally want to use interpolation to define a variable name based on another variable. Sass doesn’t allow this, because it makes it much harder to tell at a glance which variables are defined where. What you can do, though, is define a map from names to values that you can then access using variables.

SCSSSassCSS
SCSS SYNTAX
@use "sass:map";

$theme-colors: (
  "success": #28a745,
  "info": #17a2b8,
  "warning": #ffc107,
);

.alert {
  // Instead of $theme-color-#{warning}
  background-color: map.get($theme-colors, "warning");
}

1 Comment

I got a "Invalid CSS after" error and it turns out there is an error in the documentation, it is map-get and not map.get -> github.com/sass/node-sass/issues/2890#issuecomment-616216159

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.