SVG Styling
แนวคิด
Section titled “แนวคิด”SVG utilities ช่วยให้ style icons และ graphics ด้วย Tailwind:
- fill — สีเติมภายใน
- stroke — สีเส้นขอบ
- stroke-width — ความหนาเส้น
Tip: ใช้
fill-currentหรือstroke-currentเพื่อให้ SVG ใช้text-*color
กำหนดสีเติมภายใน SVG:
<svg class="fill-blue-500"> <circle cx="50" cy="50" r="40" /></svg>
<svg class="fill-current text-red-500"> <!-- ใช้ text color --></svg>
<svg class="fill-none"> <!-- โปร่งใส --></svg>Stroke
Section titled “Stroke”กำหนดสีเส้นขอบ:
<svg class="stroke-gray-500"> <line x1="0" y1="0" x2="100" y2="100" /></svg>
<svg class="stroke-current text-blue-500"> <!-- ใช้ text color --></svg>Stroke Width
Section titled “Stroke Width”กำหนดความหนาของเส้น:
<svg class="stroke-1">...</svg><!-- 1px --><svg class="stroke-2">...</svg><!-- 2px -->| Class | Width |
|---|---|
stroke-0 | 0 |
stroke-1 | 1 |
stroke-2 | 2 |
Icon Sizing
Section titled “Icon Sizing”ใช้ w-* และ h-* หรือ size-*:
<svg class="w-4 h-4">...</svg><!-- 1rem --><svg class="w-5 h-5">...</svg><!-- 1.25rem --><svg class="w-6 h-6">...</svg><!-- 1.5rem --><svg class="size-6">...</svg><!-- v4: width & height -->Icon with Text
Section titled “Icon with Text”<button class="flex items-center gap-2"> <svg class="w-5 h-5 fill-current" viewBox="0 0 20 20"> <path d="..." /> </svg> <span>Button text</span></button>Hover Effects
Section titled “Hover Effects”<svg class="w-6 h-6 fill-gray-400 hover:fill-blue-500 transition-colors"> ...</svg>
<svg class="w-6 h-6 stroke-gray-400 hover:stroke-blue-500 transition-colors"> ...</svg>Group Hover
Section titled “Group Hover”<button class="group flex items-center gap-2 hover:text-blue-500"> <svg class="w-5 h-5 fill-gray-400 group-hover:fill-blue-500 transition"> ... </svg> <span>Hover me</span></button>Rotate & Transform
Section titled “Rotate & Transform”<svg class="w-6 h-6 hover:rotate-12 transition-transform">...</svg>
<svg class="w-6 h-6 animate-spin"> <!-- Loading spinner --></svg>Dark Mode
Section titled “Dark Mode”<svg class="fill-gray-900 dark:fill-white">...</svg>
<!-- หรือ invert สำหรับ black icons --><img src="/icon-black.svg" class="dark:invert" />ตัวอย่างการใช้งาน
Section titled “ตัวอย่างการใช้งาน”Icon Button
Section titled “Icon Button”<button class="p-2 rounded-lg hover:bg-gray-100 transition group"> <svg class="w-5 h-5 fill-gray-500 group-hover:fill-blue-500 transition" viewBox="0 0 20 20" > <path d="M10 3a1 1 0 011 1v5h5a1 1 0 110 2h-5v5a1 1 0 11-2 0v-5H4a1 1 0 110-2h5V4a1 1 0 011-1z" /> </svg></button>Logo with Hover
Section titled “Logo with Hover”<a href="/" class="group"> <svg class="w-8 h-8 fill-gray-900 group-hover:fill-blue-500 dark:fill-white transition-colors" > <!-- logo path --> </svg></a>Animated Loading Spinner
Section titled “Animated Loading Spinner”<svg class="animate-spin w-5 h-5 text-blue-500" viewBox="0 0 24 24"> <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" fill="none" /> <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" /></svg>Social Icons
Section titled “Social Icons”<div class="flex gap-4"> <a href="#" class="text-gray-400 hover:text-blue-500 transition"> <svg class="w-6 h-6 fill-current"><!-- twitter --></svg> </a> <a href="#" class="text-gray-400 hover:text-pink-500 transition"> <svg class="w-6 h-6 fill-current"><!-- instagram --></svg> </a> <a href="#" class="text-gray-400 hover:text-gray-900 dark:hover:text-white transition" > <svg class="w-6 h-6 fill-current"><!-- github --></svg> </a></div>Best Practices
Section titled “Best Practices”| Do ✅ | Don’t ❌ |
|---|---|
ใช้ fill-current + text-* สำหรับ single-color icons | Hard-code fill color |
ใช้ size-* (v4) แทน w-* h-* | ใส่ width/height แยกเสมอ |
ใช้ transition กับ color changes | เปลี่ยนสีทันทีไม่มี animation |
ใช้ dark:invert สำหรับ black icons | สร้าง icon 2 ชุด light/dark |
fill-currenttrick: ใช้text-blue-500แล้ว fill จะเป็น blue อัตโนมัติ- Icon libraries: Heroicons, Lucide ออกแบบมาให้ใช้กับ Tailwind ได้ดี
- Loading spinner: ใช้
animate-spin+ SVG circle/path - Accessibility: อย่าลืมใส่
aria-labelหรือ<title>ใน SVG สำหรับ screen readers