Learn CSS

"If you want to learn how to style and design web pages, you need to master CSS. CSS stands for Cascading Style Sheets, and it is the language that defines the appearance and layout of HTML elements. In this course, you will learn the basics of CSS, such as selectors, properties, values, units, and colors. You will also learn how to use CSS to create responsive web design, animations, transitions, and more. By the end of this course, you will be able to create beautiful and functional web pages using CSS."

CSS Animations

CSS Animations are a powerful way to add motion and interactivity to web pages. Animations allow designers to create dynamic and engaging user experiences, and can be used to enhance the usability, visual appeal, and overall effectiveness of a website.

CSS Animations are created using a combination of CSS properties, keyframes, and timing functions. With CSS Animations, designers can control the speed, duration, and direction of an animation, as well as the starting and ending points of the animation.

CSS animations are defined using the @keyframes rule, which defines the keyframes of the animation. Keyframes are defined using a percentage value that corresponds to the point in the animation timeline at which the keyframe occurs. For example, a keyframe defined with a percentage of 0% would occur at the beginning of the animation, while a keyframe defined with a percentage of 100% would occur at the end.

To apply an animation to an element, you use the animation property. This property allows you to specify the name of the animation, the duration of the animation, the timing function to use, and whether the animation should repeat.

 

Syntax

Here is the basic syntax for creating a CSS Animation:


/* Define the animation */
@keyframes animation-name {
 0% {
   /* Define starting CSS properties */
 }
 100% {
   /* Define ending CSS properties */
 }
}

/* Apply the animation to an element */
.element {
 animation-name: animation-name;
 animation-duration: 2s;
 animation-timing-function: ease;
 animation-delay: 0s;
 animation-iteration-count: infinite;
 animation-direction: alternate;
 animation-fill-mode: forwards;
}


Let's break down each of the CSS properties used in the animation definition and application:

  • @keyframes: This rule specifies the name of the animation and the keyframes to be used in the animation.
  • animation-name: This property specifies the name of the animation defined in the @keyframes rule.
  • animation-duration: This property specifies the length of time it takes for the animation to complete one cycle.
  • animation-timing-function: This property specifies the speed curve of the animation.
  • animation-delay: This property specifies the amount of time to wait before starting the animation.
  • animation-iteration-count: This property specifies the number of times the animation should be played.
  • animation-direction: This property specifies the direction of the animation (forwards, backwards, alternate, or alternate-reverse).
  • animation-fill-mode: This property specifies what happens to the element after the animation ends (forwards, backwards, both, or none).

 

Shorthand Syntax

The shorthand syntax for CSS animations allows you to define multiple animation properties in a single declaration. The syntax is as follows:


animation: name duration timing-function delay iteration-count direction fill-mode;


Here is a brief description of each property:

  • name: Specifies the name of the animation you want to use.
  • duration: Specifies the length of time that the animation takes to complete, measured in seconds (s) or milliseconds (ms).
  • timing-function: Specifies the speed curve of the animation, which can be one of the following values: linear, ease, ease-in, ease-out, ease-in-out, step-start, step-end, or a cubic-bezier value.
  • delay: Specifies a delay before the animation starts, measured in seconds (s) or milliseconds (ms).
  • iteration-count: Specifies the number of times the animation should be repeated. The value can be a number, infinite, or alternate (which reverses the direction of the animation on every other cycle).
  • direction: Specifies whether the animation should play forwards (normal), backwards (reverse), alternate back and forth (alternate), or alternate back and forth and play in reverse on every other cycle (alternate-reverse).
  • fill-mode: Specifies how the animation applies styles before and after it runs. The value can be none (default), forwards (the target element retains the computed values set by the last keyframe encountered during execution), backwards (the target element gets the property values of the first keyframe encountered during execution), or both (forwards and backwards applied).

 

Examples

Here is an example of a CSS Animation that moves an element from left to right:


/* Define the animation */
@keyframes slide-in {
 0% {
   transform: translateX(-100%);
 }
 100% {
   transform: translateX(0);
 }
}

/* Apply the animation to an element */
.element {
 animation-name: slide-in;
 animation-duration: 2s;
 animation-timing-function: ease;
 animation-delay: 0s;
 animation-iteration-count: infinite;
 animation-direction: alternate;
 animation-fill-mode: forwards;
}


This animation will move the .element from left to right, and will repeat indefinitely.

Here's an example of using the shorthand syntax for CSS animations:


.box {
 animation: slidein 3s ease-out 1s infinite alternate;
}
@keyframes slidein {
 from {
   transform: translateX(-100%);
 }
 
 to {
   transform: translateX(0);
 }
}


In this example, the animation is named slidein, and it has a duration of 3 seconds with an ease-out timing function. The animation starts 1 second after the element is loaded and plays infinitely with an alternate direction.

CSS Animations can also be combined with other CSS properties to create even more dynamic effects. For example, we can use the transition property to smoothly transition between two different animations, or use the transform property to rotate, scale, or skew an element during the animation.

Overall, CSS Animations are a powerful and flexible tool for creating engaging and interactive web experiences, and should be a key part of any web designer's toolkit.