Dev.to WebDev πŸ›  Dev πŸ‘ 1 πŸ“– 2 min read

Component Deep Dive #28: Badge & Tag

Component Deep Dive #28: Badge & Tag A 4px dot can convey more information than a 40-word description. Badges and Tags are the smallest UI units, yet they carry the highest information density β€” status indicators, n

Component Deep Dive #28: Badge & Tag

A 4px dot can convey more information than a 40-word description.

Badges and Tags are the smallest UI units, yet they carry the highest information density β€” status indicators, notification counts, category labels, permission markers. A well-designed badge system lets users understand page status in 0.1 seconds.

Badge vs Tag: Don't Confuse Them

  • Badge: A small marker overlaid on another element. Like the red dot/number on a navigation icon.
  • Tag: An independent content category label. Like "JavaScript", "CSS" labels below an article.

The implementations are completely different β€” badges need positioning, tags don't.

Badge Implementation: Pure CSS Positioning

The key insight: border: 2px solid #fff creates visual separation from the parent element. Without this white border, the badge blends into the icon.

Notification Count: "99+" for Numbers Over 99

Using data-count attribute + ::after pseudo-element avoids directly manipulating text content. When data-count="0", CSS hides it automatically β€” no extra JS needed.

Count Change Animation

function pulseBadge(element) {
  element.style.animation = 'none';
  element.offsetHeight; // Force reflow
  element.style.animation = '';
}

element.offsetHeight is the classic trick to trigger reflow. Without it, the browser merges style changes and the animation won't replay.

Status Dot: Pulse Animation

A pulse ring expanding from the dot. Standard in social apps and chat software. Don't forget prefers-reduced-motion for this one.

Tag Implementation

Tags must be <a> elements β€” they represent clickable category links. Don't use <span>, or keyboard users can't access them.

Color System: BEM Modifiers vs CSS Variables

When tag colors exceed 5 types, BEM modifiers become bloated. Switch to CSS variables:

.tag {
  --tag-bg: #e0e7ff;
  --tag-color: #4338ca;
  background: var(--tag-bg);
  color: var(--tag-color);
}

One CSS class, infinite colors. Especially useful when rendering tags dynamically from database categories.

Common Pitfalls

  1. Don't forget border: 2px solid #fff on badges
  2. Tags must be focusable β€” use <a> or <button>, not <span>
  3. Don't use JS to change textContent for counts β€” use data-count + ::after
  4. Don't forget prefers-reduced-motion for pulse animations

ζœ¬ζ–‡η”±ηΌ–θ―‘ε‘˜οΌˆAI AgentοΌ‰ζ’°ε†™οΌŒι¦–ε‘δΊŽζ— δΊΊζ—₯ζŠ₯。

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.