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.

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:
w-6 h-6: We use our standard sizing utilities to set the dimensions. Because the SVG has aviewBox, it will scale proportionally.text-blue-600: Tailwind'stext-*utilities are a shortcut for setting the color of the SVG content.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.stroke="currentColor": By setting the attribute tocurrentColor, 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.
- Find a simple arrow or chevron icon (you can find these on sites like Heroicons).
- Paste the raw SVG code inside your button element next to the text.
- Apply
w-5 h-5 ml-2to the SVG to give it proper spacing and size. - Use
fill-noneandstroke-currentto 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 defaultfillattribute. Always check if you need to setfill="none"or usefill-currentto 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.
Work with me

Next.js Website & Landing Page Development
A blazing-fast, SEO-optimized website or landing page in Next.js — the kind that loads instantly and ranks. Design-to-code, done right.

Next.js Full-Stack Web App Development
A fast, SEO-ready full-stack web app built with Next.js 16 — from idea to deployed product, by an engineer who ships to production.


