What are the ways to leverage data in CSS when working with Vue?

I'm trying to create a customized button component using Vue.
How can I access the --mainColor variable in CSS without resorting to inline styling when receiving main props? Any help would be appreciated.

<script>
import Vue from "vue";

    export default Vue.extend({
    name: "CustomButton",
    props: {
        text: {
            type: String,
            default: ""
        },
        main: {
            type: String,
            default: "#10b981"
        }
    }
});
</script>

<style lang="scss" scoped>
.btn {
    --mainColor: #10b981;
    display: inline-block;
    border: 1px solid var(--mainColor);
    color: var(--mainColor);
    &:hover {
        background-color: var(--mainColor);
        color: #fff;
    }
}
</style>

Answer №1

It may seem unlikely, but you can actually implement conditional logic in Sass using their provided example:

$light-bg: #f2ece4;
$light-txt: #036;
$dark-bg: #6b717f;
$dark-txt: #d2e1dd;

@mixin theme-colors($is-light-theme: true) {
  @if $is-light-theme {
    background-color: $light-bg;
    color: $light-txt;
  } @else {
    background-color: $dark-bg;
    color: $dark-txt;
  }
}

.banner {
  @include theme-colors($is-light-theme: true);
  body.dark & {
    @include theme-colors($is-light-theme: false);
  }
}

Explore more about conditional logic in Sass here!

Answer №2

If you're working with Vue3, styling components is a breeze:

<template>
  <h1>WELCOME</h1>
</template>

<script>
export default {
  data() {
    return {
      h1Color: "blue",
    };
  },
};
</script>
<style>
h1 {
  color: v-bind(h1Color);
}
</style>

For more in-depth guidance, check out this resource

If you're using Vue2, following the method mentioned in Steven B's answer would be a good choice. See the details 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

What is the best way to blend colors in CSS?

Could someone please advise on how to create a box that transitions from silver at the top to white gradually, similar to Apple's homepage background? Appreciate your help as always. ...

Place a paragraph element within the footer section

I'm having trouble aligning the <p> vertically in the footer. I've attempted to use vertical-align:middle; and margin-bottom:10px; for the p element, but it doesn't seem to be working as expected. Code can be found here: http://jsfidd ...

Is it necessary to use both Vue Router and Express when incorporating Firebase into your project?

I am embarking on creating a simple application that will encompass various views, user authentication, data collection, database storage, and backend functionality to process and display the stored information. My chosen technology stack consists of Vue. ...

Joining a variable to an image URL using Vue.js

I recently started learning Vue.js and I'm struggling to figure out how to create an image with a dynamic src file path that includes a variable. Concatenating in Vue.js has been challenging for me. This is the image I currently have: <img :src=" ...

The console.log() function does not accurately display the true value

While looping through a list and using a @click function to update other elements, I have encountered an issue with the index values bound to selectedFixtures. Each time I click on an element, it initially prints out [], and then on subsequent clicks, it ...

Showcase text in a straight row by utilizing ng-repeat and bootstrap

My goal is to show each word on a new line, similar to how words are displayed in a hangman game. The words should be displayed as blanks. <body ng-init="models = [['H','a','p','p','y'],['X',& ...

Creating input fields using Vuejs

Currently, I am learning how to incorporate HTML content rendering in Vuejs. I'm experimenting with creating a small input component that is generated through the render function. Here is a snippet of what I have so far: export default { name: &qu ...

CSS Flexibility in Action

Presently, my tab bar has a fixed look as shown here: https://codepen.io/cdemez/pen/WNrQpWp Including properties like width: 400px; etc... Upon inspecting the code, you'll notice that all the dimensions are static :-( Consequently, I am encountering ...

Automatically expand the input field as text is typed

In my HTML code, users can enter comments for a specific question. If the comments are lengthy, I want the text box to automatically increase in height. Here is my current HTML code: <label class="label-reports">Comments</label> <input @( ...

How to Implement ASP.NET Authorization While Allowing Access to .css Files?

<authentication mode="Forms"> <forms loginUrl="Login.aspx"/> </authentication> <authorization> <deny users="?"/> </authorization> Currently, I am utilizing forms authentication. However, when I incorporate t ...

Tips for arranging vx-cards horizontally in Vuejs

I am trying to arrange the cards side by side by using v-for to fetch the data. Below is my current code: <div v-for="info in infos" v-bind:key="info.id"> <div class="vx-col w-full md:w-1/2 mb-base"> ...

Encountering a problem when attempting to connect to the LinkedIn API

Encountering an issue when attempting to call the LinkedIn API. Specifically, Receiving an error message stating: Access to XMLHttpRequest at 'https://api.linkedin.com/v2/me' from origin 'https://localhost:8085' has been blocked by CO ...

What is the process for adding color to the rows of a table?

Hello, I have a table with various fields that include: I am looking to assign colors to entire rows in the table based on certain criteria. For example, if the ASR value falls between 75 and 100, it should be assigned one color; for values between 50 and ...

What are the best situations to utilize bootstrap and when should you avoid it?

When it comes to web development, there is often a debate on whether to use Bootstrap or custom CSS for designing a website. For my school project, I am tasked with creating a homepage and contact page using Bootstrap primarily. However, we are also requir ...

Tips for creating a header.php and footer.php integrated with CSS files

I am in the process of constructing my website using HTML files. I want to make sure that my header and footer sections are dynamic so I can easily modify them without having to update numerous files each time. While I've attempted a few methods based ...

Adjust the width of a child element to be within the bounds of its parent's width and maximum width

I am looking to create a CSS menu with specific constraints: The height and width of the parent <div> are variable The number of <li> elements is unknown The <ul> must be at least as wide as the <div> Each row of <li> should ...

Tips for concealing a div upon reaching a specific scroll point, even with varying content sizes

Hey there! I am completely new to web programming and currently working on a landing page. The challenge I'm facing is that the content in the first column is dynamic, meaning it can vary in length. I'm looking for a way to hide my call-to-actio ...

What is the most efficient way to dynamically insert a space, break, or div at a specific height?

My goal is to generate a PDF from a React app that includes multiple components. The number of components can vary from 4 to 12, and the height of the components also changes based on content from the store. I am looking for a solution where every 900px ( ...

No content displayed as I adjust the height of a child element

Oftentimes, I find myself wanting to create a clean and simple web page layout using flexbox. Here's an example of what I'm aiming for: https://i.sstatic.net/0xYX5.png Here is the code snippet I have tried so far: .container { height: 10 ...

Sending parameters to dynamic URLs in Vuex and Laravel

Can you provide a method for creating a dynamic axios call within Vuex? Specifically, I'm looking to query data from /api/data/{id}, in which the id corresponds to the user id retrieved from the Laravel database. ...