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

SVG backgrounds

2m:21s

SVG backgrounds are honestly one of the best ways to add visual interest to a website. One example is the breadcrumb bar you see on this page.

Another example is the patterned background you see on Learn JavaScript.

Learn JavaScript website with an SVG pattern background

Adding SVG backgrounds

Find or create an SVG pattern

First, you have to find or create an SVG pattern.

The easiest way is to start with one of the many resources out there, like Hero Patterns, Pattern Monster, and SVGBackgrounds.com. Go through them, find the pattern that you like, and download the SVG.

The SVG will typically be a standard SVG that looks like this:

<svg xmlns="http://www.w3.org/2000/svg" width="152" height="152">
<path d="..." fill="#9C92AC" fill-opacity="0.4" />
</svg>

Convert the SVG into a pattern

What you have to do next is to convert it into a pattern by following these four steps:

  1. Change the <svg> tag to <pattern> and keep everything inside.
  2. Keep the width and height — that’s the size of the repeating tile.
  3. Add an id and patternUnits="userSpaceOnUse".
  4. Drop the xmlns attribute.
<pattern id="temple" patternUnits="userSpaceOnUse" width="152" height="152">
<path d="..." fill="#9C92AC" fill-opacity="0.4"></path>
</pattern>

Then include these patterns into an svg element like this. You can include as many patterns as you want.

<svg width="0" height="0">
<!-- pattern 1 -->
<!-- pattern 2 -->
</svg>

Include the SVG pattern

If you have included the patterns into the HTML, you can use any of the patterns by referencing their #id like this. So if I want to use the temple pattern, I just write that in the fill’s url().

<svg class="w-full h-full">
<rect width="100%" height="100%" fill="url(#temple)"></rect>
</svg>

Making things easier with SVG and SVGBackground components

I typically put all my patterns into a patterns.svg file.

patterns.svg
<svg width="0" height="0">
<!-- pattern 1 -->
<!-- pattern 2 -->
</svg>

Then I include this patterns.svg with the SVG component at the top of my pages.

index.astro
---
import { SVG } from '@splendidlabz/astro'
---
<SVG name="patterns" />

Finally, I include the pattern itself with an SVGBackground component. This basically adds the fill: url layer.

---
import { SVG, SVGBackground } from '@splendidlabz/astro'
---
<SVG name="patterns" />
<section class="stack bg-purple-100">
<SVGBackground pattern="temple" />
<!-- Content goes here -->
</section>

That’s it for SVG backgrounds and patterns!

Code snippets to copy-paste

HTML for patterns.svg

src/svg/patterns.svg
<svg width="0" height="0">
<!-- pattern 1 -->
<!-- pattern 2 -->
</svg>

HTML for each pattern

<pattern id="pattern-id" patternUnits="userSpaceOnUse" width="152" height="152">
<!-- Pattern content -->
</pattern>

Using SVG and SVGBackground

---
import { SVG, SVGBackground } from '@splendidlabz/astro'
---
<SVG name="patterns" />
<section class="stack">
<SVGBackground pattern="temple" />
<!-- Content goes here -->
</section>