Create Icon Buttons with Notification Badges and Tooltips
Build a set of four icon buttons (Messages, Notifications, Settings, Profile) with animated notification badges and hover tooltips. This exercise demonstrates tooltip positioning, badge animations using @keyframes, and the use of CSS data attributes for dynamic content.
Task 1: 📸 View Reference Picture
💡 Important Tip: Tooltips use CSS data attributes and ::before pseudo-elements. Hover over each icon to see the tooltip!
Part A: Tooltip Container Setup
- For
.tooltip-icons, set display to flex - For
.tooltip-icons, set justify-content to center - For
.tooltip-icons, set gap to 40px - For
.tooltip-icons, set padding to 80px 0
Part B: Icon Button Base Styling
- For
.icon-btn, set position to relative - For
.icon-btn, set width to 60px - For
.icon-btn, set height to 60px - For
.icon-btn, set background to linear-gradient(135deg, #667eea 0%, #764ba2 100%) - For
.icon-btn, set border-radius to 50% - For
.icon-btn, set display to flex - For
.icon-btn, set align-items to center - For
.icon-btn, set justify-content to center - For
.icon-btn, set cursor to pointer - For
.icon-btn, set transition to transform 0.3s ease
Part C: Icon Button Hover Effect
- For
.icon-btn:hover, set transform to translateY(-5px) - For
.icon-btn i, set font-size to 24px - For
.icon-btn i, set color to #fff
Part D: Notification Badge Styling
- For
.badge, set position to absolute - For
.badge, set top to -5px - For
.badge, set right to -5px - For
.badge, set width to 22px - For
.badge, set height to 22px - For
.badge, set background to #ff4757 - For
.badge, set border-radius to 50% - For
.badge, set color to #fff - For
.badge, set font-size to 11px - For
.badge, set font-weight to 700 - For
.badge, set display to flex - For
.badge, set align-items to center - For
.badge, set justify-content to center - For
.badge, set animation to pulse 2s infinite
Part E: Pulse Animation
- Create @keyframes pulse with 0% having box-shadow: 0 0 0 0 rgba(255, 71, 87, 0.7)
- At 70%, set box-shadow to 0 0 0 10px rgba(255, 71, 87, 0)
- At 100%, set box-shadow to 0 0 0 0 rgba(255, 71, 87, 0)
Part F: Tooltip Base Styling
- For
.icon-btn::before, set content to attr(data-tooltip) - For
.icon-btn::before, set position to absolute - For
.icon-btn::before, set bottom to 100% - For
.icon-btn::before, set left to 50% - For
.icon-btn::before, set transform to translateX(-50%) translateY(-10px) - For
.icon-btn::before, set background to #2d3748 - For
.icon-btn::before, set color to #fff - For
.icon-btn::before, set padding to 8px 12px - For
.icon-btn::before, set border-radius to 6px - For
.icon-btn::before, set font-size to 13px - For
.icon-btn::before, set white-space to nowrap - For
.icon-btn::before, set opacity to 0 - For
.icon-btn::before, set pointer-events to none - For
.icon-btn::before, set transition to all 0.3s ease
Part G: Tooltip Arrow
- For
.icon-btn::after, set content to "" - For
.icon-btn::after, set position to absolute - For
.icon-btn::after, set bottom to 100% - For
.icon-btn::after, set left to 50% - For
.icon-btn::after, set transform to translateX(-50%) - For
.icon-btn::after, set border-width to 6px - For
.icon-btn::after, set border-style to solid - For
.icon-btn::after, set border-color to #2d3748 transparent transparent transparent - For
.icon-btn::after, set opacity to 0 - For
.icon-btn::after, set transition to all 0.3s ease
Part H: Show Tooltip on Hover
- For
.icon-btn:hover::before, set opacity to 1 - For
.icon-btn:hover::before, set transform to translateX(-50%) translateY(-15px) - For
.icon-btn:hover::after, set opacity to 1
Key Learning Points:
attr(data-tooltip)allows CSS to read HTML data attributes for dynamic content.- Tooltips use
::beforefor the text box and::afterfor the arrow. @keyframescreates reusable animation sequences that can be applied withanimation.- Pulse effect uses expanding box-shadow with decreasing opacity for a ripple effect.
pointer-events: noneprevents tooltips from interfering with mouse interactions.white-space: nowrapkeeps tooltip text on a single line.
Tip: Try adding tooltips in different positions (top, bottom, left, right) by adjusting the positioning properties!