0

everyone!

I've got this files structure

_utils.scss
_variables.scss
styles.scss

in styles.scss I import every partial with:

@use "utils" as *;
@use "variables";
body {
  min-height: 100vh;
  background-color: var(--cl-primary);
  font-size: var(--fs-normal);
}

The content of _utils.scss is this:

@use "sass:math";
/// Converts PX to REM
/// @access public
/// @param {string} $size - Value to be converted in PX.
/// @returns {string} - Returns REM string.
/// @example font-size: rem(24px);
/// @returns font-size: 1.5rem;
@function toRem($size) {
  $remSize: math.div($size, 16px);
  @return #{$remSize}rem;
}

And the content of _variables.scss is this:

:root {
  --cl-primary: #3829e0;
  --cl-primary-tint: #e0e8ff;

  --fs-normal: toRem(32px);
}

Questions:

Why on the webpage the css custom property --fs-normal is evaluated to "toRem(32px)" instead of 2rem?

2nd question: what should I do in order to get --fs-normal to be evaluated to 2rem?

Thank you all!

EDIT: i tried to reproduce a given solution in a fiddle: https://jsfiddle.net/mihai3636/fcjspt1v/24/

EDIT2:

ANSWER:

Beside string interpolation I should have added this line inside _variables.scss:

@use "./utils" as *;

Please read the last comment from Invulner on the answer

So _variables.scss should look like this in order to work:

@use "./utils" as *;
:root {
  --cl-primary: #3829e0;
  --cl-primary-tint: #e0e8ff;

  --fs-normal: #{toRem(32px)};
}
2
  • Does this answer your question? Unable to set SCSS variable to CSS variable? Commented Apr 9, 2024 at 8:31
  • Not really. What helped me was both interpolation + importing the utils file inside my _variable.scss partial (check Invulner's last comment from his answer). What I mean is that a part of my fix is also found in the question linked by you, but the solutions / questions are different if you ask me. Before asking this quesiton I personally stumbled upon other answers mentioning interpolation, I tried it but it didn't work by itself. Commented Apr 9, 2024 at 19:02

1 Answer 1

1

From the doc:

To provide maximum compatibility with plain CSS, more recent versions of Sass require SassScript expressions in custom property values to be written within interpolation. Interpolation will also work for older Sass versions, and so is recommended for all stylesheets.

So, without interpolation, the function call within a CSS custom property is treated as a string literal by SCSS and not executed. To make it work, you need to add interpolation with #{}:

--fs-normal: #{toRem(32px)};
Sign up to request clarification or add additional context in comments.

6 Comments

I already tried that but I get the same result, sorry I forgot to mention it. I am new to scss and I'm not really sure if I'm doing something else wrong. I'll try to wrap up a short live example.
Check my main post, I posted a small fiddle at the end. Nothing seems to work.
@Mihai Looks like jsFiddle doesn't support the newer Sass module system introduced in Dart Sass. Check this example, without math:sass. It's intended to verify that interpolation effectively passes the function result to a CSS variable.
Thanks for fixing that jsFiddle. In my code I have the same things but it just does not understand the #{toRem(32px)}. Here is the code on my github repo, perhaps you could spot something which is obviously wrong: github.com/mihai3636/order-summary-component Also, I'm using VsCode Live Sass Compiler by Glenn Marks, do you think something could be wrong with that compiler?
@Mihai From the documentation: @use makes variables, functions, and mixins available only within the scope of the current file and never adds them to the global scope. Therefore, simply add @use "./utils" as *; to the file where the utility is used.
|

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.