Utilizing style binding to define SCSS variables

I am currently working on a Vue component and I want to style it dynamically using properties that are passed into it.

My goal is to adjust my global SCSS variables based on a color passed in as a property to the component.

Although I have created a simplified version of what I am attempting to achieve, the :style attribute doesn't seem to be setting the variable correctly.

Is there a way to make this work or how should I approach this issue?

<template>
  <div id="app">
    <p :style="{ $backgroundColor: 'green' }">{{ message }}</p>
  </div>
</template>

<style>
  $backgroundColor: red;

  p {
    background: $backgroundColor;
  }

</style>

https://jsfiddle.net/50wL7mdz/335379/

Answer №1

Here is a solution that may be beneficial to you:

$customColor: blue !default;
h2 {
  $customColor: orange; // feel free to choose any color
  color: $customColor;
}

Check out the updated demo here

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

Adjusting the alignment of text using the "=" symbol

Can you help me align text with the "=" sign, just like in the image below? https://i.sstatic.net/L53k2.png I know I can achieve this using mathjax, but I'd prefer to do it with CSS. However, my current method isn't producing aligned equal sign ...

Guide on compiling SCSS to CSS when running npm start

When running my application with npm start, I'm unable to compile scss files to css live. Is there a way to achieve live reload for sass to css similar to how ts are compiled to js by the tsc:w utility? Currently, I have to stop the build, then run gu ...

Vertical Graph - Utilizing HTML and Cascading Style Sheets

My goal is to create a vertical bar chart that looks like this: However, the result I am currently getting is different: At the bottom of my vertical bar chart, both lines are connected together and I am trying to figure out how to separate them. I am a ...

Guide to positioning an item at the bottom of the screen using Material UI

Can anyone help me align a button to the bottom of the screen so that it remains in place even when scrolling through a list? I've tried adjusting the code but can't seem to get it right. This is how my current screen appears, with the button al ...

Is your max-height transition not functioning properly?

Is there a way to create a collapsible panel using only CSS similar to the Bootstrap bootstrap collapse panel? Why isn't the max-height transition working for me? How can I toggle the panel if it's checked, and have the panel collapse when d ...

Tips for ensuring that each flexbox element fills up a single line

My goal is to have each child element fill up one line by itself. See this screenshot for reference: https://i.stack.imgur.com/F40Xm.png Below is the code I am currently using: main { display: flex; justify-content: space-around; align-items: ...

Tips for aligning social media icons above an image on a webpage

Image I am seeking advice on how to position three social media icons on top of each picture, aligned to the left but in line with the image. Below is the code snippet I have been working on: body { background-color: #1a1a1a; } #downloadbutton{ width: ...

Colorful background visuals without any text

Can you achieve a background color effect using Stylus without any text? span { width: 5px; height: 5px; } span.red { background-color: #f00; } <span class="red"></span> ...

The like button animation works perfectly for individual posts, but for multiple posts, only the like button on the first post

Having an issue with the like button code on a single news feed. The button works for the first post, but when clicking on other posts, it only affects the first post button. I am using post UI and looping the UI based on the total post count without Javas ...

The Axios.delete encountered a problem while attempting to delete data from the database, showing the error message "Error:

My current tech stack includes Vue 2.3, MongoDB, Node.js, Express, and Handlebars. Initially, I was using handlebars but now I want to switch to Vue. <form class="center" action="/audioannotations/delete/{{_id}}?_method= ...

I encountered an issue where my style.css file was not properly linking to my HTML code. Whenever I saved the file, it would open

I'm a beginner in the world of HTML/CSS. When I save my style.css file, it shows up as a Notepad+ settings file and doesn't seem to work with my HTML. Can anyone advise me on what steps I should take? ...

Characters with accents appear larger compared to regular letters

Currently, I am working on a website that includes special characters like č, ď, ň, ř, š, ž. However, these special letters are appearing larger than the normal letters. For example: https://i.sstatic.net/WBNQg.png I have a .properties file where I ...

Props and theme merge to create uniquely designed styled components with a thematic twist

Out of sheer curiosity, I've been incorporating styled-components into my React application. Within this framework, I make use of the recommended theme to ensure consistency in colors and sizes throughout. Currently, my approach looks something like ...

Utilizing the Power of Graphql with NuxtJs

Exploring the integration of GraphQL with NuxtJs. I am facing a challenge as NuxtJs does not offer an option to have a provider in the root element. How can I properly inject the apolloProvider into the root Vue element? Goal: https://github.com/Akryum ...

What methods can I use to prevent a number from becoming negative?

I am currently developing a game where players collect resources, spend them to create more resources, and engage in various activities. However, I'm facing an issue where the resource count goes into negative numbers when too much of a certain item i ...

Customizing the appearance of a JavaScript countdown timer's output on an individual basis

I am currently working on customizing the appearance of a JavaScript date counter. My goal is to style the days, hours, minutes, and seconds individually. Ideally, I want each unit to be right-aligned within its own div, with specific left and top absolute ...

Ways to customize the background color of child cells in a table

I've implemented material-table in this UI and I'm currently working on customizing the style of specific cells only when the onTreeExpandChange is true. Essentially, my goal is to change the background color for cells 2 & 3. Check out the s ...

FlexNav excluding

I have implemented FlexNav for my website navigation. The demo I used sets the width of top-level menu items to 20%, which works well with five menu items. .flexnav li { . . . width: 20%; } However, in my menu, the text lengths of the top ...

There seems to be an issue with Vue js error message only displaying once. I am currently working on finding a solution to ensure that the error message appears

As a Vue newcomer, I am currently grappling with a specific issue that I need help solving. I am referencing a component in my code: <notification-component v-if="error" :error="error"><notification-component> In one of my ...

What is the reason that styled() does not apply styles to the Material-UI <TextField /> component?

I've managed to figure out a solution, but I'm curious about why this code is working: <TextField label='Zip Code' size='small' InputProps={{ style: { borderRadius: '.4rem' }, }} sx={{ width: & ...