2

I have a button that needs to transition its color both when it's hovered and clicked. When clicked, I add it a class name called .selected.

Setting transition property to the button makes every style transition which is unintentional.

.score-button {
  width: 35px;
  height: 35px;
  border: 1px solid var(--fill-button-text8);
  background-color: transparent;
  border-radius: 23px;
  color: var(--fill-button-text8);
  cursor: pointer;
  font-size: 18px;
  font-weight: 600;
  transition: linear 0.5s;

  &:hover {
    background-color: var(--fill-button-bg-hover);
  }

  &.selected {
    border: none;
    background-color: var(--primary-color);
    color: var(--fill-button-text);
  }
}
Mr.Singh
  • 1,421
  • 6
  • 21
  • 46
Intaek
  • 315
  • 4
  • 15

1 Answers1

1

You can specify a property to transition:

transition: background-color linear 0.5s;

.score-button {
    width: 35px;
    height: 35px;
    border: none;
    border: 1px solid var(--fill-button-text8);
    background-color: transparent;
    border-radius: 23px;
    color: var(--fill-button-text8);
    cursor: pointer;
    font-size: 18px;
    font-weight: 600;
    transition: background-color linear 0.5s;

    &:hover {
      background-color: var(--fill-button-bg-hover);
    }

    &.selected {
      border: none;
      background-color: var(--primary-color);
      color: var(--fill-button-text);
    }
}
Zach Jensz
  • 3,650
  • 5
  • 15
  • 30