Not able to set active-class for selected button in Vuetify v-btn-toggle

15.1k Views Asked by At

I am using Vuetify v-button-toggle to toggle between three buttons like below. And each button has different active class. But clicking on a button does not set the active respective active class. What I am missing?

<v-btn-toggle
  v-model="justify"
  borderless
  dense
  group
  class="d-flex flex-column "
>
  <v-btn active-class="dnrSelected" exact>
    <v-icon right class="mr-2">mdi-close</v-icon>
    <span>Do Not Recommend</span>
  </v-btn>
  <v-btn active-class="rSelected">
    <v-icon right class="mr-1">mdi-check</v-icon>
    <span>Recommend</span>
  </v-btn>
  <v-btn active-class="srSelected">
    <v-icon right class="mr-1">mdi-check-all</v-icon>
    <span>Strongly Recommend</span>
  </v-btn>
</v-btn-toggle>

CSS classes

<style scoped>
  .dnrSelected {
    background-color: #e57373;
  }
  .rSelected {
    background-color: #c5e1a5;
  }
  .srSelected {
    background-color: #81c784;
  }
</style>

Update: As expected class is added to the button on click

<button type="button" class="v-btn srSelected v-btn--active v-btn--contained theme--light v-size--default"><span class="v-btn__content"><i aria-hidden="true" class="v-icon notranslate mr-1 v-icon--right mdi mdi-check-all theme--light"></i> <span>Strongly Recommend</span></span></button>

Update: Default Vuetify class below is overriding the custom style

.v-btn-toggle--group > .v-btn.v-btn {
  background-color: transparent !important;
  border-color: transparent;
  margin: 4px;
  min-width: auto;
}

How to override this default style class?

2

There are 2 best solutions below

0
On

One workaround I have found is targeting the elements specifically in the custom CSS by giving the containing element an ID like below

<v-btn-toggle
 id="btnGroup"
 borderless
 dense
 group
 class="d-flex flex-column"
>

And then using this id to specify target elements like below

#btnGroup .dnrSelected {
  background-color: #ef9a9a !important;
}
7
On

I just tried on codepen and it works fine. https://codepen.io/v08i/pen/rNaXyxe

Here is partial code. Full code at codepen.

<div id="app">
  <v-app id="inspire">
    <v-card
      flat
      class="py-12"
    >
      <v-card-text>
        <v-row
          align="center"
          justify="center"
        >
          <v-col cols="12">
            <p class="text-center">Multiple</p>
          </v-col>
          <v-btn-toggle
            v-model="toggle_exclusive"

          >
            <v-btn active-class="aa">
              <v-icon>mdi-format-align-left</v-icon>
            </v-btn>
            <v-btn active-class="bb">
              <v-icon>mdi-format-align-center</v-icon>
            </v-btn>
            <v-btn active-class="cc">
              <v-icon>mdi-format-align-right</v-icon>
            </v-btn>
            <v-btn active-class="dd">
              <v-icon>mdi-format-align-justify</v-icon>
            </v-btn>
          </v-btn-toggle>

          <v-col
            cols="12"
            class="text-center"
          >
            Model: {{ toggle_exclusive }}
          </v-col>
        </v-row>
      </v-card-text>
    </v-card>
  </v-app>
</div>

One suggestion though. I think scoped CSS might be messing because these are vuetify's own components. Can you remove scoped from style and check?