How to import variables from SASS into plain CSS?

Imagine having a vast array of SASS variables spread across different .scss files, such as:

$app-color-white: #ffffff;
$app-color-black: #000000;

How can we efficiently convert these variables into vanilla CSS variables?

:root {
  --app-color-white: #ffffff;
  --app-color-black: #000000;
}

Is there a SASS-specific method or perhaps a pre-processor that can help with this task?

I aim to use my SASS framework seamlessly in vanilla CSS projects as well.

Answer №1

This functionality is now achievable thanks to the utilization of Sass modules along with the innovative sass:meta.module-variables() feature: it

Provides a comprehensive list of all variables defined within a module, mapping variable names (without $) to their respective values.

Here is an example:

// _custom_vars.scss

$bg-color: blue;
$text-color: white;
// main_style.scss

@use 'sass:meta';
@use '_custom_vars';

:root {
  @each $variable_name, $variable_value in meta.module-variables(_custom_vars) {
    --#{$variable_name}: #{$variable_value};
  }
}

The output will be:

:root {
  --bg-color: blue;
  --text-color: white;
}

⚠️ Please note that Sass modules are currently exclusive to Dart Sass.

Answer №2

In my opinion, utilizing a variable map would be the most effective approach for achieving this task;

For example:

// creating a Sass variable map
$colors: (
  primary: #FFBB00,
  secondary: #0969A2
);

// extracting CSS4 variables from color map
:root {
  // iterating through each item in color map
  @each $name, $color in $colors {
    --color-#{$name}: $color;
  }
}

Resulting output:

:root {
  --color-primary: #FFBB00;
  --color-secondary: #0969A2;
}

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating an optical illusion using nested left borders in HTML and CSS for a captivating and repeating visual impact

I am looking to achieve a nested border-left effect on HTML lists where the left border moves in one step with each nested level: https://i.sstatic.net/QDS3d.png What I want is something similar to this with minimal gap between the listed term and bullet ...

HTML - Oversized Header Extending Beyond Screen Width

I'm currently building an HTML web page and encountering an issue with the header. It seems that the header is wider than my screen size, resulting in a horizontal scroll bar appearing. I've tried some solutions, but none of them seem to work. An ...

Animating CSS Pixel Fragments

After applying a simple CSS animation that moves size and box shadows from one side of the screen to the other, I am noticing residual pixel fragments left behind. To see this issue in action on Chrome 66, check out the Code Pen: https://i.sstatic.net/Gl ...

Interactive HTML and CSS tree structure viewing experience

I am looking to create a responsive tree diagram, here is a basic example: https://jsfiddle.net/ppnfpggx/2/ However, I am facing some challenges with the responsive layout. Particularly on smaller devices, it should appear like this:https://i.sstatic.net/ ...

Tips for maintaining color on <a> tags even after they have been clicked

My webpage has 3 <a> tag links to 3 different pages. I have set the hover state color to red, and now I want the background-color of the clicked page to remain as it was in the hover state. How can I achieve this? <html lang="en"> < ...

The sticky and flexible footers and headers CSS feature is functioning perfectly in WebKit, though it seems to be having difficulties in

I'm working on constructing a layout that features a header and footer with flexible heights, while the middle section takes up the remaining space. If there is overflow in the middle section, a scroll bar should appear specifically for that section. ...

The text is not displaying correctly in the HTML format

Here is a snippet of text that I'd like to maintain the original layout for: <p> We recently reduced the number of savings accounts we offer. I welcomed this because I think members find it confusing to look through tables of lots of slightly ...

Struggling to implement a seamless scrolling effect using CSSTransition Group

In my quest to achieve a smooth vertical scroll effect for content appearing and disappearing from the DOM, I've turned to CSSTransitionGroup (or ReactTransitionGroup). While I'm familiar with CSS animations for such effects, I need to implement ...

Arrangement of images in an array

Here's the scenario I'm facing. https://i.stack.imgur.com/FbAfw.jpg So, I have an array of images that I want to use to create a gallery with a specific layout. I've tried using grid and playing around with :nth-child(even) and :nth-child( ...

Creating visually appealing website designs with Firefox by implementing smooth background image transitions using Javascript with

Currently, I am facing an issue with the banner area on my website. The background of the banner is set to change every 10 seconds using JavaScript. However, there seems to be a flickering effect that occurs for a brief moment when the background-image is ...

Spinning an object using JQuery

I am currently working on a project to test my skills. I have set up a menu and now I want to customize it by rotating the icon consisting of three vertical lines by 90 degrees every time a user clicks on it. This icon is only visible on smartphones when t ...

Struggles arise when the Flexbox container fails to accurately calculate its width and causing overflow

My goal is to create a reusable React library that can take a list of components and display them in a flex container. One key feature is that the library recalculates the component's width when it mounts or when the window is resized. If the total wi ...

Tips for creating space between flex columns without causing content to overflow

I am struggling with a simple 2-column flex setup where I want to add some margins between the columns. However, when I try to do this, the second column ends up overflowing outside of the .flex-row container. This issue becomes more prominent when I inclu ...

TinyMCE version 5.x - Stand out with a specific selection in a personalized drop-down navigation bar

In my customized TinyMCE 5.x dropdown menu, there are 3 options that adjust the width of the editor. I am looking for a way to indicate the currently selected option, but I am unable to interact with the menu items once they are initialized. It seems like ...

Why isn't my heading showing up as expected in my document?

Can anyone help me understand why my title appears so low on certain screens? I've previously tried removing the negative margin when the title was hidden, but now it's positioned too far down. Here is the link to my webpage: ...

What methods does the W3C CSS Validator use to recognize a CSS3 document?

Help Needed! Can someone tell me how to specify a CSS3 file for validation by the W3C? In HTML5, we use <!DOCTYPE html>, but what should we use in a CSS file? Whenever I try to validate my CSS file containing CSS3 elements like @font-face and box- ...

The precedence of theme CSS is paramount

Check out this link for Smirnoff Espresso Vodka 70cl (password: hide) Upon inspecting the page source, you'll see that my main stylesheet (main-style) is loaded at the top of the head section with high priority, even above my theme's style. Why ...

What is the best way to format changing text within a paragraph?

If I needed to implement this functionality using JavaScript: ...... if (!isNaN(num)) document.getElementById("isNum").innerHTML = num +" is a number"; ...... Then in HTML : <div> <button onclick="checknum()">Enter any value : </b ...

Excessive delays encountered while loading fonts in a Ruby on Rails project

I've encountered an issue in my Rails project where I am utilizing two fonts from the /assets/fonts directory. @font-face { font-family: FuturaStd-Light; src: url("/assets/fonts/FuturaStd-Light.otf"); } @font-face { font-family: HelveticaNeue; ...

jQuery Refuses to Perform Animation

I'm facing an issue with animating a specific element using jQuery while scrolling down the page. My goal is to change the background color of the element from transparent to black, but so far, my attempts have been unsuccessful. Can someone please pr ...