Full answer
An inline svg in the HTML is part of the page. CSS and scripts can style it, and it needs no extra file request.
Scaling: the viewBox attribute defines the drawing's own coordinate system, such as viewBox="0 0 24 24". The width and height set the size on the page. The drawing scales to fit, and keeps its shape (preserveAspectRatio defaults to keeping the ratio). Without a viewBox, the SVG cannot scale and may be cut off.
Colour: set fill="currentColor" (or stroke="currentColor") on the shapes. currentColor is the text colour of the element, so the icon follows the color of its parent. A hover colour on the button changes the icon, with no extra rule. This also works in forced colors mode.
Accessibility has two cases.
- Decorative icon next to text, such as a search icon beside the word "Search": hide it with
aria-hidden="true"andfocusable="false", so it adds no noise. - Meaningful icon with no text, such as an icon-only button: give the control a name. Best is visually hidden text in the button, or
aria-labelon the button. For a standalone image, userole="img"on the svg witharia-labelledbypointing to atitleelement inside it, or anaria-label.
A title inside an SVG may show a tooltip in some browsers, but it should not be the only way to give a name, because support in assistive technology varies.
If the same icon is used many times, define it once with symbol and reuse it with <use href="#id">. This keeps the markup small.
The search icon is 24 by 24 pixels and is drawn in the button's blue. On hover, both the text and the icon turn red. A screen reader reads "Search" and "Close" and ignores the drawings.
Check the icons at a large zoom and in forced colors mode.
<style>
button { color: #0a58ca; font: 16px sans-serif; display: inline-flex; align-items: center; gap: 6px; }
button:hover { color: #c00; }
svg { width: 24px; height: 24px; }
</style>
<button type="button" id="b">
<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false" id="s"><circle cx="10" cy="10" r="6" fill="none" stroke="currentColor" stroke-width="2"/><path d="M15 15l6 6" stroke="currentColor" stroke-width="2"/></svg>
Search
</button>
<button type="button" id="b2" aria-label="Close"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5 5l14 14M19 5L5 19" stroke="currentColor" stroke-width="2"/></svg></button>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- viewBox sets the drawing's coordinates and lets it scale.
- currentColor follows the text colour.
- Decorative icons use aria-hidden.
- Icon-only buttons need an accessible name.
- symbol and use reuse icons.
Common mistakes
- Leaving out the viewBox, so the icon cannot scale.
- Using a fixed fill colour, so the icon ignores the text colour and hover state.
- Leaving an icon-only button with no accessible name.
Follow-ups
- What does currentColor refer to?
- How do you reuse one icon many times?
Tips
- Name the three points: viewBox, currentColor, accessible name.
- Separate decorative icons from meaningful ones.