You can add animation to a website using several methods, most commonly with CSS, JavaScript, or dedicated animation libraries. To use CSS animation, you must first specify some keyframes for the animation. These keyframes define the styles an element will have at different points in time during the animation sequence.
Adding Animation with CSS
CSS animations are a powerful way to add visual flair to your website elements. According to the reference, an animation allows an element to gradually change from one style to another. You have the flexibility to change as many CSS properties as you need, multiple times throughout the animation.
The core components of a CSS animation are:
- Keyframes (
@keyframes
rule): This is where you define the animation sequence. To use CSS animation, you must first specify some keyframes for the animation. Keyframes hold what styles the element will have at certain times (e.g., at the start (0%
orfrom
), at the end (100%
orto
), or at various intermediate points (50%
)). - Animation Properties: These properties are applied to the HTML element you want to animate, linking it to the defined keyframes and controlling the animation's timing, duration, iteration count, and more.
How CSS Animations Work
You define a set of styles within @keyframes
at specific percentage points (or from
/to
), and the browser smoothly transitions the element's styles between these points over the specified duration.
Example:
Let's create a simple animation that moves a box and changes its color.
/* Define the animation sequence */
@keyframes slidein {
0% {
transform: translateX(0%);
background-color: blue;
}
50% {
transform: translateX(200%);
background-color: green;
}
100% {
transform: translateX(0%);
background-color: blue;
}
}
/* Apply the animation to an element */
.animated-box {
width: 100px;
height: 100px;
background-color: blue;
animation-name: slidein; /* Name of the keyframes rule */
animation-duration: 3s; /* How long the animation takes */
animation-iteration-count: infinite; /* How many times it repeats */
animation-timing-function: ease-in-out; /* Speed curve of the animation */
}
In this example:
@keyframes slidein
defines the animation named "slidein".- At
0%
(start), the box is at its original position and is blue. - At
50%
(midpoint), it's moved 200% horizontally and is green. - At
100%
(end), it's back to its original position and is blue. - The
.animated-box
class applies this "slidein" animation over a duration of 3 seconds, making it repeat infinitely.
Key CSS Animation Properties
Here are some essential CSS properties used to control animations:
Property | Description |
---|---|
animation-name |
Specifies the name of the @keyframes rule to use. |
animation-duration |
Specifies how long the animation should take to complete one cycle. |
animation-timing-function |
Specifies the speed curve of the animation (e.g., ease , linear , ease-in-out ). |
animation-delay |
Specifies a delay before the animation starts. |
animation-iteration-count |
Specifies how many times the animation should be played (e.g., 1 , infinite ). |
animation-direction |
Specifies whether the animation should play forwards, backwards, or alternate. |
animation-fill-mode |
Specifies the style applied to the element when the animation is not playing (e.g., forwards , backwards , both ). |
animation |
Shorthand property for setting all animation properties. |
CSS animations are excellent for simpler effects like fades, slides, rotations, and color changes, offering good performance as they are often handled by the browser's rendering engine.
Other Animation Methods
While CSS animations are fundamental, especially regarding keyframes as mentioned in the reference, other methods offer different capabilities:
- JavaScript Animations: For more complex animations, sequenced effects, or animations based on user interaction or data, JavaScript provides greater control. You can manually change styles over time using timers (
setTimeout
,requestAnimationFrame
) or leverage libraries. - Animation Libraries: Libraries like GreenSock (GSAP), Anime.js, or specialized libraries for scroll-based animations offer pre-built effects, easier syntax for complex timelines, and better cross-browser consistency compared to manual JavaScript.
Choosing the right method depends on the complexity of the animation, performance requirements, and the level of control needed. However, understanding the concept of keyframes, as used in CSS animations, is a fundamental step in creating timed style changes on the web.