Changing the color of an SVG using Tailwind CSS is straightforward. You can make use of Tailwind's utility classes such as text-[color] to set the color of an SVG icon, assuming the SVG respects the currentColor property for its fill or stroke.
Steps to Change SVG Color with Tailwind CSS
-
Ensure SVG Uses currentColor:
- For Tailwind to control the color of an SVG, the
fill or stroke attributes of the SVG should be set to currentColor. This allows the color to adapt based on the text-[color] class applied to the parent or the SVG itself.
-
Use Tailwind's text-[color]:
- Tailwind uses the
text-[color] utility classes to define the color that will be applied to the SVG when fill="currentColor" or stroke="currentColor" is used.
Example 1: Inline SVG
If you're using an inline SVG, make sure to set fill="currentColor or stroke="currentColor", and then use Tailwind's text-[color] utility.
<!-- Change SVG color dynamically using text-purple-500 -->
<div class="text-purple-500">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
viewBox="0 0 24 24"
class="w-6 h-6"
>
<path
d="M12 0C5.37 0 0 5.37 0 12s5.37 12 12 12 12-5.37 12-12S18.63 0 12 0z"
/>
</svg>
</div>
Here:
- The SVG's
fill is set to follow the currentColor.
- Tailwind's
text-purple-500 dynamically sets the color to purple.
- You can apply any Tailwind text color class (like
text-red-500, text-blue-700, etc.).
Example 2: Adding a Stroke Color to SVG
If you want to set the stroke color of an SVG, you can also use the stroke="currentColor" attribute.
<!-- Styling SVG stroke color with Tailwind -->
<div class="text-green-500">
<svg
xmlns="http://www.w3.org/2000/svg"
stroke="currentColor"
fill="none"
viewBox="0 0 24 24"
class="w-6 h-6"
stroke-width="2"
>
<path
d="M12 2l3.09 6.26L22 9l-5 4.87L18.18 21"
/>
</svg>
</div>
Here:
stroke="currentColor" is used to inherit the text-green-500 color.
- The
stroke-width defines the thickness of the stroke.
Example 3: External SVG (Using CSS utilities)
When dealing with an external SVG (e.g., via <img>), the text-[color] utility won't work directly, since <img> doesn't respect currentColor. Instead, you must edit the fill or stroke directly in the SVG file or use custom code to change its internal styles.
Alternatively, you can use inline <svg> elements as demonstrated above for greater control over the styling.
Bonus: Hover Effects
You can also add interactive hover or focus effects to change the color dynamically using Tailwind's modification classes like hover: or focus:.
<div class="text-gray-500 hover:text-blue-500">
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" class="h-6 w-6" viewBox="0 0 24 24">
<path d="M12 0C5.37 0 0 5.37 0 12s5.37 12 12 12 12-5.37 12-12S18.63 0 12 0z" />
</svg>
</div>
Here:
- The SVG is initially gray (
text-gray-500).
- On hover, the color changes to blue (
hover:text-blue-500).
Key Tailwind Utilities for SVG Color:
- Use
text-[color] for fill or stroke styled SVGs based on currentColor.
- Use Tailwind modifiers like
hover:, focus:, sm:, etc., to create dynamic styles.
- Tailwind's arbitrary value syntax (e.g.,
text-[#ff0000]) can be used for custom colors.
By following these steps, you can easily style your SVG icons dynamically using Tailwind CSS.