DEV Community

Cover image for From Six SVG Files to One Editable Toolbar Sprite
Axialis Software
Axialis Software

Posted on Edited on

From Six SVG Files to One Editable Toolbar Sprite

The search icon in your toolbar looks a little faint beside the others. Its shape and size are fine. You just want a heavier stroke.

If your toolbar reads from an SVG sprite, the HTML already knows which icon to draw. In this walkthrough, we’ll build a six-icon sprite in IconVectors 2.30 for Windows, connect it to a small HTML toolbar, and make that stroke change in place.

Create the sprite from six SVG files

Put six SVGs in a folder. For this example, use search.svg, filter.svg, refresh.svg, download.svg, share.svg, and settings.svg. The sample drawings use a 24 × 24 viewBox and the same starting stroke width, so the change to Search will be easy to spot.

  1. Open the folder in Icon Explorer and select the six SVGs.
  2. Choose Create SVG Sprite and name the new file icons.svg.
  3. Press F3 and open the SVG Sprite tab. Find the symbol ID for Search.

The command creates a sprite in the current folder and opens it in the editor. The six source files are still there. The strip below the canvas lets you switch between entries in the sprite.

For the markup below, the Search symbol needs id="search". If your file uses a different ID, use the one you see in the source or set it with Change SVG ID before wiring up the toolbar.

Reference the icons by ID

Put toolbar.html alongside icons.svg. Here’s the markup for Search and Filter. The other four buttons use the same pattern with their own symbol IDs.

<div role="group" aria-label="File actions">
  <button type="button">
    <svg width="24" height="24" viewBox="0 0 24 24" aria-hidden="true">
      <use href="./icons.svg#search"></use>
    </svg>
    Search
  </button>

  <button type="button">
    <svg width="24" height="24" viewBox="0 0 24 24" aria-hidden="true">
      <use href="./icons.svg#filter"></use>
    </svg>
    Filter
  </button>
</div>
Enter fullscreen mode Exit fullscreen mode

The part before # points to the sprite file. The part after it picks the symbol. Each button already has a visible label, so its SVG is hidden from assistive technology.

Serve the page and the sprite from the same local server. With Python installed, run this from the folder that contains both files:

python -m http.server 8000
Enter fullscreen mode Exit fullscreen mode

Then open http://localhost:8000/toolbar.html.

Edit Search and refresh the page

  1. Open the same icons.svg that the page loads.
  2. Select Search in the sprite strip, then select its drawing on the canvas.
  3. Change the stroke width from 1.75 to 3. Save the sprite and refresh the page.

Search now has a heavier outline. If the browser still shows the earlier drawing, do a hard refresh. The other five icons should look the same.

The same six-button toolbar is shown twice. Only the Search icon has a heavier stroke in the second row. Both rows reference icons.svg#search.

The HTML still reads <use href="./icons.svg#search"></use>. To check what changed, look at the search symbol before and after. The stroke width is different, and the ID is still search.

Keep the IDs stable

The first time you wire the toolbar up, the symbol IDs become part of the contract with the page. A change to the artwork can ship as an update to icons.svg. If you rename a symbol’s SVG ID, update any external references to it as part of the same change.

If your build generates icons.svg from individual files, make the lasting edit in the source SVG and regenerate the sprite so the change survives the next build.

That makes the next toolbar fix straightforward. Open the sprite, pick the icon, make the edit, and save the file the app already loads.

Try IconVectors 2.30 for Windows

Top comments (0)