5

How i can do this?

// style.scss

$primary-color: #dc4545;

div{
    background : $primary-color;
}

Try to do this:

div{
    background : var(--primary-color)
}

Is this possible in any way?

1

2 Answers 2

6

You can define global variables on the :root:

:root {
  --primary-color: #dc4545; 
}

div {
  background: var(--primary-color); 
}

Edit: Or were you trying to mix and match?

$primary-color: #dc4545;

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

div {
  background: var(--primary-color); 
}
Sign up to request clarification or add additional context in comments.

2 Comments

i know how much variable are used in this project . But is not possible to change one by one because those variable are used more then one file so is that any other way to convert .
Unfortunately not the SASS variables exists pre-compiled only so for CSS to recognize the variable you'll need to convert it to be defined on CSS first.
-1

First of all :root and html selector basically same thing but :root with higher specificity than html selector

html {
  
}

/*****exactly :root and html selector are same but with higher specifity(:root)*****/
:root {
  --color-primary-light: #FF3366;
  --color-primary-dark: #BA265D;
  --bakcground-color: #fff;
  --default-font-size: 16px;
  --color: blue;
  }
  
  /****************How to implement***************/
  .root-selector {
    background-color: var(--background-color);
    font-size: var(--default-font-size);
    color: var(--color);
  }
<div class="root-selector">
  Thank you buddy
</div>

1 Comment

:root with higher specificity --> what do you mean by this? root will select nothing, so there no specificity here

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.