Course Content
CSS Basic
0/1
CSS Selectors
0/1
CSS Comments
0/1
CSS Backgrounds
0/1
CSS Borders
0/1
CSS Outline
0/1
CSS Fonts
0/1
CSS Height and Width
0/1
CSS Margins and Paddings
0/2
CSS Icons
0/1
CSS Links
0/1
CSS Lists
0/1
CSS Tables
0/1
CSS Display Properties
0/1
CSS Max-Width Property
0/1
CSS Positioning Elements
0/1
CSS Z-Index Property
0/1
CSS Overflow
0/1
CSS Float
0/1
CSS Opacity
0/1
CSS Forms
0/1
CSS Dropdowns
0/1
CSS Buttons
0/1
CSS Media Queries
0/1
About Lesson

The @keyframes rule in CSS is used to define animations. It allows you to specify the style changes that occur over a series of keyframes during an animation sequence. Each keyframe specifies how the element should look at a certain point in time during the animation.

Syntax:

CSS
@keyframes animation-name {
  keyframe-selector-1 {
    /* CSS properties and values at the first keyframe */
  }
  keyframe-selector-2 {
    /* CSS properties and values at the second keyframe */
  }
  /* More keyframes as needed */
}
  • animation-name: This is a custom name you give to your animation sequence, which you can later reference when applying the animation to an element using the animation property.
  • keyframe-selector: This specifies the percentage of the animation duration (0% to 100%) or keywords (from and to) representing the starting and ending points of the animation.

Here’s a simple example of a @keyframes rule defining a basic animation that gradually changes the opacity of an element:

CSS
@keyframes fade-in {
  from {
    opacity: 0; /* Element starts completely transparent */
  }
  to {
    opacity: 1; /* Element becomes fully opaque */
  }
}

In this example:

  • fade-in is the custom name given to the animation sequence.
  • from represents the starting point of the animation (equivalent to 0%).
  • to represents the ending point of the animation (equivalent to 100%).

Once you have defined your @keyframes rule, you can apply it to an element using the animation property in combination with the animation name and other animation-related properties (such as duration, timing function, etc.).

CSS
.element {
  animation: fade-in 1s ease-out;
}

This would apply the fade-in animation to the .element class, with a duration of 1s and an ease-out timing function.

In summary, the @keyframes rule is essential for defining the sequence of style changes that occur during an animation in CSS. It provides the foundation for creating dynamic and visually appealing effects on web pages.

Output: