Accessibility
Accessible SVG icons in React
Make SVG icons accessible in React with labeled buttons, decorative icons, meaningful graphics, and a keyboard and screen-reader checklist.
Start by deciding whether the SVG repeats nearby text, represents a control, or communicates information on its own. Put an action’s accessible name on the button or link. Give a standalone graphic a text equivalent when the surrounding content does not already convey its meaning.
Let visible text name the control
Place a download glyph beside text such as “Download report.” The text already describes the action, so mark the SVG aria-hidden="true" to avoid duplicate announcements. The same applies when an icon accompanies a visible warning such as “Payment failed.”
An icon loaded with an img element can use alt="" when it is decorative. For an inline decorative SVG, use aria-hidden="true" on the SVG itself. Keep aria-hidden off any parent button or link. Hiding the parent can remove the interactive control from a screen reader’s view while leaving it in the keyboard tab order.
If you use visible text, keep it as the control’s name. A separate aria-label can override that text and make the control harder to find by voice, especially when the wording differs. See the W3C design system’s SVG examples.
Name an icon-only button and expose its state
For an icon-only action, put the name on the button. The symbol usually has no meaning apart from the control. A bookmark toggle should also expose whether it is pressed. Keep the label stable so a screen reader can report the new state.
import { Bookmark } from 'lucide-react'
export function BookmarkButton({ saved, onToggle }) {
return (
<button type="button" aria-label="Bookmark report"
aria-pressed={saved} onClick={onToggle}>
<Bookmark size={20} aria-hidden="true"
fill={saved ? 'currentColor' : 'none'} />
</button>
)
}
Use a real button so keyboard activation is built in. Keep the hit area and focus outline visible at the smallest layout. For a navigation control, use a link and give it a name that identifies the destination. The WAI button pattern explains how button names and toggle states work together.
Choose the state that matches the control
- A bookmark toggle can keep the name “Bookmark report” and use
aria-pressedfor its on and off state. - A disclosure such as “Filters” can use
aria-expandedto communicate whether its panel is open. - A navigation link for the active section can use
aria-current="page"when it leads to the current page.
Name repeated controls with the item they affect when users need that context, such as “Remove invoice 1042.” A tooltip can clarify a symbol for sighted users who hover or focus it. Give the button its own accessible name even when it has a tooltip.
Give meaningful graphics a text equivalent
Use adjacent text for status where possible. If an SVG loaded as an image is the sole way someone learns the status, give it meaningful alternative text, such as alt="Connection offline". If the same text is visible next to the image, use an empty alt value.
For a meaningful SVG rendered inline, use an image role and a label. Generate the title ID per instance if the component can appear more than once.
import { useId } from 'react'
export function OfflineIndicator() {
const titleId = useId()
return (
<svg role="img" aria-labelledby={titleId} width="16" height="16"
viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="2">
<title id={titleId}>Connection offline</title>
<circle cx="8" cy="8" r="6" />
<path d="M4 12 12 4" />
</svg>
)
}
For a status that changes after a user action, announce the outcome through your status text or an appropriate live region. Changing just the SVG color or swapping the glyph can leave the update unclear.
Make the control easy to see and tap
Separate the glyph size from the target size. A 16 px icon can sit inside a padded button with a visible focus outline. For controls and graphics that users need to identify, check the qualifying visual cues against adjacent colors, including selected states. WCAG 2.2 generally calls for 3:1 non-text contrast for required visual information, with exceptions described in the non-text contrast criterion.
Use more than color for status. A warning triangle beside visible warning text still communicates the issue when the browser uses forced colors. A selected toggle can change shape or fill and expose its state to assistive technology at the same time.
Walk through a report toolbar with a keyboard
Imagine a report page with a bookmark toggle, a collapsible filters panel, and a button that runs an export. The three icons look similar in the toolbar because they have the same size and stroke weight. Each control still needs its own behavior and accessible state.
- Bookmark report stays in the same place and keeps its name. After activation, focus remains on the control, the icon changes appearance, and
aria-pressedchanges totrue. Activate it again and check the inverse. - Filters reveals fields on the same page. Use a disclosure button with
aria-expanded. Check that Tab can reach the fields when the panel is open and skips them when it is closed. - Export CSV starts an operation on the page. Confirm that the name stays understandable even when the download icon turns into a spinner. Provide visible progress feedback if there is a meaningful delay and a clear result when the operation completes or fails.
Try the sequence without a pointer. Tab to each control and watch where the focus indicator lands. Press Space on the bookmark and Enter on Filters. Continue through the fields in reading order. Repeat while listening with a screen reader. The wording depends on the browser and assistive technology, so verify the meaning of each announcement and the control’s state.
Keep a disclosure and its panel connected
Use a button to toggle the panel, and reference the panel’s unique ID with aria-controls. Keeping the panel in the DOM with hidden makes the ID stable and removes the closed fields from the tab order.
'use client'
import { useId, useState } from 'react'
import { SlidersHorizontal } from 'lucide-react'
export function ReportFilters() {
const [open, setOpen] = useState(false)
const panelId = useId()
return (
<>
<button type="button" aria-expanded={open}
aria-controls={panelId} onClick={() => setOpen(!open)}>
<SlidersHorizontal aria-hidden="true" />
Filters
</button>
<div id={panelId} hidden={!open}>
<label>
<input type="checkbox" /> Show archived reports
</label>
</div>
</>
)
}
Focus can remain on the trigger for this simple disclosure. The next Tab moves to the first field in the open panel. If another control inside the panel can close it, restore focus to the trigger when the closing action would otherwise hide the focused element. See the WAI disclosure pattern.
Announce changes without moving focus
When an export finishes, keep the feedback visible so the user can still read it after interacting with another control. If focus stays on the export button, expose the change in a live status element. Render the element before the operation starts and update its text as the operation progresses.
<p role="status">{exportMessage}</p>
Set the message from the actual outcome, such as “Export ready” or “Export failed. Try again.” If the flow creates a link to the file, put that link in the normal reading and tab order so the user can reach it. Keep live updates short. Announcing every animation frame or percentage change can mask more important feedback. See the WCAG status messages guidance.
Troubleshoot missing or duplicate announcements
When a screen reader announces “button” without a name, inspect the accessible name in your browser’s accessibility tree. Check that visible text is inside the control, a referenced label ID exists, or an icon-only button has a useful aria-label. An attribute on an inner SVG alone is easy to miss or expose differently across browsers. Put the control’s name on the interactive element and test it in context.
When the same action is announced twice, look for two independent sources of meaning. A titled SVG next to visible button text can surface the icon as a separate graphic. Hide the decorative SVG while leaving the button and its label exposed. Check repeated toolbar controls for duplicated IDs if names or states seem to belong to the wrong item.
Run an automated accessibility scan for missing names and related markup errors. Then do the task with a keyboard and a screen reader. Automated checks can identify an unnamed button, while a person can find a control named “More” whose purpose remains unclear next to several other “More” buttons.
Check the result in context
- Tab through the interface and verify that the focused control is always visible.
- With a screen reader, check the control’s name, role, and initial state. Activate it with Enter or Space and confirm that the updated state is announced.
- Check active and inactive states with reduced color cues or forced colors.
- Try a narrow screen and verify that nearby targets are easy to distinguish and activate. See the WCAG target-size guidance.
- Look for duplicate announcements on controls that already have visible labels. In the browser’s accessibility tree, decorative icons should be absent and their buttons should still be present.
For the markup and export decisions that come before this audit, read how to use SVG icons in React.