ZL
About Articles Contact
Published on Nov 21, 2023
Filed under:
#svelte,
#astro,
#html,
#svg,
#splendidlabz

The easiest way to use SVGs in an Astro or Svelte app

SVG is one of the most important elements in a website or web application because it provides us with scalable graphics.

They are usually used for two things:

  1. Icons from icon sets
  2. Custom graphics

Today I’m going to show you how the SVG utility from Splendid UI works, and why it’s the easiest way to use SVG to load both icons and custom graphics.

Installing the SVG Utility

First, you need to install Splendid UI.

Terminal window
npm install splendidlabz —save

Then you import the SVG component from splendidlabz/svelte

<script>
import { SVG } from 'splendidlabz/svelte'
</script>

Since this SVG is a Svelte component, it can be used for both Astro and Svelte projects.

Loading an icon set with the SVG component

The SVG component uses Iconify to load icons. This lets you load icons from popular icon sets just by naming the icon set and the icon name.

Here are some examples:

  • tabler:menu - Menu icon from Tabler
  • material-symbols:menu - Menu icon from Material Symbols
  • zondicons:menu - Menu icon from Zondicons
<SVG icon="tabler:menu" /> Tabler's Menu
<SVG icon="material-symbols:menu" /> Material Symbol's menu
<SVG icon="zondicons:menu" /> Zondicon's menu
Demo
Tabler's Menu
Material Symbol's menu
Zondicon's menu

Loading icons in Svelte

You can load the icons with the following code in Svelte:

<script>
import { SVG } from 'splendidlabz/svelte'
</script>
<SVG icon="icon-set:icon-name" />

Loading icons in Astro

If you’re using Astro, you must add a client:load directive or the icons won’t be loaded.

---
import { SVG } from 'splendidlabz/svelte'
---
<SVG icon="icon-set:icon-name" client:load />

We need a client directive when using Astro because Iconify saves icons in localStorage, which means icons will not be loaded on the server.

Unfortunately, this means you’ll see a flicker of invisible icons for a bit when the page is loaded for the first time. (On subsequent loads, the process is so quick you’ll never even notice it).

. Your browser doesn't support the HTML video element. Click here to watch the video.
Loading icons on subsequent requests can be pretty quick!

As you can see from the video above, the time taken to load the SVG icon is negligible. After seeing this, I felt that attempts to remove this tiny delay would be considered over-engineering.

That’s why I’m happy to release the SVG component as it is today.

Finding icon sets and names

You can view all supported icon sets and their respective icons through Iconify’s icon browser, the Figma component or Icones.

Iconify icon browser
Finding an icon's name

Loading a custom SVG from your project files

This SVG component also supports loading custom SVGs from your project files.

To use this, you need to make sure you have two conditions fulfilled:

  1. There is an svg folder in your src directory.
  2. You have added your SVG into this folder.

When both conditions are fulfilled, you can pass the name of this SVG file into icon and the SVG component will do the rest.

If you use Astro, there’s no need to use the client:load directive in this case because SVG will create the necessary SVG element in the HTML right away.

For example, if you have an SVG that is called logo.svg, you only need to pass logo into icon.

---
import { SVG } from 'splendidlabz/svelte'
---
<SVG icon="logo" />

That’s it!

Wrapping Up

I created this SVG component to make it easier for me to load icons when I’m building Astro and Svelte apps.

I hope you enjoy it as much as I do!

And if you do, please consider checking out Splendid UI and spreading the word about it. Thank you.

Edit: I’ve divided the SVG component to Icon and SVG. Please see their respective documentation accurate information.

Further reading

  • Iconify
  • Splendid UI
  • SVG component docs
Previous An easy way to implement conversion tracking for Facebook Next Why I love the web

Join My Newsletter

I share what I’m learning on this newsletter: code, building businesses, and living well.

Sometimes I write about technical deep-dives, product updates, musings on how to live, and sometimes my struggles and how I’m breaking through.

Regardless of the type of content, I do my best to send you an update every week.

If you’re into making things and growing as a person, you’ll probably feel at home here.

“

Zell is the rare sort of developer who both knows his stuff and can explain even the most technical jargon in approachable — and even fun — ways!

I’ve taken his courses and always look forward to his writing because I know I’ll walk away with something that makes me a better front-ender.

Geoff Graham
Geoff Graham — Chief Editor @ CSS Tricks
The Footer

General

Home About Contact Testimonials Tools I Use

Projects

Magical Dev School Splendid Labz

Socials

Youtube Instagram Tiktok Github Bluesky X

Follow Along

Email RSS
© 2013 - 2025 Zell Liew / All rights reserved / Terms