13

in the Header of my HTML-Page i set the following CSS Variables:

:root{ 
  --data-color-primary: #ffcc00; 
  --data-color-secondary: #4b4b4b; 
}

In my SASS-File i use it as follow:

DIV.color {

   &.color-primary {
     background-color: var(--data-color-primary);
   }
   &.color-secondary {
     background-color: var(--data-color-secondary);
   }
}

Now i try to set the font-color depending on the brightness of the background-color:

@function set-notification-text-color($color) {
  @if (lightness($color) > 60) {
     @return #000000;
  } @else {
     @return #ffffff;
  }
}

DIV.color{
   &.color-primary {
      background-color: var(--data-color-primary);
      color: set-notification-text-color(var(--data-color-primary));
   }
}

But in my SASS-compliler i get the following i get the following Error:

Error: argument $color of lightness($color) must be a color

How is ist possible to hand over der CSS variable to the function.


My Problem is, the CSS Variables are set in the Backend of my CMS by User (Liferay 7) an will be rendered in a *.FTL-File and is printed in the HTML-Code.

$primary: ${clr_primary};
$secondary: ${clr_primary};

and then i cant use the SASS-Variable $primary in my SASS-File.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
hydrococcus
  • 414
  • 2
  • 5
  • 20

2 Answers2

37

Use SASS variables in your CSS variables.

$primary: #ffcc00;
$secondary: #4b4b4b;

:root{ 
  --data-color-primary: #{$primary};
  --data-color-secondary: #{$secondary};
}

Now call your mixin

DIV.color{
   &.color-primary {
      background-color: $primary;
      color: set-notification-text-color($primary);
   }
}

Another options would be to create a mixin which retrieves the CSS variable

@function color($color-name) {
  @return var(--data-color-#{$color-name});
}

Now call that function like so

DIV.color {
   &.color-primary {
      background-color: color(primary);
      color: set-notification-text-color(color(primary));
   }
}

Check out this link for usefull information about combining CSS and SASS variables

https://codepen.io/jakealbaugh/post/css4-variables-and-sass

Red
  • 6,599
  • 9
  • 43
  • 85
  • @RaduM What specifically doens't work as of SASS 3.5.6? – Red Nov 05 '18 at 10:11
  • 2
    If you use Sass variables inside a :root { } they will not be compiled to their values. Put the following code into https://www.sassmeister.com/ and you will see. Switch versions then. $primary: #ffcc00; $secondary: #4b4b4b; :root{ --data-color-primary: $primary; --data-color-secondary: $secondary; } – RaduM Nov 06 '18 at 11:09
  • 1
    @RaduM I see, thanks for pointing out. I updated the code and added a solution for SASS v3.5.6+ aswell. – Red Nov 06 '18 at 13:08
3

If you need to change CSS variables outside of :root you can do the following

.class {
  // sass-lint:disable no-duplicate-properties
  #{--background}: transparent;      
  #{--background-focused}: transparent;
  // sass-lint:enable no-duplicate-properties
}

and this compiles to

.class {
  --background: transparent;      
  --background-focused: transparent;
}
Guest
  • 31
  • 1