← Back to HTML & CSS, page 8

html-css-0151 · Easy fresher

How do you use an inline SVG icon so that it scales, follows the text colour and stays accessible?

Give the svg a viewBox so it scales, use currentColor so it follows the text colour, and hide decorative icons with aria-hidden. Icon-only controls need an accessible name from visually hidden text or aria-label.

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" and focusable="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-label on the button. For a standalone image, use role="img" on the svg with aria-labelledby pointing to a title element inside it, or an aria-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.

html
<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.