Skip to main contentSkip to docs navigation

Chassis

Use the optimized SVG file of an icon as an image, inside the markup of a page, or as a mask in CSS. Installation, size, color and accessibility.

Introduction

The build writes one optimized SVG file per icon, dist/svgs/<name>.svg. Choose the files when a project needs a few icons and not the set: as an image, pasted into the markup of a page, or as a mask in CSS. The files are also what a design tool or an app for iOS or Android reads.

The examples use @acme/glyphs, the set of the team Acme, whose frame is 16 and whose prefix is ag. The files of another set have the same shape, with its frame and its prefix.

Installation

The files are in the dist/svgs/ folder of the output. Icon font describes the three ways to get the output: the package, the archive of a release, and the folders of a build.

Shell
npm install @acme/glyphs

The page of the set that chassis-icons preview writes shows each icon with its name.

Basic usage

Use the file of an icon as an image:

HTML
<img src="node_modules/@acme/glyphs/dist/svgs/ring-line.svg" alt="" width="16" height="16" />

The build writes each file on the frame of the set, with fill="currentcolor" and the class of the icon on its root:

PLAINTEXT
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentcolor" class="ag-ring-line" viewBox="0 0 16 16">
  <path fill-rule="evenodd" d="M8 15C11.866 15 15 11.866 15 8C15 4.13401 11.866 1 8 1C4.13401 1 1 4.13401 1 8C1 11.866 4.13401 15 8 15ZM8 13C10.7614 13 13 10.7614 13 8C13 5.23858 10.7614 3 8 3C5.23858 3 3 5.23858 3 8C3 10.7614 5.23858 13 8 13Z"/>
</svg>

Size and color

The size of an image is the width and the height of the <img> element. The color is not: a file that a page loads as an image does not know the color of the text, and its icon is black.

To color an icon, put the file into the markup of the page. The root has fill="currentcolor", so the icon takes the color of the text around it:

HTML
<span style="color: rebeccapurple;">
  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentcolor" class="ag-ring-line" viewBox="0 0 16 16">
    <path fill-rule="evenodd" d="M8 15C11.866 15 15 11.866 15 8C15 4.13401 11.866 1 8 1C4.13401 1 1 4.13401 1 8C1 11.866 4.13401 15 8 15ZM8 13C10.7614 13 13 10.7614 13 8C13 5.23858 10.7614 3 8 3C5.23858 3 3 5.23858 3 8C3 10.7614 5.23858 13 8 13Z"/>
  </svg>
</span>

In CSS, use the file as a mask, and the icon has the background-color of its element:

CSS
.marker::after {
  display: inline-block;
  width: 1em;
  height: 1em;
  content: "";
  background-color: currentcolor;
  mask: url("/static/icons/svgs/diamond-line.svg") center / contain no-repeat;
}

Accessibility

An image has its alt text, and an icon in the markup of a page is named as an icon of the sprite is.

A decorative image has an empty alt, as in the example above. An image that carries meaning says it:

HTML
<img src="node_modules/@acme/glyphs/dist/svgs/ring-line.svg" alt="Notifications" width="16" height="16" />

For an icon in the markup of the page, see SVG sprite. An icon that CSS draws as a mask or a background has no name: keep the text of its element.

Customization

A file has no settings of its own. What its root holds follows from the configuration of the set.

A set of your own

The frame and the prefix are the settings frame and prefix of chassis.build. The root of dist/svgs/<name>.svg has the frame as its width, its height and its viewBox, 0 0 <frame> <frame>, and class="<prefix>-<name>". With "frame": 16 and "prefix": "ag", as in the examples, it has width="16", height="16", viewBox="0 0 16 16" and class="ag-<name>".

Best practices

A file of dist/svgs/ is output: the build writes it again from source/. These practices keep a project on the files as they are built.

  • Read the files by name: dist/svgs/<name>.svg is the address of an icon for as long as the icon is in the set.
  • Use the sprite for many icons: a page with many icons loads one file and not one per icon.

Avoid the habits that undo this:

  • Do not edit a file of dist/svgs/: the next build writes it again. Change the icon in source/.
  • Do not expect a color from an image: an <img> element and a background-image draw the icon in black. Use the markup, a mask, the sprite or the font for a color.

Troubleshooting

Each entry gives the message or the symptom, then the cause and the fix.

Icon is black

An icon in an <img> element or a background-image ignores the color of the text. An image is drawn apart from the page and has no text color. Put the file into the markup of the page, or use it as a mask with a background-color.

Next steps

Continue with one of these pages: