Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Practical Astro
Lesson Plan
  1. Content Systems
    1. Introduction
    2. An import alias
    3. Using Markdown
    4. Markdown extended
    5. Markdown component
    6. Markdown component in frameworks
    7. Content collections made easy
    8. Basic blogs
    9. Blogs made easy
    10. Prev/Next navigation
    11. Pagination made easy
    12. Better tag pages
    13. Paginated tag pages
    14. Markdown layouts
    15. Partials
    16. JS over JSON Data
    17. Dynamic 404 pages
    18. Putting everything together
    19. Other useful systems
  2. Image Systems
    1. Introduction
    2. Images are complicated because of this
    3. Astro's Image component
    4. Astro's Picture component
    5. Improvements over Astro's Image and Picture Components
    6. Images in Markdown and MDX
    7. Images in framework components
    8. Background images
    9. Art Direction with Astro
    10. Videos without layout shifts
    11. GIF Replacements and background video
    12. SVGs and icons
    13. SVG backgrounds
    14. Adding noise to images
    15. Asset caching on GitHub Actions
    16. Putting everything together
    17. Other useful systems

SVGs and icons

4m:9s

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:

  1. SVG icons from icon packs
  2. Custom SVG icons
  3. 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.

Iconify's icon search page

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 />
User
Home
Search
Settings
Bell
Heart
Star
Camera

Third, I built a couple of enhancements into the component:

  • I pass the class prop into the SVG so they can be styled with classes — this is very useful with Tailwind.
  • I set each SVG’s viewBox from iconify’s width and height values so icons can be resized easily.
  • Each icon is sized to 1em so 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.

SVG in 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 width and height values if they’re missing, using values from the viewBox.
  • It creates a viewBox property if it’s missing, using width and height values 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" />