|
| 1 | +--- |
| 2 | +id: css-animations |
| 3 | +title: CSS Animations |
| 4 | +sidebar_label: CSS Animations |
| 5 | +tags: [html, introduction, web development, markup language, hyper text, web pages, career opportunities, personal growth, web-development, web design, web pages, websites, career opportunities, contribute to the web, stay relevant, express yourself, learn other technologies, have fun, how to use html, steps to start using html, set up your development environment, create your first html document, learn html syntax and structure, explore html elements and-attributes] |
| 6 | +description: In this tutorial you will learn about animations in CSS |
| 7 | +--- |
| 8 | + |
| 9 | +## What are CSS Animations? |
| 10 | +An animation lets an element gradually change from one style to another. |
| 11 | +You can change as many CSS properties you want, as many times as you want. |
| 12 | +To use CSS animation, you must first specify some keyframes for the animation. |
| 13 | + |
| 14 | +Keyframes hold what styles the element will have at certain times. |
| 15 | + |
| 16 | +## The `@keyframes` Rule |
| 17 | +When you specify CSS styles inside the `@keyframes` rule, the animation will gradually change from the current style to the new style at certain times. |
| 18 | +To get an animation to work, you must bind the animation to an element. |
| 19 | + |
| 20 | +```css |
| 21 | +/* The animation code */ |
| 22 | +@keyframes example { |
| 23 | + from {background-color: red;} |
| 24 | + to {background-color: yellow;} |
| 25 | +} |
| 26 | + |
| 27 | +/* The element to apply the animation to */ |
| 28 | +div { |
| 29 | + width: 100px; |
| 30 | + height: 100px; |
| 31 | + background-color: red; |
| 32 | + animation-name: example; |
| 33 | + animation-duration: 4s; |
| 34 | +} |
| 35 | +``` |
| 36 | +<BrowserWindow url="http://127.0.0.1:5500/index.html"> |
| 37 | + <div style={{ |
| 38 | + width: '100px', |
| 39 | + height: '100px', |
| 40 | + backgroundColor: 'red', |
| 41 | + animation: 'myAnimation 4s infinite', |
| 42 | + padding: '5px', |
| 43 | + }}> |
| 44 | + Hello World! |
| 45 | + </div> |
| 46 | + <style>{` |
| 47 | + @keyframes myAnimation { |
| 48 | + from { background-color: red; } |
| 49 | + to { background-color: yellow; } |
| 50 | + } |
| 51 | + `}</style> |
| 52 | + </BrowserWindow> |
| 53 | + |
| 54 | +The `animation-duration` property defines how long an animation should take to complete. If the `animation-duration` property is not specified, no animation will occur, because the default value is 0s (0 seconds). |
| 55 | +In the example above we have specified when the style will change by using the keywords "from" and "to" (which represents `0%` (start) and `100%` (complete)). |
| 56 | +It is also possible to use percent. By using percent, you can add as many style changes as you like. |
| 57 | + |
| 58 | +## Delay an Animation |
| 59 | +The `animation-delay` property specifies a delay for the start of an animation. |
| 60 | + |
| 61 | +```css |
| 62 | +div { |
| 63 | + width: 100px; |
| 64 | + height: 100px; |
| 65 | + position: relative; |
| 66 | + background-color: red; |
| 67 | + animation-name: example; |
| 68 | + animation-duration: 4s; |
| 69 | + animation-delay: 2s; |
| 70 | +} |
| 71 | +``` |
| 72 | +<BrowserWindow minHeight={400} url="http://127.0.0.1:5500/index.html"> |
| 73 | + <div className="Delay-an-Animation" style={{ |
| 74 | + width: '100px', |
| 75 | + height: '100px', |
| 76 | + position: 'relative', |
| 77 | + backgroundColor: 'red', |
| 78 | + animationName: 'DelayAnAnimationExample', |
| 79 | + animationDuration: '4s', |
| 80 | + animationDelay: '2s', |
| 81 | + padding: '5px', |
| 82 | + }}> |
| 83 | + Hello World! |
| 84 | + </div> |
| 85 | + <style>{` |
| 86 | + @keyframes DelayAnAnimationExample { |
| 87 | + 0% { background-color: red; left: 0px; top: 0px; } |
| 88 | + 25% { background-color: yellow; left: 200px; top: 0px; } |
| 89 | + 50% { background-color: blue; left: 200px; top: 200px; } |
| 90 | + 75% { background-color: green; left: 0px; top: 200px; } |
| 91 | + 100% { background-color: red; left: 0px; top: 0px; } |
| 92 | + } |
| 93 | + .Delay-an-Animation { |
| 94 | + position: absolute; |
| 95 | + } |
| 96 | + `}</style> |
| 97 | + </BrowserWindow> |
| 98 | + |
| 99 | +# Times animation should run |
| 100 | +The `animation-iteration-count` property specifies the number of times an animation should run. |
| 101 | + |
| 102 | +```css |
| 103 | +div { |
| 104 | + width: 100px; |
| 105 | + height: 100px; |
| 106 | + position: relative; |
| 107 | + background-color: red; |
| 108 | + animation-name: example; |
| 109 | + animation-duration: 4s; |
| 110 | + animation-iteration-count: 3; |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +<BrowserWindow minHeight={400} url="http://127.0.0.1:5500/index.html"> |
| 115 | + <div style={{ |
| 116 | + width: '100px', |
| 117 | + height: '100px', |
| 118 | + position: 'relative', |
| 119 | + backgroundColor: 'red', |
| 120 | + animationName: 'example', |
| 121 | + animationDuration: '4s', |
| 122 | + animationIterationCount: 3, |
| 123 | + padding: '5px', |
| 124 | + }}> |
| 125 | + Hello World! |
| 126 | + </div> |
| 127 | + <style>{` |
| 128 | + @keyframes example { |
| 129 | + 0% { background-color: red; left: 0px; top: 0px; } |
| 130 | + 25% { background-color: yellow; left: 200px; top: 0px; } |
| 131 | + 50% { background-color: blue; left: 200px; top: 200px; } |
| 132 | + 75% { background-color: green; left: 0px; top: 200px; } |
| 133 | + 100% { background-color: red; left: 0px; top: 0px; } |
| 134 | + } |
| 135 | + `}</style> |
| 136 | + </BrowserWindow> |
| 137 | + |
| 138 | +# Direction of the animation |
| 139 | + |
| 140 | +The `animation-direction` property specifies whether an animation should be played forwards, backwards or in alternate cycles. |
| 141 | + |
| 142 | +The `animation-direction` property can have the following values: |
| 143 | + |
| 144 | +- `normal` - The animation is played as normal (forwards). This is default |
| 145 | +- `reverse` - The animation is played in reverse direction (backwards) |
| 146 | +- `alternate` - The animation is played forwards first, then backwards |
| 147 | +- `alternate-reverse` - The animation is played backwards first, then forwards |
| 148 | + |
| 149 | +```css |
| 150 | +div { |
| 151 | + width: 100px; |
| 152 | + height: 100px; |
| 153 | + position: relative; |
| 154 | + background-color: red; |
| 155 | + animation-name: example; |
| 156 | + animation-duration: 4s; |
| 157 | + animation-direction: reverse; |
| 158 | +} |
| 159 | +``` |
| 160 | +<BrowserWindow minHeight={400} url="http://127.0.0.1:5500/index.html"> |
| 161 | + <div style={{ |
| 162 | + width: '100px', |
| 163 | + height: '100px', |
| 164 | + position: 'relative', |
| 165 | + backgroundColor: 'red', |
| 166 | + animationName: 'example', |
| 167 | + animationDuration: '4s', |
| 168 | + animationDirection: 'reverse', |
| 169 | + padding: '5px', |
| 170 | + }}> |
| 171 | + Hello World! |
| 172 | + </div> |
| 173 | + <style>{` |
| 174 | + @keyframes example { |
| 175 | + 0% { background-color: red; left: 0px; top: 0px; } |
| 176 | + 25% { background-color: yellow; left: 200px; top: 0px; } |
| 177 | + 50% { background-color: blue; left: 200px; top: 200px; } |
| 178 | + 75% { background-color: green; left: 0px; top: 200px; } |
| 179 | + 100% { background-color: red; left: 0px; top: 0px; } |
| 180 | + } |
| 181 | + `}</style> |
| 182 | + </BrowserWindow> |
| 183 | + |
| 184 | + |
| 185 | + |
0 commit comments