|
| 1 | +# CSS Transitions |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +CSS transitions provide a way to control the speed of animation changes to CSS properties. This can enhance the user experience by making the changes appear smooth and visually appealing. |
| 6 | + |
| 7 | +## Basic Concepts |
| 8 | + |
| 9 | +### What is a Transition? |
| 10 | + |
| 11 | +A transition is a way to animate a property change over a given duration. |
| 12 | + |
| 13 | +```css |
| 14 | +.element { |
| 15 | + transition: property duration timing-function delay; |
| 16 | +} |
| 17 | +``` |
| 18 | +## Transition Properties |
| 19 | + |
| 20 | +### transition-property |
| 21 | + |
| 22 | +Specifies the name of the CSS property the transition effect is for. |
| 23 | +Example: width, height, background-color, etc |
| 24 | + |
| 25 | +```css |
| 26 | +.element { |
| 27 | + transition-property: background-color; |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +### transition-duration |
| 32 | + |
| 33 | +Specifies the duration over which the transition should occur. |
| 34 | +Example: 1s, 200ms, etc |
| 35 | + |
| 36 | +```css |
| 37 | +.element { |
| 38 | + transition-duration: 1s; |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +### transition-timing-function |
| 43 | + |
| 44 | +Specifies the speed curve of the transition effect. |
| 45 | +Example: ease, linear, ease-in, ease-out, ease-in-out, cubic-bezier(n,n,n,n) |
| 46 | + |
| 47 | +```css |
| 48 | +.element { |
| 49 | + transition-timing-function: ease-in-out; |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### transition-delay |
| 54 | + |
| 55 | +Specifies when the transition effect will start. |
| 56 | +Example: 0s, 1s, etc. |
| 57 | + |
| 58 | +```css |
| 59 | +.element { |
| 60 | + transition-delay: 0.5s; |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | + |
| 65 | +## Shorthand Property |
| 66 | + |
| 67 | +You can combine all the individual transition properties into one shorthand property. |
| 68 | + |
| 69 | +```css |
| 70 | +.element { |
| 71 | + transition: background-color 1s ease-in-out 0.5s; |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +## Basic Transition |
| 76 | + |
| 77 | +```html |
| 78 | +<!DOCTYPE html> |
| 79 | +<html lang="en"> |
| 80 | +<head> |
| 81 | + <meta charset="UTF-8"> |
| 82 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 83 | + <style> |
| 84 | + .box { |
| 85 | + width: 100px; |
| 86 | + height: 100px; |
| 87 | + background-color: blue; |
| 88 | + transition: background-color 1s ease; |
| 89 | + } |
| 90 | + .box:hover { |
| 91 | + background-color: red; |
| 92 | + } |
| 93 | + </style> |
| 94 | +</head> |
| 95 | +<body> |
| 96 | + <div class="box"></div> |
| 97 | +</body> |
| 98 | +</html> |
| 99 | +``` |
| 100 | + |
| 101 | +## Transition Multiple Properties |
| 102 | + |
| 103 | +```html |
| 104 | +<!DOCTYPE html> |
| 105 | +<html lang="en"> |
| 106 | +<head> |
| 107 | + <meta charset="UTF-8"> |
| 108 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 109 | + <style> |
| 110 | + .box { |
| 111 | + width: 100px; |
| 112 | + height: 100px; |
| 113 | + background-color: blue; |
| 114 | + margin: 50px; |
| 115 | + transition: width 2s, height 2s, transform 2s; |
| 116 | + } |
| 117 | + .box:hover { |
| 118 | + width: 200px; |
| 119 | + height: 200px; |
| 120 | + transform: rotate(45deg); |
| 121 | + } |
| 122 | + </style> |
| 123 | +</head> |
| 124 | +<body> |
| 125 | + <div class="box"></div> |
| 126 | +</body> |
| 127 | +</html> |
| 128 | +``` |
| 129 | + |
| 130 | +## Delayed Transition |
| 131 | + |
| 132 | +```html |
| 133 | +<!DOCTYPE html> |
| 134 | +<html lang="en"> |
| 135 | +<head> |
| 136 | + <meta charset="UTF-8"> |
| 137 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 138 | + <style> |
| 139 | + .box { |
| 140 | + width: 100px; |
| 141 | + height: 100px; |
| 142 | + background-color: blue; |
| 143 | + transition: background-color 1s ease 2s; /* Delay of 2s */ |
| 144 | + } |
| 145 | + .box:hover { |
| 146 | + background-color: red; |
| 147 | + } |
| 148 | + </style> |
| 149 | +</head> |
| 150 | +<body> |
| 151 | + <div class="box"></div> |
| 152 | +</body> |
| 153 | +</html> |
| 154 | +``` |
| 155 | + |
| 156 | +## Conclusion |
| 157 | +CSS transitions provide a simple way to create smooth animations and improve the user experience. Using the various transition properties, you can control your animations' timing, speed, and behaviour to create visually appealing effects. |
0 commit comments