user9996891
user9996891

Reputation:

How to change/override the default color of a switch label in vuetify?

As you can see from the offical docs of vuetify, the labels for switches have their own pre defined color. How can i override them to get black text? I am passing the switch as a prop from a global component called form structure into another component that i have named "Primary"

https://vuetifyjs.com/en/components/selection-controls

<v-switch v-if="externalSwitch" model="switch2":label="externalSwitchLabel"> 
</v-switch>

<v-layout v-for="info in information" :key="info.title">
  <v-flex>
    <form-structure :externalSwitchLabel="`${info.title}`" 
      :externalSwitch="true" :hasSubTitle="true" :subTitle="`${info.status}`" 
      :script="`${info.script}`">
    </form-structure>
  </v-flex>
</v-layout>

My array looks like this:

information : [
  {title: "Something1", status:"active", script: "Hello"},
  {title: "Something2", status:"in Progress", script: "Ciao" }
]

My Css looks like this:

<style scoped>
.v-label.theme--light {
  color: black
}
</style>

Upvotes: 6

Views: 23637

Answers (7)

xjestoyou
xjestoyou

Reputation: 23

I stumbled upon this problem with vuetify for vue2 a while ago. Somewhere I found a solution, probably on stackOverflow. What helped me was using this:

  <style scoped>   
  .v-input--checkbox >>> label {
      color: black ;
      /* background-color: orange; */
  } 
  </style>

When upgrading the project to vue3, using vuetify for vue3, the problem came back. I found the solution for this, the important thing here is to reset the opacity to 100.

  <style scoped>   
  .v-checkbox :deep(label) {
      /* color: black ; */
      /* background-color: orange; */
      opacity: 100;   
  } 
  </style>

Before any of you ask: No, I don't know what the >>> means, I just know it works.

Upvotes: 2

Bennie van Eeden
Bennie van Eeden

Reputation: 428

My solution for Vuetify v3

Update the default color of the parent and thumb:

.v-selection-control__wrapper,.v-switch__thumb {
  color: deeppink;
}

And keep using color for the active-color

v-switch off/on

Codepen

note: the css is not scoped, but you can add more specific selectors to be sure only your specific switch is affected

Upvotes: 1

Jason
Jason

Reputation: 1141

Try this.

.v-input--is-label-active label {
    color: red !important;
}

Upvotes: 0

ABucin
ABucin

Reputation: 956

I tried the slot approach and it worked for me:

<v-switch>
   <template v-slot:label>
      <span class="input__label">Label text</span>
   </template>
</v-switch>

CSS looks like this:

.input__label {
   color: black;
}

Upvotes: 7

Onwuka Gideon
Onwuka Gideon

Reputation: 411

If you don't want to override the default style for the label, you can pass in a slot to v-switch with your desired styled element.

Example:

<v-switch v-model="enableScreenshot" label="Enable Screenshot">
   <template v-slot:label>
      <span class="v-label-white">Enable Screenshot</span>
   </template>
</v-switch>

Then your style:

.v-label-white {
  color: white;
  font-weight: bolder
}

Upvotes: 1

Aramillo
Aramillo

Reputation: 3216

You only need to remove scoped from style:

<style>
.v-label.theme--light{
  color: black
}
</style>

This means that the style will be applied globally in the application.

Upvotes: 0

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

You could use color prop by giving it rgb or hexadecimal value as follows :

new Vue({
  el: '#app',
  data () {
    return {
   
      switch1: true,
      switch2: true
    }
  }
})
.v-input__slot .v-label{
color: black!important
}
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css">
<div id="app">
  <v-app id="inspire">
    <v-container fluid px-0>
     
      <v-switch
        :label="`Switch 1: ${switch1.toString()}`"
        v-model="switch1"
                color="#f45525"
      ></v-switch>
       <v-switch
        :label="`Switch 2: ${switch2.toString()}`"
        v-model="switch2"
                color="rgb(0,150,45)"
      ></v-switch>
    </v-container>
  </v-app>
</div>

Upvotes: 1

Related Questions