DEV Community

Ariel Lun
Ariel Lun

Posted on

Creating Animations and Transitions with CSS

Animating with CSS can be a fun way to add interest and interactivity to your web pages. CSS animations and transitions can be used to create simple animations, such as a fading in or out effect, or more complex ones, such as an image carousel.
CSS animations are made up of two parts:
a style rule that defines the animation, and a @keyframes rule that defines the animated states. The style rule is applied to an element, and the @keyframes rule is used to specify the animation. To create a simple animation, you can use the transition property in CSS. This property allows you to define how an element should change over time, from one state to another.

For example, let's say you want to animate a button from blue to green when it's hovered over. You could do this with the following CSS:

button {
backgroundcolor: blue;
transition: backgroundcolor 1s;
}
button:hover {
backgroundcolor: green;
}

In this code, we've defined a button with a blue background color. When the button is hovered over, the background color will change to green over the course of 1 second (1000 milliseconds). If we wanted the animation to happen faster or slower, we could adjust the duration accordingly. We can also add other properties to our animation, such as changing the width or height of an element.

@keyframes is a newer way to create animations in CSS. It allows for more control over the intermediate steps in an animation sequence than transition does. For example, let's say we want to create an animation that makes an element grow from 0% width to 100% width over the course of 3 seconds. We could do this with the following @keyframes rule:

@keyframes grow {
0% { width: 0%; }
100% { width: 100%; }
}

Then we could apply this animation to an element like so:

div {
animationname: grow; /* Corresponds to @keyframes rule */
animationduration: 3s; /* Length of animation */
}

Transitioning with CSS

When you hover over an element on a web page, you might see it change color, grow in size, or move to a new location. These are all examples of CSS transitions. Transitions let you change the value of a CSS property over time, giving your elements a smooth, animated change between two states.
CSS transitions are a powerful tool, but they're also easy to use.
First, you specify the CSS property that you want to animate. Next, you specify the duration of the animation. Finally, you specify the 'transition timing function', which controls how the animation will progress over time. The 'transition timing function' can be linear, meaning that the animation will change at a constant rate. Or, the 'transition timing function' can be set to 'ease', meaning that the animation will start slowly and end quickly. There are also other 'transition timing functions' available (e.g. 'easein', 'easeout', and 'easeinout').
Once you've created a CSS transition, you can bind it to an event (like 'hover') using CSS selectors. When the event occurs, the transition will start automatically.
CSS transitions are a great way to create simple animations on your web pages. With just a few lines of code, you can add animations that will engage your users and add visual interest to your site.

In conclusion, CSS is a powerful tool that can be used to create animations and transitions. With a little creativity and some CSS knowledge, you can create some amazing effects.

Star our Github repo and join the discussion in our Discord channel to help us make the BLST website even better!
Test your API for free now at BLST!

Top comments (1)

Collapse
 
codeystein profile image
codeyStein

Heya! Just a quick reminder you can use three back ticks to get better code blocks!

Like this:
Image description

Nice read btw, I have completley lost practise using CSS keyframes haha