W3Newbie
  • HTML Tutorial
  • CSS Tutorial
  • Web Dev
    • VS Code Tutorial
    • Command Line Tutorial
    • Git Tutorial
    • Github Tutorial
    • Sass Tutorial
  • Courses
  • Resources
  • Tutorials
  • Click to open the search input field Click to open the search input field Search
  • Menu Menu

CSS Animations

CSS animations bring elements on your web pages to life, creating visual motion and captivating user experiences. They allow you to seamlessly transition between different styles and states, adding dynamic effects without relying on JavaScript or external libraries.

Here’s how CSS animations work:

Key CSS Animation Concepts:

  • @keyframes: This rule defines the animation sequence, specifying keyframes (waypoints) that mark changes in styles over time.
  • animation-name: Assigns a name to the animation, linking it to the defined keyframes.
  • animation-duration: Sets the duration of the animation in seconds or milliseconds.
  • animation-timing-function: Controls the pace of the animation (e.g., ease, linear, ease-in-out).
  • animation-delay: Specifies a delay before the animation starts.
  • animation-iteration-count: Determines how many times the animation repeats (e.g., infinite, 2).
  • animation-direction: Controls whether the animation plays forwards, backwards, or alternates.
  • animation-fill-mode: Specifies the style of the element when the animation isn’t running (e.g., forwards, backwards, both).

CSS Animations Examples with HTML:

1. Simple Fade-In Animation:

HTML:

<div class="fade-in">This text fades in.</div>

CSS:

.fade-in {
  opacity: 0; /* Initially invisible */
  animation: fade-in 2s ease-in-out; /* Animation name, duration, timing function */
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; } /* Transition to full opacity */
}

2. Moving Element Animation:

HTML:

<div class="move-right">This box moves to the right.</div>

CSS:

.move-right {
  animation: move-right 3s linear infinite; /* Infinite loop */
}

@keyframes move-right {
  from { transform: translateX(0); }
  to { transform: translateX(200px); } /* Move 200px to the right */
}

3. Rotating Element Animation:

HTML:

<img src="image.jpg" class="rotate">

CSS:

.rotate {
  animation: rotate 2s linear infinite alternate; /* Rotate back and forth */
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); } /* Rotate 360 degrees */
}

4. Pulsing Animation:

HTML:

<button class="pulse">Click me</button>

CSS:

.pulse {
  animation: pulse 1s infinite; /* Repeat indefinitely */
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); } /* Expand slightly */
  100% { transform: scale(1); } /* Return to original size */
}

Key Points:

  • Use the animation property to apply animations to elements.
  • Define animation keyframes using the @keyframes rule.
  • Control animation duration, timing function, iteration count, and direction.
  • Animate properties like opacity, position, transform, and more.
  • Create visually engaging and interactive experiences without JavaScript.

Best Practices:

  • Consider performance impact, especially for complex animations or those running on less powerful devices.
  • Optimize animation properties for smooth transitions and visual harmony.
  • Ensure accessibility for users with motion sensitivity or visual impairments by providing alternative ways to access content or disabling animations if necessary.
< Previous
Next Lesson >
  • CSS Tutorial
  • CSS Introduction
  • CSS Syntax
  • CSS Style Sheets
  • CSS Comments
  • CSS Fonts
  • CSS Sizing
  • CSS Colors
  • CSS Backgrounds
  • CSS Borders
  • CSS Outlines
  • CSS Margin
  • CSS Padding
  • CSS Box Model
  • CSS Floats
  • CSS Alignment
  • CSS Positioning
  • CSS Overflow
  • CSS Z-Index
  • CSS Opacity
  • CSS Pseudo-Classes
  • CSS Pseudo-Elements
  • CSS Buttons
  • CSS Icons
  • CSS Media Queries
  • CSS Responsive Web Design
  • CSS Navigations
  • CSS Animations
  • CSS Flexbox
  • CSS Grid
  • CSS Responsive Typography
  • CSS Mobile-First Responsive
  • CSS Fluid Layouts
  • CSS Variables

The Newbie Template Bundle

The 12 Website Bundle Pack

Subscribe on YouTube

Web Development Courses

The HTML Email Mastery Course - Build Responsive HTML Email Templates.

Latest Tutorial Posts

  • The HTML Email Mastery Course - Build Responsive HTML Email Templates.w3newbie.com
    HTML Email Mastery Course CouponOctober 12, 2020 - 8:10 pm
  • Create A 5 Page Website With PHP Includes, HTML5, CSS3 & Bootstrap 4w3newbie.com
    Create A 5 Page Website With PHP Includes, HTML5, CSS3 & Bootstrap 4April 22, 2019 - 1:48 pm
© w3newbie.com, 2026. Terms of Use. Privacy Policy.
Scroll to top Scroll to top Scroll to top