🇯🇵 日本語 | 🇺🇸 English | 🇪🇸 Español | 🇵🇹 Português | 🇹🇭 ไทย | 🇨🇳 中文

CSS Fade-In Template: Smooth Appearance with @keyframes and opacity

📝 Usage

This template is used when you want to add a fade-in effect to an element.

📘 Explanation

This code uses @keyframes to define the fade-in animation. It gradually changes the opacity from 0 to 1, making the element appear smoothly.

🔹 Partial Code

<style>
  .fade-in-element {
    opacity: 0;
    animation: fadeIn 5s ease-in-out forwards;
  }

  @keyframes fadeIn {
    0% {
      opacity: 0;
    }
    100% {
      opacity: 1;
    }
  }
</style>
  

💻 Full Working Code

<div class="fade-in-element">
  This is a fade-in element.
</div>

<style>
  .fade-in-element {
    opacity: 0;
    animation: fadeIn 5s ease-in-out forwards;
  }

  @keyframes fadeIn {
    0% {
      opacity: 0;
    }
    100% {
      opacity: 1;
    }
  }
</style>
  
Copied!

✨ Practical Techniques for Fade-In Animations

Using @keyframes and opacity for fade-in effects is an excellent way to capture user attention. When applied appropriately, it can dramatically enhance the page load experience.

⏱️ Animation Optimization

Shortening the default 5-second animation to around 1s–2s gives a more natural impression. By adding animation-delay, you can display multiple elements sequentially. For example, setting animation-delay: 0.5s starts the animation with a 0.5-second delay.

🎨 Design Variations

In addition to simple fade-ins, combining transform: translateY(20px) can create an effect where elements slide in from the top. Also, using a custom easing function like cubic-bezier(0.68, -0.55, 0.265, 1.55) instead of ease-in-out can result in more unique movement.

⚠️ Accessibility Considerations

Be mindful of users who may experience dizziness or nausea from motion. Use @media (prefers-reduced-motion: reduce) to offer a no-animation alternative. Also, avoid making critical content reliant solely on animation to appear.

While fade-in effects can enhance user experience, excessive use may become distracting. It's best to limit them to key elements or specific areas you want users to focus on.