最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sass - How to overwrite css variables value with scss variables - Stack Overflow

programmeradmin6浏览0评论

I am working with primeng and would like to overwrite the css variable value. (this is not important but this will make my issue more clear and what I try to achieve)

for example if I have an badge and i would like to set the background color.

in my debug console I can retrieve the styling that is applied.

background: var(--p-badge-primary-background);

in my stylesheet I have the following code

@import "styleGuide";

.p-badge {
  --p-badge-primary-background: red;
}

this works fine but I have a whole styleguide defined and I want to use this as a template so I can changed the layout/style with minimal changes. I don't want look for al the refences of specific color. (here I overwrite a css varible with a new value)

What I like to achieve is to use a scss variables for the value that is defined in my styleguide that I imported.

.p-badge {
  --p-badge-primary-background: $color-brand-primairy; 
}
    

I am working with primeng and would like to overwrite the css variable value. (this is not important but this will make my issue more clear and what I try to achieve)

for example if I have an badge and i would like to set the background color.

in my debug console I can retrieve the styling that is applied.

background: var(--p-badge-primary-background);

in my stylesheet I have the following code

@import "styleGuide";

.p-badge {
  --p-badge-primary-background: red;
}

this works fine but I have a whole styleguide defined and I want to use this as a template so I can changed the layout/style with minimal changes. I don't want look for al the refences of specific color. (here I overwrite a css varible with a new value)

What I like to achieve is to use a scss variables for the value that is defined in my styleguide that I imported.

.p-badge {
  --p-badge-primary-background: $color-brand-primairy; 
}
    
Share Improve this question edited Mar 20 at 13:56 Naren Murali 60.3k5 gold badges44 silver badges78 bronze badges asked Mar 20 at 13:50 BabulaasBabulaas 8894 gold badges16 silver badges51 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

You can use interpolation to do this.

SASS Interpolation Docs

.p-badge {
  --p-badge-primary-background: #{$color-brand-primary};
}

What I do is this:

on helpers.scss

@use 'variables' as vars;

on _variables.scss

$black: #000;
$white: #fff;

$colors: (
  'black': $black,
  'white': $white,
);
:root {
  @each $name, $color in $colors {
    --#{$name}: #{$color};
  }
}

and I get on main.css

:root {
  --black: #000;
  --white: #fff;
}
发布评论

评论列表(0)

  1. 暂无评论