2018-09-30 23:01:58 +01:00
---
id: 587d78a7367417b2b2512ae0
title: Use CSS Animation to Change the Hover State of a Button
challengeType: 0
videoUrl: 'https://scrimba.com/c/cg4vZAa'
2019-08-05 09:17:33 -07:00
forumTopicId: 301073
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
You can use CSS `@keyframes` to change the color of a button in its hover state.
2018-09-30 23:01:58 +01:00
Here's an example of changing the width of an image on hover:
2019-05-14 01:11:58 -07:00
```html
< style >
img:hover {
animation-name: width;
animation-duration: 500ms;
}
@keyframes width {
100% {
width: 40px;
}
}
< / style >
< img src = "https://bit.ly/smallgooglelogo" alt = "Google's Logo" / >
```
2020-11-27 19:02:05 +01:00
# --instructions--
Note that `ms` stands for milliseconds, where 1000ms is equal to 1s.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Use CSS `@keyframes` to change the `background-color` of the `button` element so it becomes `#4791d0` when a user hovers over it. The `@keyframes` rule should only have an entry for `100%` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
The @keyframes rule should use the `animation-name` background-color.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(code.match(/@keyframes \s+?background-color\s*?{/g));
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
There should be one rule under `@keyframes` that changes the `background-color` to `#4791d0` at 100%.
```js
assert(code.match(/100%\s*?{\s*?background-color:\s*?#4791d0 ;\s*?}/gi));
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
```html
< style >
button {
border-radius: 5px;
color: white;
background-color: #0F5897 ;
padding: 5px 10px 8px 10px;
}
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
button:hover {
animation-name: background-color;
animation-duration: 500ms;
}
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
< / style >
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
< button > Register< / button >
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
2019-04-29 01:13:38 +07:00
```html
< style >
button {
border-radius: 5px;
color: white;
background-color: #0F5897 ;
padding: 5px 10px 8px 10px;
}
button:hover {
animation-name: background-color;
animation-duration: 500ms;
}
@keyframes background-color {
100% {
background-color: #4791d0 ;
}
}
< / style >
< button > Register< / button >
2018-09-30 23:01:58 +01:00
```