Back to Blog
Lesson 37 of the Tailwind CSS: Utility-First Styling from Scratch course
CSSAugust 24, 20264 min read

Working with SVGs: Styling and Scaling Icons in Tailwind CSS

Learn to master SVG icons in Tailwind CSS. Discover how to inline SVGs, control fill and stroke colors, and resize icons for a production-ready UI.

Tailwind CSSSVGIconsUI DesignFrontend
A person holds a sticker featuring the React logo, commonly used in web development.

Previously in this course, we explored Transitions and Animations to bring our interface to life. In this lesson, we shift our focus to visual assets by learning how to implement and style SVGs directly within our HTML using Tailwind CSS.

Understanding Inline SVGs

An SVG (Scalable Vector Graphic) is essentially an XML-based markup language for describing two-dimensional vector graphics. Unlike JPEGs or PNGs, SVGs are code. This makes them perfect for web UI because they can scale infinitely without losing quality and, crucially, they can be styled using CSS.

When you "inline" an SVG, you paste the raw <svg> code directly into your HTML file. This allows you to treat the individual elements inside the SVG (like <path> or <circle>) as DOM elements that respond to your Tailwind utility classes.

Styling and Scaling with Tailwind

To control an SVG with Tailwind, you primarily interact with two properties: fill and stroke.

  • Fill: Controls the interior color of a shape.
  • Stroke: Controls the color of the outline (the "path" itself).

Concrete Example: A Search Icon

Let’s take a standard search icon and style it for our marketing site. We will add a color and control its size using standard spacing utilities.

HTML
<!-- An inline SVG icon -->
style="color:#808080"><style="color:#4EC9B0">svg 
  class="w-6 h-6 text-blue-600 fill-current" 
  viewBox="0 0 24 24" 
  xmlns="http://www.w3.org/2000/svg"
>
  style="color:#808080"><style="color:#4EC9B0">path d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
style="color:#808080"></style="color:#4EC9B0">svg>

Key takeaways from this snippet:

  1. w-6 h-6: We use our standard sizing utilities to set the dimensions. Because the SVG has a viewBox, it will scale proportionally.
  2. text-blue-600: Tailwind's text-* utilities are a shortcut for setting the color of the SVG content.
  3. fill-current: This is a vital class. It forces the SVG to inherit the current text color, allowing you to change the icon color simply by changing the parent or the element's text color utility.
  4. stroke="currentColor": By setting the attribute to currentColor, the stroke will also match whatever utility class you provide (e.g., text-gray-500).

Hands-on Exercise: Replacing the Hero Button Icon

In our ongoing marketing site project, locate the "Get Started" button in your hero section.

  1. Find a simple arrow or chevron icon (you can find these on sites like Heroicons).
  2. Paste the raw SVG code inside your button element next to the text.
  3. Apply w-5 h-5 ml-2 to the SVG to give it proper spacing and size.
  4. Use fill-none and stroke-current to ensure the icon adapts to the button's text color on hover.

Common Pitfalls

  • Forgetting fill-current: If your SVG looks like a solid black block, it's likely because it has a default fill attribute. Always check if you need to set fill="none" or use fill-current to let Tailwind manage the color.
  • Over-styling paths: You rarely need to style individual <path> elements. It is almost always better to style the parent <svg> tag. If you need multi-color icons, use two separate paths with different classes, but keep it minimal to avoid DOM bloat.
  • Accessibility: SVGs are invisible to screen readers by default. Always add aria-hidden="true" to your decorative icons so screen readers skip them, or include a <title> tag inside the SVG if the icon conveys specific meaning.

FAQ

Q: Should I use <img> tags or inline SVGs? For UI icons (buttons, navigation, features), always use inline SVGs. It allows you to use Tailwind utilities to change colors on hover or based on dark mode states. Use <img> only for static illustrations.

Q: Can I animate SVGs? Absolutely! Because you have access to the SVG paths, you can apply Tailwind's transition and hover:scale-110 classes directly to the <svg> tag to create interactive effects.

Q: What if my SVG has hardcoded colors? If you copy an SVG that has fill="#000000" hardcoded, Tailwind might struggle to override it. You can either remove the attribute manually or use !fill-current (the "important" modifier) to force the override.

Recap

Working with SVGs in Tailwind is a powerful way to keep your UI consistent. By inlining your code, you gain the ability to resize icons with width/height utilities and change colors dynamically using fill-current and stroke-current. Remember to keep your icons accessible and rely on the parent container for standard sizing.

Up next: We will dive into Accessibility Best Practices to ensure our marketing site is usable for everyone.

Similar Posts