0

I'm trying to create a transition of two different background-color's on hover.

I've looked up different solutions but none of them have worked out so far. I have also attempted using -webkit-transition and transition and neither have worked so far.

a {
  color: inherit;
  text-decoration: none;
  background-color: #ffdc73; /* hsl(200, 100%, 80% */
  box-shadow:
    0 7px 30px -10px #ffdc73, /* This one should usually be only box-shadow but I made it two so it would be more visable */
    0 7px 30px -10px #ffdc73;
  transition: box-shadow 0.3s ease-in-out;
}

a:hover,
a:focus {
  outline: none;
  background-color: #282936;
  color: #fff;
  box-shadow:
    0 7px 30px -10px #282936, /* rgba(154,160,185,0.05) */
    0 7px 30px -10px #282936; /* rgba(166,173,201,0.2) */
  transition: box-shadow 0.3s ease-in-out;
}
<a href="#"> Buttton </a>
Ericgit
  • 6,089
  • 2
  • 42
  • 53

4 Answers4

1

Please note:

Transitions are defined within the actual block not the event block.

e,g a{here...} and a:hover{not here...}

Below is the transition of background-color and box-shadow property of style object.

a {
  color: inherit;
  text-decoration: none;
  background-color: #ffdc73; /* hsl(200, 100%, 80% */
  box-shadow:
    0 7px 30px -10px #ffdc73, /* This one should usually be only box-shadow but I made it two so it would be more visable */
    0 7px 30px -10px #ffdc73;
  transition: 0.3s ease-in-out;
  
  transition-property: box-shadow, background-color;
}

a:hover,
a:focus {
  outline: none;
  background-color: #282936;
  color: #fff;
  box-shadow:
    0 7px 30px 10px #282936, /* rgba(154,160,185,0.05) */
    0 7px 30px 10px #282936; /* rgba(166,173,201,0.2) */
}
<a>Check this out</a>
Mosia Thabo
  • 4,009
  • 1
  • 14
  • 24
0

The box-shadow should be applied to the :hover, while the transition should be applied to the root element:

a {
  color: inherit;
  text-decoration: none;
  background-color: #ffdc73;
  transition: box-shadow 0.3s ease-in-out;
}

a:hover,
a:focus {
  outline: none;
  background-color: #282936;
  color: #fff;
  box-shadow: 0 7px 30px -10px #282936, 0 7px 30px -10px #282936;
}
<a href="#">Hover over me</a>
Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
0

If you want to create a background-color transition, then change box-shadow in a style to background-color

a {
  color: inherit;
  text-decoration: none;
  background-color: #ffdc73; /* hsl(200, 100%, 80% */
  box-shadow:
    0 7px 30px -10px #ffdc73, /* This one should usually be only box-shadow but I made it two so it would be more visable */
    0 7px 30px -10px #ffdc73;
  transition: background-color 0.3s ease-in-out; /* Change transition-property from box-shadow to background-color */
}

a:hover,
a:focus {
  outline: none;
  background-color: #282936;
  color: #fff;
  box-shadow:
    0 7px 30px -10px #282936, /* rgba(154,160,185,0.05) */
    0 7px 30px -10px #282936; /* rgba(166,173,201,0.2) */
}
  • `a` is actually a box with text in it and I have successfully worked out the shadow and box colour transitions thanks to you guys, now is there anyway I can create a transition from the `a` `color` tag to the `a:hover` `color` tag? Edit: Got it worked out, nevermind. –  Aug 07 '19 at 00:53
0

If you are looking to transition the background color, you will need to include that property in you transition.

a {
  color: inherit;
  text-decoration: none;
  background-color: #ffdc73; /* hsl(200, 100%, 80% */
  box-shadow:
    0 7px 30px -10px #ffdc73, /* This one should usually be only box-shadow but I made it two so it would be more visable */
    0 7px 30px -10px #ffdc73;
  transition: 
    background-color 0.3s ease-in-out,
    box-shadow 0.3s ease-in-out;
}

a:hover,
a:focus {
  outline: none;
  background-color: #282936;
  color: #fff;
  box-shadow:
    0 7px 30px -10px #282936, /* rgba(154,160,185,0.05) */
    0 7px 30px -10px #282936; /* rgba(166,173,201,0.2) */
  transition: 
   background-color 0.3s ease-in-out,
   box-shadow 0.3s ease-in-out;
}

https://next.plnkr.co/edit/Udj72JIVEIQdQUbR

Jason B
  • 136
  • 3
  • 6