I have a very different treatment for SVGs.
They don’t go through the image optimization pipeline because that pipeline is not built for SVGs. Instead, I look at SVGs in three different ways:
- SVG icons from icon packs
- Custom SVG icons
- SVG backgrounds
We will focus on the SVG icons in this lesson and SVG backgrounds in the next one.
SVG Icons
I think the best way to include icons in your project is through Iconify. It gives you access to 200+ free icon sets.
Most people would probably do this with either Astro Icon or Astro IconSet, but I don’t use either of them.
I use the Icon component from Splendid Svelte instead.
Why Splendid Svelte Icons over Astro Icon or Astro IconSet
I like using Splendid Svelte’s Icon over Astro Icon and Astro IconSet for three reasons.
First, it works in both Astro and Svelte — I use Svelte as my frontend framework, so having a component that works in both beats one that only works with Astro.
Second, there’s no setup required. With Astro Icon or Astro IconSet, you need to install an @iconify-json package for every icon set you use. You also need to declare them in your config to keep server bundles lean.
Splendid’s Icon needs none of that — it loads any icon from Iconify’s API by name, then caches it in localStorage so subsequent loads are near-instant. The downside is that it’s a client component, so icons only appear after hydration — you may notice a brief delay on first load.
---import { Icon } from '@splendidlabz/svelte'---
<Icon icon="tabler:user" client:idle />Third, I built a couple of enhancements into the component:
- I pass the
classprop into the SVG so they can be styled with classes — this is very useful with Tailwind. - I set each SVG’s
viewBoxfromiconify’swidthandheightvalues so icons can be resized easily. - Each icon is sized to
1emso they’re at a somewhat natural size when used together with text. - Each icon has
aria-hidden="true"by default because they’re usually used close to text labels. This way, screen readers don’t speak the icon out loud.
If you’re interested, I talk about how I style SVGs in Unorthodox Tailwind.
Custom SVGs
The best way to include SVGs is to import them into your project. Astro lets you do this easily with an import statement.
---import YourSVG from '@/svgs/your-svg.svg'---
<YourSVG />The problem with this is that you have to write one import statement for each SVG, which can be clunky.
---import SVG1 from '@/svgs/svg1.svg'import SVG2 from '@/svgs/svg2.svg'import SVG3 from '@/svgs/svg3.svg'import SVG4 from '@/svgs/svg4.svg'import SVG5 from '@/svgs/svg5.svg'---
<SVG1 /><SVG2 /><SVG3 /><SVG4 /><SVG5 />I prefer having one SVG component to handle this process. So I built myself an SVG component in Splendid Astro.
---import { SVG } from '@splendidlabz/astro'---
<SVG name="svg1" /><SVG name="svg2" /><SVG name="svg3" /><SVG name="svg4" /><SVG name="svg5" />Enhancements in the SVG component
Since I’m building the SVG component, I had the leeway to add a few enhancements as well, like:
- It fills in
widthandheightvalues if they’re missing, using values from theviewBox. - It creates a
viewBoxproperty if it’s missing, usingwidthandheightvalues as the source. - It lets you add a class to the SVG — useful when you use Tailwind.
- It forwards all other attributes, like
aria-label, onto the<svg>as well.
Custom SVGs in Svelte
If you need custom SVGs in Svelte, there’s a SVG component in Splendid Svelte too!
This is built to be browser-friendly, so you can use it inside islands.
<script> import { SVG } from '@splendidlabz/svelte'</script>
<SVG name="logo" />Code snippets to copy-paste
Icons from icon packs
---import { Icon } from '@splendidlabz/svelte'---
<Icon icon="<icon-pack-name>:<icon-name>" client:idle />Custom SVG in Astro
---import { SVG } from '@splendidlabz/astro'---
<SVG name="logo" />Custom SVG in Svelte
<script> import { SVG } from '@splendidlabz/svelte'</script>
<SVG name="logo" />Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson