In a hotel booking app, a row of small icons under the photo says wifi, breakfast and pool, without a single word. If the icons come from one set, you read them at a glance. If one is heavy, one thin, and another filled in, your eye stops and reading them gets slower.
An icon is a small picture that stands for one thing: wifi, breakfast, hot water. A good icon is recognised at a glance. But an icon doesn't work alone: it still needs text, a colour that can be seen, and a name for screen readers. Picture the facilities board in front of Mrs. Sri's homestay. If each symbol were drawn by a different person, one with a thin line, one as a thick black block, another with a fat marker, the board would look patched together. If they're all drawn with the same marker in the same box, the board is tidy even though the symbols differ. On a web page, that same marker and box are the same rules for every icon. And a guest who can't see the board needs someone to read it out correctly. On the web, a screen reader does the reading.
One set: the same grid, stroke and style
An icon set is a group of icons drawn by the same rules. The facility icons on Mrs. Sri's page are drawn on a 24 × 24 grid (viewBox="0 0 24 24"), with a stroke (line) 2 thick and round line ends. All of them are outline (lines with no fill). Each icon is written with the <svg> tag, an SVG image: a picture made of lines and shapes. Each <path> inside it is one line of the icon:
<ul class="amenities">
<li><svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M2 9a15 15 0 0 1 20 0"/><path d="M5 12.5a10 10 0 0 1 14 0"/><path d="M8.5 16a5 5 0 0 1 7 0"/><path d="M12 19.5h.01"/></svg> Wifi</li>
<li><svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M4 9h12v5a5 5 0 0 1-5 5H9a5 5 0 0 1-5-5z"/><path d="M16 11h1.5a2.5 2.5 0 0 1 0 5H16"/><path d="M8 2.5v3M12 2.5v3"/></svg> Breakfast</li>
<li><svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3c3 4 6 7.5 6 11a6 6 0 0 1-12 0c0-3.5 3-7 6-11z"/><path d="M9.5 14.5a2.5 2.5 0 0 0 2.5 2.5"/></svg> Hot water</li>
</ul>
<style>
.amenities li { display: flex; align-items: center; gap: 6px; }
.icon {
width: 20px; height: 20px;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round; stroke-linejoin: round;
}
</style>
viewBox="0 0 24 24"is the grid the icon is drawn on.widthandheightof 20px set the size it's shown at, and everything on the grid is scaled with it (made smaller or bigger together).- In the CSS,
fill: nonegives the icon no fill,stroke-width: 2is the stroke's thickness, andstroke-linecap: roundmakes the line ends round. - Because it's scaled, a stroke of 2 on a 24 grid shows 2 × 20 / 24 = 1.67px thick. For example, one icon comes from another set drawn on a 16 grid with a stroke of 2. At 20px its stroke becomes 2 × 20 / 16 = 2.5px: 50% thicker, and that icon looks as if it were printed in bold.
| Shown at | Stroke 2 on a 24 grid | Stroke 2 on a 16 grid |
|---|---|---|
| 16px | 1.33px | 2px |
| 20px | 1.67px | 2.5px |
| 24px | 2px | 3px |
Don't mix outline and filled (fully coloured in) icons in one row. Also use only a few sizes: 16, 20 or 24px. On Mrs. Sri's page, 20px icons sit next to 14px text, with a 6px gap.
currentColor: icons follow the text colour
stroke: currentColor means the icon's stroke uses its element's color value (the text colour). So the wifi icon next to the text "Wifi" automatically matches its text. In dark mode the text becomes #f1f5f9. The icon changes with it, and its contrast on the #1f2937 card is 13.40:1.
An icon whose colour is written into its file doesn't change. An example is style="fill: #1f2937" in a file exported from a design app. On a light card that icon shows (14.68:1). But on the dark #1f2937 card its contrast is 1:1: the icon disappears.
Other options for adding icons. This lesson puts the <svg> straight into the HTML (called inline). The reason: only this way and a sprite (see the table below) follow currentColor. We tried it in Chromium, Firefox and WebKit with red text: the inline icon and the sprite turned red, while the <img> icon stayed black.
| Way | When to pick it |
|---|---|
Inline <svg> |
a few icons that must follow the text colour |
<img src="wifi.svg" alt=""> |
a fixed colour, such as a logo. The browser keeps the file in its cache (a store in the browser, so it isn't downloaded again), but currentColor does not apply |
Sprite: <svg><use href="icons.svg#wifi"/></svg> |
many icons used on many pages. One file holds every icon, and currentColor still applies |
| Icon font (a font whose letters are icons) | avoid it. If the font fails to load, the icon becomes a box or a plain word, and a screen reader may read its characters out |
You don't have to draw icons yourself. Ready-made sets such as Lucide (ISC licence), Heroicons (MIT) and Material Symbols (Apache 2.0) may be used in commercial projects. Check the licence (the rules for using them), then use one set only.
An icon next to text: aria-hidden
The save button on the Terrace Room card holds a heart icon and the text "Save". The icon was exported from a design app, complete with <title>heart-icon</title> (the picture's title) inside its <svg>. Take a guess: what does a screen reader announce for this button?
Chrome calculates the button's name as "heart-icon Save", and a guest using a screen reader hears "heart-icon Save, button". The reason: a screen reader reads an <svg> in the page as an image, and the <title> inside it becomes the image's name. A button's name (what the screen reader reads out) is taken from its content: the image's name plus the text. If the <svg> has no <title>, the icon still counts as an image, but with no name. Some screen readers then call it "image".
An icon next to text is only decoration, because the text already says everything. So hide the icon from screen readers:
<button class="save">
<svg class="icon" viewBox="0 0 24 24" aria-hidden="true">...</svg>
Save
</button>
aria-hidden="true" is an attribute that makes screen readers skip the icon, though eyes still see it. The button's name becomes "Save". The facility list above does the same: the icons are hidden, and the text "Wifi" is what's read.
A button with only an icon: aria-label
The close button on the photo gallery holds only a cross icon. The button has no text, so its name is empty, and a screen reader only says "button". Give it a name with the aria-label attribute:
<button class="close" aria-label="Close the photo gallery">
<svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M6 6l12 12M18 6L6 18"/></svg>
</button>
aria-labelbecomes the button's name, so a screen reader reads "Close the photo gallery, button".- An icon without text is only for symbols nearly everyone knows: a cross to close, a magnifying glass to search. For anything else, use icon + text.
- In a button that holds only an icon, the icon is the only sign, so it needs at least 3:1 contrast against its background (WCAG 1.4.11).
#1f2937on a white circle: 14.68:1.
A 44 × 44px tap area
The icon may be 20px, but the area you can press (the touch target, in the density lesson) must be big enough for a finger. The common guideline is 44 × 44px. That's the figure in WCAG 2.5.5 (Target Size, level AAA), and Apple uses 44pt in its design guidelines. The WCAG 2.2 minimum at level AA (2.5.8) is 24 × 24px. Here is the CSS of that close button:
.close {
display: grid; place-items: center;
width: 44px; height: 44px; /* a 20px icon + 12px on each side */
padding: 0; border: 0; border-radius: 9999px;
color: #1f2937; background: #ffffff;
}
The icon stays 20px. The button is what's made 44 × 44px, and place-items: center puts the icon in its middle.
⚠️ Icons that look patched, vanish, or get read out oddly
- Mixing sets. One icon fully filled, one more from a 16 grid whose stroke is 2.5px: the row looks patched together. Use one set, one grid, one stroke weight.
- Colour written into the icon. In dark mode its contrast is 1:1 and the icon disappears. Use
currentColor. - A decorative icon without
aria-hidden="true". Screen readers announce "heart-icon Save" or "image". Hide icons that stand next to text. - An icon-only button without
aria-label. Its name is empty, just "button". And don't leave its tap area at 24px when it can be 44px.
TL;DR: Use one icon set (a group of icons drawn by the same rules): one grid (viewBox="0 0 24 24"), one stroke weight (stroke-width: 2, shown as 1.67px at 20px), one style (outline, lines with no fill, not mixed with filled (fully coloured-in) icons), and few sizes (16, 20, 24px). An icon from a 16 grid with a stroke of 2 shows at 2.5px and looks too heavy. stroke: currentColor makes icons follow the text colour, dark mode included. A colour written into the icon file doesn't change, and in dark mode its contrast can drop to 1:1. An icon next to text is decoration: give it aria-hidden="true", so screen readers don't read out its <title> (the picture's title inside the icon) or the word "image". A button holding only an icon needs a name for screen readers through aria-label, such as aria-label="Close the photo gallery", and its icon needs 3:1 contrast. The tap area is at least 44 × 44px (WCAG 2.5.5, and Apple's guideline too), even when the icon is only 20px.
⚡ One icon set
Write:
.icon {
width: 20px; height: 20px;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round; stroke-linejoin: round;
}
Meaning: All icons in one set follow the same rules: one grid (viewBox="0 0 24 24" on the <svg> tag), one stroke weight (stroke-width: 2), and one style, outline (lines with no fill, fill: none). stroke: currentColor makes the icon's stroke follow its text colour, dark mode included. A colour written into the icon file doesn't change, and on a dark card its contrast can drop to 1:1.
⚡ An icon next to text: hide it
Write:
<button class="save">
<svg class="icon" viewBox="0 0 24 24" aria-hidden="true">...</svg>
Save
</button>
Guess first: without aria-hidden, a heart icon that still has <title>heart-icon</title> sits in a button that says "Save". What does a screen reader announce?
"heart-icon Save, button". A button's name comes from its content, the <title> (the picture's title) inside the icon included.
Meaning: An icon next to text is decoration. The aria-hidden="true" attribute makes screen readers skip it, while eyes still see it. The button's name in this code becomes "Save".
⚡ Only an icon: give it a name
Write:
<button class="close" aria-label="Close the photo gallery">
<svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M6 6l12 12M18 6L6 18"/></svg>
</button>
Meaning: This close button holds only a cross icon. With no text, the button's name is empty, and a screen reader only says "button". The aria-label attribute becomes its name: "Close the photo gallery". The icon may be 20px, but the tap area (the area a finger can press) is at least 44 × 44px.