스튜디오 실행
문서

Dynamic Icon Tinting with CSS SVG Masks

Modern CSS Architecture • Cross-Browser Vector Masking

The primary constraint of CSS background-image is that the icon's color values are baked into the SVG string. When you need an icon to adapt dynamically on :hover, :active, or in dark mode, CSS Masks provide the solution.

1. The Mask Architecture Pattern

A CSS mask uses the SVG shape as a stencil. Setting the element's background-color: currentColor; causes the current text color to fill the stencil shape:

.dynamic-icon {
  display: inline-block;
  width: 20px;
  height: 20px;
  background-color: currentColor;
  
  /* Standard and WebKit Mask Declarations */
  -webkit-mask-image: url('data:image/svg+xml,%3Csvg xmlns=\'http://www.w3.org/2000/svg\' viewBox=\'0 0 24 24\' fill=\'none\' stroke=\'%23000\' stroke-width=\'2\'%3E%3Cpath d=\'M5 12h14M12 5l7 7-7 7\'/%3E%3C/svg%3E');
  mask-image: url('data:image/svg+xml,%3Csvg xmlns=\'http://www.w3.org/2000/svg\' viewBox=\'0 0 24 24\' fill=\'none\' stroke=\'%23000\' stroke-width=\'2\'%3E%3Cpath d=\'M5 12h14M12 5l7 7-7 7\'/%3E%3C/svg%3E');
  
  -webkit-mask-size: contain;
  mask-size: contain;
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
  -webkit-mask-position: center;
  mask-position: center;
}

/* Hover State Color Switching */
.btn:hover .dynamic-icon {
  color: var(--text-primary);
}

2. Browser Compatibility & Prefixes

Modern Safari, Chromium, and Firefox all support standard mask-image. For full mobile WebKit (iOS) compatibility, vendor prefixing (-webkit-mask-image and -webkit-mask-size) remains recommended. SVG2CSS outputs both standard and prefixed declarations.

3. Masking Solid Shapes vs. Outlined Vectors

SVG를 CSS로 변환. 브라우저에서 생성하는 URL 인코딩 Data URI.

SVG 코드를 URL 인코딩 CSS Data URI, background-image 규칙, mask-image 색상 틴팅 및 Tailwind 임의 클래스로 변환합니다. 서버 업로드 없이 브라우저에서 100% 로컬로 안전하게 실행됩니다.

스튜디오 워크스페이스 실행