2

I want to ask how I can auto-generate css variable by sass function

for example: I want this variable to auto-generate

:root{--sample1:red;}

SASS function:

@function brand($key,$val) {
 //this not works  
 :root{
    --#{$key}:$val;
  }
@return val(--#{$val});}

use the brand function

   header{
       background-color:brand("sample1",red)
   }
5
  • 1
    What do you mean by auto generate? Commented Oct 23, 2018 at 10:45
  • I mean generate variable my SASS function Commented Oct 23, 2018 at 11:07
  • That doesn't clarify anything. Give us the input and expected output and the method you wish to use. Do you mean by using a preprocessor? How should it auto-generate? Commented Oct 23, 2018 at 11:09
  • Atleast your var(--#{$val}) is wrong as it should be #{$key} Commented Oct 23, 2018 at 11:31
  • This guy uses maps to create something similar. His approach is interesting and maybe it helps you to find a better way to resolve your problem: codepen.io/jakealbaugh/post/css4-variables-and-sass Commented Oct 23, 2018 at 11:54

1 Answer 1

5

I achieved something similar, here is the idea.

$spacing: (
  none: none,
  01: 4px,
  02: 8px,
  03: 16px,
  04: 24px,
  05: 32px,
  06: 40px,
  07: 48px,
  08: 56px,
  09: 64px,
  10: 72px,
  11: 80px,
  12: 88px,
  13: 96px
);

:root {
  // SPACING Custom Properties
  @each $space, $value in $spacing {
    --spacing-#{$space}: #{$value};
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.