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

【CSS】Animation Implementation Using span Elements

While span elements are inline elements, combining them with CSS animations allows you to add dynamic effects to portions of text. This article explains how to implement various animation effects on span elements using display: inline-block and @keyframes.

This text is animated

Characteristics of span Animations

  • Partial text decoration: Can apply effects to specific words within paragraphs
  • Lightweight implementation: Achievable with CSS alone, no JavaScript required
  • High versatility: Usable in various scenarios like buttons, labels, and warning displays

🔹 Basic Code

<span class="highlight">Animated text</span>

<style>
.highlight {
  animation: highlight 2s infinite;
  display: inline-block; /* Makes animation possible */
}

@keyframes highlight {
  0% { background-color: yellow; }
  50% { background-color: orange; transform: scale(1.05); }
  100% { background-color: yellow; }
}
</style>

🧩 Animation Customization

By modifying @keyframes rules, you can create various animation effects. While span elements are normally inline elements, setting display: inline-block enables animation of properties like width, height, and margins.

Common Animation Patterns

/* Rotation animation - ideal for icons */
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* Fade-in - for elements you want to highlight */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Bounce - for call-to-action buttons */
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}

Animation Configuration Points

  • animation-iteration-count: infinite for endless loops, or a number for specific iterations
  • animation-timing-function: ease, linear, ease-in-out etc. to change motion quality
  • animation-delay: Delays animation start

🎯 Practical Use Cases and Considerations

Effective Usage Scenarios

1. Emphasizing Important Keywords

Contract key clauses or price displays that users must read

2. Operation Guides

Indicating required fields or input examples in forms

3. Status Displays

Badge expressions like "New" or "Limited"

Important Considerations

✗ Overusing Animations

Applying animations to too many elements simultaneously may confuse users or make important content less noticeable.

✓ Accessibility Considerations

For users sensitive to motion, provide an option to disable animations using @media (prefers-reduced-motion: reduce).

✗ Performance Impact

Animating heavy properties like box-shadow or filter may cause performance issues on mobile devices.

💻 Complete Working Code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>span Animation Demo</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      padding: 20px;
      text-align: center;
    }
    .highlight {
      animation: highlight 2s infinite;
      padding: 0.2rem 0.4rem;
      border-radius: 4px;
      display: inline-block;
    }
    @keyframes highlight {
      0% { background-color: #ffeb3b; }
      50% { background-color: #ff9800; transform: scale(1.05); }
      100% { background-color: #ffeb3b; }
    }
  </style>
</head>
<body>
  <p>This <span class="highlight">text</span> is animated</p>
</body>
</html>

💯 Complete Working Code (All Animations + Interactive Effects)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Complete span Animation Demo</title>
  <style>
    body {
      font-family: 'Arial', sans-serif;
      line-height: 1.6;
      padding: 20px;
      max-width: 800px;
      margin: 0 auto;
      background: #f5f5f5;
    }
    .demo-section {
      background: white;
      border-radius: 8px;
      padding: 20px;
      margin: 20px 0;
      box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    }
    h2 {
      color: #333;
      border-bottom: 2px solid #eee;
      padding-bottom: 10px;
    }

    /* Basic highlight animation */
    .highlight {
      animation: highlight 2s infinite;
      padding: 0.2rem 0.4rem;
      border-radius: 4px;
      display: inline-block;
    }
    @keyframes highlight {
      0% { background-color: #ffeb3b; }
      50% { background-color: #ff9800; transform: scale(1.05); }
      100% { background-color: #ffeb3b; }
    }

    /* Rotation animation */
    .spin {
      animation: spin 3s linear infinite;
      display: inline-block;
      font-weight: bold;
      color: #2196F3;
    }
    @keyframes spin {
      from { transform: rotate(0deg); }
      to { transform: rotate(360deg); }
    }

    /* Fade-in animation */
    .fade-in {
      animation: fadeIn 2s ease-in-out infinite alternate;
    }
    @keyframes fadeIn {
      from { opacity: 0.3; }
      to { opacity: 1; }
    }

    /* Bounce animation */
    .bounce {
      animation: bounce 1.5s ease infinite;
      display: inline-block;
      color: #4CAF50;
      font-weight: bold;
    }
    @keyframes bounce {
      0%, 100% { transform: translateY(0); }
      50% { transform: translateY(-10px); }
    }

    /* Pulse animation (practical example) */
    .pulse {
      animation: pulse 1.5s infinite;
      color: #e91e63;
      font-weight: bold;
      padding: 0 5px;
    }
    @keyframes pulse {
      0% { transform: scale(1); }
      50% { transform: scale(1.1); }
      100% { transform: scale(1); }
    }

    /* Warning display */
    .warning {
      animation: warning 1s infinite alternate;
      background: #ff5722;
      color: white;
      padding: 2px 5px;
      border-radius: 3px;
    }
    @keyframes warning {
      from { background: #ff5722; }
      to { background: #ff9800; }
    }

    /* Interactive hover effect */
    .hover-effect {
      display: inline-block;
      padding: 5px 15px;
      background: #4285f4;
      color: white;
      border-radius: 20px;
      cursor: pointer;
      transition: all 0.3s ease;
      position: relative;
      margin-left: 10px;
    }
    .hover-effect:hover {
      background: #3367d6;
      transform: translateY(-3px);
      box-shadow: 0 5px 15px rgba(0,0,0,0.2);
    }
    .hover-effect:hover::after {
      content: "Click me!";
      position: absolute;
      top: -40px;
      left: 50%;
      transform: translateX(-50%);
      background: #333;
      color: white;
      padding: 5px 10px;
      border-radius: 4px;
      font-size: 12px;
      white-space: nowrap;
      animation: fadeIn 0.3s ease-out;
    }
    .hover-effect:active {
      transform: translateY(-1px) scale(0.98);
    }
  </style>
</head>
<body>
  <div class="demo-section">
    <h2>Basic Highlight Animation</h2>
    <p>This <span class="highlight">text</span> has background color change and scale animation</p>
  </div>

  <div class="demo-section">
    <h2>Custom Animation Examples</h2>
    
    <h3>Rotation Animation</h3>
    <p><span class="spin">⚙️ Settings</span> icon is rotating</p>
    
    <h3>Fade-in Animation</h3>
    <p>Important <span class="fade-in">message</span> appears slowly</p>
    
    <h3>Bounce Animation</h3>
    <p>Please <span class="bounce">click here</span>!</p>
  </div>

  <div class="demo-section">
    <h2>Practical Use Cases</h2>
    
    <h3>Limited Offer Highlight</h3>
    <p>Use the <span class="pulse">limited offer</span> now!</p>
    
    <h3>Warning Display</h3>
    <p><span class="warning">⚠️ Important</span>: Today is the application deadline!</p>
    
    <h3>Interactive Element (Improved)</h3>
    <p>Hover over <span class="hover-effect">👉 here</span></p>
    <ul>
      <li>Button floats up and shows tooltip on hover</li>
      <li>Click produces press-down effect</li>
      <li>Smooth transitions</li>
    </ul>
  </div>
</body>
</html>
Copied!