What is preventing my CSS variables from functioning in my Vue component that is utilizing SASS?

Currently, I am working with sass and vue version-3. Within one of my components, the following code is present:

HelloWorld.vue :

<template>
  <div class="greetings">
    <h1>{{ msg }}</h1>
    <h3>
      You’ve successfully created a project with
      <a href="https://vitejs.dev/" target="_blank" rel="noopener">Vite</a> +
      <a href="https://vuejs.org/" target="_blank" rel="noopener">Vue 3</a>.
    </h3>
  </div>
</template>

<script setup>
defineProps({
  msg: {
    type: String,
    required: true
  }
})
</script>

<style scoped lang="scss">
@use "sass:list";
@use "@/assets/sass-folder/variables";
@use "@/assets/sass-folder/main";

h1 {
    color: list.nth(variables.$theme-lighten-1, 2);
}
    
h3 {
  color: var(--blue);
}

</style>

In this component, I included a main.scss file using the @use sass command. However, the css variable --blue is not being recognized within this component. Here is the content of the main.scss file:

main.scss :

:root {
  --blue: #1e90ff;
  --white: #ffffff;
}

//h3 {
//    color: red;
//}

If I uncomment the h3 styles in that file, the color of the h3 tag in my component changes to red, indicating that the file is imported correctly. Can someone please assist me in identifying what part of my code is causing the --blue variable to not be recognized in the HelloWorld.vue component?

Answer №1

Option 1: The issue may lie in how Scss is being utilized

I have no experience working with @use before. If I connect your HelloWorld.vue to your main.scss the way I usually do, your blue variable functions properly. My usual approach involves having a main.ts or main.js file with just a few lines of code:

import { createApp } from "vue";
import App from "./App.vue";
import "./assets/main.scss";

Option 2: The problem might be within the Scss code itself

I suggest trying a combination of local <style scoped> and global <style> styles. Additionally, if that doesn't work, experimenting with a deep selector :deep() in the scoped style could be worth testing out. Lastly, moving the blue and white variables to the variables file instead of main might prove to be effective.

Hopefully one of these options points you in the right direction.

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

Contrasting the utilization of percentage width in relative vs fixed contexts

I am facing an issue with the sizing of two div elements on my page, one with relative positioning and the other with fixed positioning. <div class="outer1"> </div> <div class="outer2"> </div> Here is the CSS: .outer1{ width: ...

Is there a way to create a Swipe slideshow without relying on plugins or external tools?

How can I create a responsive swipe image slideshow using CSS and JQuery that changes the image when swiping from right to left? I want it to work seamlessly on both computer and mobile screens. ...

Bring to life in both directions

I want to incorporate Animista animations to make a button scale in from the left when hovering over a div block, and then scale out to the left when the mouse moves out of the div block. Check out my code snippet below: http://jsfiddle.net/30sfzy6n/3/. ...

Suggestions for improving the smoothness of the Bootstrap toggle hide/show feature

Recently completed my bootstrap toggle hide/show feature and everything seems to be functioning correctly, except for the transition between the different contents. The borders appear jagged and not smooth when activating the toggle. I suspect there may b ...

What is the reason behind the immediate firing of the animate callback function when a CSS transition is present?

I am facing an issue where the callback function fires immediately, disregarding the linear ease type and defaulting to the swing ease in CSS transitions. Is there a way to ensure that the animate function works correctly with the transition? The reason fo ...

Ways to solve the issue of the mobile dropdown menu in Bootstrap

Check Out the Expected Look Here (Image link) See How it Appears on Mobile Devices The dropdown menu causes an increase in the size of the navbar on the mobile version. Below is the code snippet: <div style=" background-image: url(https://i.imgu ...

How to Prevent Scrolling When Modal is in Use on Full Page JS

I am trying to achieve the functionality where when the modal is open, I want to prevent full-page scrolling in JavaScript. The issue arises when I open the modal and try to scroll, it ends up moving the content that's behind the modal, which is actua ...

Tips for creating a CSS transition that reverses direction

Is there a way to create a hover effect where a border appears at the bottom of an element when it is being hovered over, and then retracts back when the mouse leaves the element? I've managed to get the border to appear on hover using CSS, but I&apos ...

Opening up a method or property for external access beyond the Vue framework

I recently discovered in Vue's documentation that it is possible to modify the data of a Vue instance created with new Vue directly from the console. However, as my application grew and I delved deeper into Vue, I encountered issues with this approach ...

How can I specifically target and count children from a certain class using CSS selectors?

Consider this HTML structure with items arranged at the same level: <div class="h2">Title: Colors</div> <div class="pair">Hello world (1)</div> <div class="pair">Hello world (2)</div> <div class="pair">Hello wo ...

I am encountering a TypeError that says "Cannot read properties of undefined (reading '$router')". Can anyone explain what this error means and provide guidance on how to resolve it?

$Happy New Year! I'm currently facing an issue with setting up a redirect after retrieving an authentication token. The console keeps throwing a "TypeError: Cannot read properties of undefined (reading '$router')" error, and the user is not ...

:after pseudo-element along with HTML tags

I have recently designed a basic webpage in an attempt to simulate ajax using CSS. In order to achieve this, I aimed to create hidden radio buttons with visible labels, and load an external CSS file when the user clicks on a label. However, I encountered o ...

Invisible Thinglink image revealed upon resizing browser

I am currently repurposing a pre-existing theme that has a drop-down menu showcasing an image. I am attempting to integrate a Thinglink into the content section of this drop-down, but unfortunately, the image does not appear until I adjust the size of the ...

Exploring the world of computed properties in Vue.js

Currently working on implementing a dynamic category feature in Vue.js My project involves four input fields: Team, Gender, Age, Grade I want the text on the screen to update every time one of these inputs changes, indicating the selected category. Init ...

PHP code isn't showing any posts

Context: I have implemented a PHP loop to generate a dynamic feed of my portfolio posts on my WordPress website. Issue: The first five posts are being displayed correctly, but the subsequent posts are not. I'm uncertain about the underlying cause and ...

navigation bar icons alignment problem

Help with positioning an icon next to the navbar text Example Code: <ul> <li><a href="#" id="other-color" class="ui-btn ui-shadow ui-btn-icon-left ui-custom-icon ui-arrow ui-nodisc-icon">Set Filter</a></li> <li> ...

Exploring the benefits of utilizing Scoped CSS for individual components within Next.js version 13

Switching from a Vue.js background to starting with Next.js, I want to scope my CSS for each component such as Navbar instead of using it inside Globas.css. Is there a way to achieve this? I am also utilizing the Tailwind CSS library. I attempted creatin ...

Determining the visible dimensions of an iframe

Is there a way to determine the visual size inside an iframe using JavaScript or jQuery? In this scenario, only a portion of the iframe is displayed. The iframe itself has dimensions of 300x300, but it is being limited by a div to 300x100. <div style= ...

Adding an image to a server through PHP in the TinyMCE editor

Currently, I am in the process of utilizing TinyMCE text editor for uploading images using PHP and JS database. However, I find myself perplexed when it comes to sending the image to the server. Below is the snippet of the JS code being used: <script ...

Issues with the styling of DIV elements

Currently tackling a project that involves numerous DIVs and sections, encountering an issue with the header. When I attempt to minimize the browser window, the search bar and panes div are not staying within the "header" section as expected. The structur ...