Ping animation helps show new notifications or alerts in a friendly, eye-catching way.
Ping animation (notifications) in Tailwind
<div class="relative"> <span class="absolute inline-flex h-3 w-3 rounded-full bg-red-400 opacity-75 animate-ping"></span> <span class="relative inline-flex rounded-full h-3 w-3 bg-red-500"></span> </div>
The animate-ping class creates the expanding circle effect.
Use two <span> elements: one for the ping animation and one for the solid dot.
<div class="relative"> <span class="absolute inline-flex h-4 w-4 rounded-full bg-green-400 opacity-75 animate-ping"></span> <span class="relative inline-flex rounded-full h-4 w-4 bg-green-500"></span> </div>
<div class="relative"> <span class="absolute inline-flex h-2 w-2 rounded-full bg-blue-400 opacity-75 animate-ping"></span> <span class="relative inline-flex rounded-full h-2 w-2 bg-blue-500"></span> </div>
<button class="relative p-2"> Notifications <span class="absolute top-0 right-0 inline-flex h-3 w-3 rounded-full bg-red-400 opacity-75 animate-ping"></span> <span class="absolute top-0 right-0 inline-flex rounded-full h-3 w-3 bg-red-500"></span> </button>
This example shows a button labeled "Notifications" with a red ping animation on the top-right corner. The ping circle expands and fades out repeatedly, drawing attention to new alerts.
Accessibility is added with aria-label and role="status" so screen readers announce the new notification.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Ping Animation Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="flex items-center justify-center min-h-screen bg-gray-100"> <button class="relative p-3 bg-white rounded-md shadow-md font-semibold text-gray-800"> Notifications <span class="absolute top-2 right-2 inline-flex h-4 w-4 rounded-full bg-red-400 opacity-75 animate-ping" aria-hidden="true"></span> <span class="absolute top-2 right-2 inline-flex rounded-full h-4 w-4 bg-red-500" aria-label="New notifications" role="status"></span> </button> </body> </html>
Use relative on the container to position the ping elements absolutely inside it.
Keep the solid dot on top so the ping circle appears behind it.
Use aria-label and role="status" for accessibility to announce notifications.
Ping animation uses two overlapping circles: one solid and one expanding with fading.
Tailwind's animate-ping class creates the expanding effect easily.
Position the ping inside a relative container for correct placement.
Practice
animate-ping class in Tailwind CSS do in a notification badge?Solution
Step 1: Understand the purpose of
Theanimate-pinganimate-pingclass in Tailwind creates an animation where an element expands in size and fades out repeatedly.Step 2: Connect to notification badges
This effect is used to simulate a ping or ripple effect around a notification dot, making it visually stand out.Final Answer:
Creates a circle that expands and fades repeatedly to show a ping effect -> Option DQuick Check:
Ping animation = expanding fading circle [OK]
- Thinking it changes color permanently
- Confusing ping with blinking text
- Assuming it adds shadows
Solution
Step 1: Identify the correct Tailwind class for ping animation
The official Tailwind class for ping effect isanimate-ping. Other classes likeanimate-pulseoranimate-spindo different animations.Step 2: Check the element and classes
The element should have width and height (e.g.,w-3 h-3), background color (e.g.,bg-red-500), rounded full for circle, andanimate-pingfor the ping effect.Final Answer:
<span class='w-3 h-3 bg-red-500 rounded-full animate-ping'></span> -> Option BQuick Check:
Use animate-ping with size and color classes [OK]
- Using animate-pulse or animate-spin instead of animate-ping
- Misspelling animate-ping as ping-animation
- Forgetting rounded-full for circle shape
<div class='relative'> <span class='absolute inline-flex h-3 w-3 rounded-full bg-green-400 opacity-75 animate-ping'></span> <span class='relative inline-flex rounded-full h-3 w-3 bg-green-500'></span> </div>
Solution
Step 1: Analyze the structure and classes
The outerdivis relative, so the absolute positionedspanwithanimate-pingwill expand and fade behind the secondspan.Step 2: Understand the animation effect
The firstspanis a green circle with opacity 75% and ping animation, so it expands and fades repeatedly behind the solid green dot.Final Answer:
A small green dot with a larger fading green circle expanding behind it -> Option AQuick Check:
Relative container + absolute ping behind solid dot = expanding fading circle [OK]
- Ignoring relative positioning causing ping to misplace
- Confusing ping with spin or blink animations
- Not noticing opacity for fading effect
<span class='w-3 h-3 bg-red-500 rounded-full animate-ping'></span>
What is the most likely reason?
Solution
Step 1: Check the positioning context
Theanimate-pinganimation uses absolute positioning internally, so the parent must haverelativeto position the ping correctly.Step 2: Verify other options
The classanimate-pingis correct and supported by Tailwind. Size being too big does not hide animation. Misspelling would cause no animation but question states code as is.Final Answer:
The parent container is missingrelativepositioning -> Option CQuick Check:
Parent needs relative for ping to show [OK]
- Forgetting to add relative to parent
- Assuming animate-ping is unsupported
- Changing size thinking it affects animation visibility
Solution
Step 1: Ensure parent container is relative and inline-block
The container must berelativeto position the ping absolutely relative to the bell icon, andinline-blockto size properly.Step 2: Position ping and solid dot at top-right with layering
Use twospanelements: one withanimate-pingand opacity for fading effect behind a solid dot. Both positionedabsolute top-0 right-0.Final Answer:
<div class='relative inline-block'> <svg class='w-6 h-6 text-gray-700'>...bell icon...</svg> <span class='absolute top-0 right-0 inline-flex h-3 w-3 rounded-full bg-red-500 opacity-75 animate-ping'></span> <span class='absolute top-0 right-0 inline-flex h-3 w-3 rounded-full bg-red-600'></span> </div> -> Option AQuick Check:
Relative container + absolute ping + solid dot top-right [OK]
- Missing relative on container causing misplacement
- Using relative instead of absolute on ping span
- Positioning ping at wrong corner
- Not layering ping behind solid dot
Ready to prove it?
Pass the test and this pattern moves unknown
