2026-07-15 13:41:24 +03:00
# SVG Sprite for a Site Without a Bundler
2026-07-14 16:11:39 +03:00
2026-07-15 13:41:24 +03:00
Combine SVG icons into one file and use them on an HTML page.
2026-07-14 16:11:39 +03:00
2026-07-15 13:41:24 +03:00
## Generate the sprite
2026-07-14 16:11:39 +03:00
2026-07-15 13:41:24 +03:00
You do not need to install the package in your project.
2026-07-14 16:11:39 +03:00
2026-07-15 13:41:24 +03:00
### 1. Create the sprite config
2026-07-15 16:56:44 +03:00
Choose a directory for the future SVG sprite, for example `assets/app-icons` , and create `svg-sprite.config.json` inside it. In `input` , specify the path to existing SVG files relative to the configuration file. There is no need to move or copy the icons.
2026-07-14 16:11:39 +03:00
```json
{
"mode" : "standalone" ,
"name" : "icons" ,
2026-07-15 13:41:24 +03:00
"input" : "../svg-icons/**/*.svg"
2026-07-14 16:11:39 +03:00
}
```
2026-07-15 16:56:44 +03:00
### 2. Generate the sprite
2026-07-14 16:11:39 +03:00
2026-07-15 13:41:24 +03:00
Pass the config path to the command:
2026-07-14 16:11:39 +03:00
```bash
2026-07-15 13:41:24 +03:00
npx --yes @gromlab/svg-sprites assets/app-icons/svg-sprite.config.json
2026-07-14 16:11:39 +03:00
```
2026-07-15 13:41:24 +03:00
The package collects the icons in a `.svg-sprite` directory next to the config:
2026-07-14 16:11:39 +03:00
2026-07-15 13:41:24 +03:00
```text
assets/app-icons/.svg-sprite/
├── sprite.svg
└── svg-sprite.manifest.json
2026-07-14 16:11:39 +03:00
```
2026-07-15 13:41:24 +03:00
- `sprite.svg` is the finished sprite for use on the site.
- `svg-sprite.manifest.json` contains icon data for Viewer.
2026-07-14 16:11:39 +03:00
2026-07-15 13:41:24 +03:00
The `.svg-sprite` directory is created automatically and fully replaced on every generation. Do not edit its contents manually.
2026-07-14 16:11:39 +03:00
2026-07-15 16:56:44 +03:00
### 3. Use an icon
2026-07-14 16:11:39 +03:00
2026-07-15 13:41:24 +03:00
In `index.html` , point to the generated `sprite.svg` . After `#` , add the icon file name without the `.svg` extension:
2026-07-14 16:11:39 +03:00
```html
2026-07-15 13:41:24 +03:00
< svg
width = "24"
height = "24"
aria-label = "Done"
>
2026-07-15 16:56:44 +03:00
< use href = "./assets/app-icons/.svg-sprite/sprite.svg#icon-name" ></ use >
2026-07-14 16:11:39 +03:00
</ svg >
```
2026-07-15 13:41:24 +03:00
## Debug and preview
2026-07-14 16:11:39 +03:00
2026-07-15 13:41:24 +03:00
`sprite.svg` is a technical file, not an icon gallery. Opening it does not provide a convenient view of the whole set. Gradients, masks, filters, and references to internal `id` values may also render with artifacts.
2026-07-14 16:11:39 +03:00
2026-07-15 13:41:24 +03:00
Use the official Viewer for visual checks. It displays every icon in the sprite and helps you verify its colors and rendering.
2026-07-14 16:11:39 +03:00
2026-07-15 13:41:24 +03:00
Viewer is optional and intended only for development. You do not need to install the package through npm.
2026-07-14 16:11:39 +03:00
2026-07-15 13:41:24 +03:00
Viewer works directly with files from `.svg-sprite` . Nothing needs to be copied.
2026-07-14 16:11:39 +03:00
2026-07-15 13:41:24 +03:00
### Add Viewer to the page
2026-07-14 16:11:39 +03:00
2026-07-15 13:41:24 +03:00
Add a module script to `index.html` and provide paths to the generated manifest and sprite:
2026-07-14 16:11:39 +03:00
```html
< script
type = "module"
2026-07-15 13:41:24 +03:00
src = "https://cdn.jsdelivr.net/npm/@gromlab/svg-sprites/dist/viewer-element.js"
2026-07-14 16:11:39 +03:00
></ script >
2026-07-15 13:41:24 +03:00
< gromlab-sprite-viewer
viewer-title = "Project icons"
manifest-url = "./assets/app-icons/.svg-sprite/svg-sprite.manifest.json"
sprite-url = "./assets/app-icons/.svg-sprite/sprite.svg"
></ gromlab-sprite-viewer >
2026-07-14 16:11:39 +03:00
```
2026-07-15 13:41:24 +03:00
You can move Viewer to a separate HTML file in the site root used only for development and icon checks.