:root variables not being recognized in SCSS and Vue3 with Vite

I am encountering an issue with setting variables in my root SCSS file, as they are not being recognized correctly.

Within my Vite configuration, I have included the following to import my styling:

css: {
  preprocessorOptions: {
    scss: {
      additionalData: `
        @use "./src/styles/main.scss";
      `
    }
  }
}

Inside my main.scss file, I have defined the following:

:root {
  --test: #ff0000;
}

* {
  background-color: var(--test);
}

Upon inspecting the code output, I notice that it displays '--test is not defined' when hovering over the variable.

Strangely, removing the variable and directly assigning background-color: #ff0000 works without any issues. It appears that the :root variables are not being properly registered.

What might be causing this problem?

Answer №1

In my opinion, the method you are considering for implementing the styling may not be the most optimal approach. To apply a global style to your Vue application, it is recommended to import the CSS either within your App component or in the main.ts file like this:

import './styles/main.scss'.

The rationale behind this is that styles used in the additionalData will be applied universally across all files, resulting in duplication of styles in all generated CSS files.

The vite configuration documentation elaborates on this by stating:

All preprocessor options also support the additionalData option, which can be used to inject extra code for each style content. Note that if you include actual styles and not just variables, those styles will be duplicated in the final bundle.

I believe that by following the aforementioned import method, you can potentially avoid encountering this issue.

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

Connecting Arrays within Arrays in VUE and VUEX: A Comprehensive Guide

In the process of developing a VUE(X) application that involves multiple arrays. state: { triads: [{ people: [], places: [], equipment: [] }] }, I aim to create a user interface with three lists, each containing an input field for adding ...

Tips for concealing overflow x on a responsive website

Could use some help here. Whenever I switch to responsive mode, the overflow x feature gets enabled and I'm at a loss on how to disable it. I've attempted using @media to disable overflow but unfortunately it's not working as expected. ...

Animation using CSS keyframes for simultaneous manipulation of various properties

I am facing a challenge with animating both the bottom and left properties of an object inside a container div simultaneously. How can I make it move diagonally? Despite using keyframes, the following code does not seem to achieve this effect: #containe ...

When I hover over div 1, I am attempting to conceal it and reveal div 2 in its place

I need help with a CSS issue involving hiding one div on mouse hover and showing another instead. I attempted to achieve this using pure CSS, but both divs end up hidden. Would jQuery be necessary for this task? Below is the CSS/HTML code I wrote: .r ...

Enhance the URL with parameters using Vue.js

I am currently working with vuejs3 and I am looking to implement a filter feature. When a user clicks on a specific link, I want to add the selected parameters to the URL and update the browser's address bar initially. Subsequently, I plan to make an ...

Navigate back to the previous route within the Vue router hierarchy

In my Vue application, I have a Settings page with child routes such as settings/user, settings/addUser, etc. I am looking to implement a back button that when pressed, takes the user back to the specific page they visited within the Settings section. Usin ...

What exactly does the "data-effect" attribute refer to?

After downloading a code, I came across the following line: <button data-effect="st-effect-4">Slide along</button> I noticed that "st-effect-4" is a class name in the code, but I am curious to know what exactly this data-effect attribute is u ...

Text in SVG file misaligned at the edge

After creating an SVG with a base64 background image and two text areas for top and bottom texts, I encountered an issue on Internet Explorer and Edge. The problem is that the bottom text is aligned to the left instead of the center, and its position is in ...

Having trouble with parsing the .mjs module from Iconify in Vue 3 CLI project that utilizes the Composition API and TypeScript

Issue Update: After modifying the code from if(data.aliases?.[name2] !== void 0) to: if(data.aliases != null && data.aliases[name2] !== void 0) in the iconify .mjs file, I was able to resolve the error. However, this fix needs to be implemented in ...

Using jQuery to modify a CSS property when a specific option is chosen

I'm currently working on creating an option dropdown menu and I've hit a roadblock trying to display a textbox only when the 'other' option is selected. Here's what I have so far: <span id="con-country"><label for="count ...

Utilizing Vue.js: Disabling button on image carousel when there is no "next" photo accessible

This is my initial experience with Vue. I am attempting to assemble a slideshow using an array of images. I have successfully managed to disable the "previous" button when the user reaches the beginning of the slideshow, but I am encountering difficulties ...

Vue.js <v-data-table> - Automatic sorting/ custom sorting options

I am trying to arrange the numerical data in a Vue.js data-table in descending order right from the start. I want it to look like the screenshot provided below. Screenshot of My Desired Result The data that needs to be arranged in descending order is the ...

Modify the text inside a div based on the navigation of another div

Hey there, experts! I must confess that I am an absolute beginner when it comes to JavaScript, JQuery, AJAX, and all the technical jargon. Despite my best efforts, I'm struggling to grasp the information I've found so far. What I really need is ...

How to center the navigation list vertically with double lines and maintain the href block?

Looking for a solution to align vertically in a basic navigation list bar, particularly when some items have two lines of text. I want the text to be vertically aligned to the middle, while still maintaining the ability to hover and click within the list ...

How can I modify the header size of my cards in Bootstrap-Vue?

How do I adjust the size of my card's header text? <b-card header = "We are certified data installers" md="5" class="card" bg-variant="dark" style="border-color: rgb(235, 100, 33); font-size: 30px;" ...

Issues with connecting static files in express.js

Hey there! I'm having an issue with the redirect not working when I click the "Submit" button on the login page. The code in my index.js file looks like this: app.use(bodyParser.urlencoded({ extended: true })); app.use(express.static(path.join(__dir ...

Vue app setup interrupted by miscellaneous ECONNRESET warning

While setting up my vue app using vue-cli, everything went smoothly until I ran the command npm install to install dependencies. I encountered an error with https, so I switched it to http which resulted in the following log: npm WARN registry Unexpecte ...

CSS :target activates on hover instead of click

I have a slider that is currently functioning well. However, I am facing a small issue. I would like to change the slider image when hovering over one of the <li> elements, instead of clicking on them. Is this achievable? I came across this referenc ...

Tips for Enhancing the Appearance of a List Input Box

While on my hunt for an auto-completing text field, I stumbled across lists. <label>Bad Habit <input list="badhabits" id="habits" name="habit"/> </label> <datalist id="badhabits"> <option value="alcoholics"> <option ...

Tips for ensuring an animation is triggered only after Angular has fully initialized

Within this demonstration, the use of the dashOffset property initiates the animation for the dash-offset. For instance, upon entering a new percentage in the input field, the animation is activated. The code responsible for updating the dashOffset state ...