Icon
The Ignite UI for Blazor Icon is a component that renders a named icon from a registered icon set, such as a collection of custom SVG icons.
Register SVG icons by name in a collection, then display each icon at a theme-based size in the color of the surrounding text.
Live Demo
Anatomy
An icon is a symbol representing a command, device, directory, or common action.
The Blazor Icon renders an igc-icon element, which draws the registered SVG in its shadow root and exposes no CSS parts:
igc-icon // host - inline box sized by --size
└─ #shadow-root
└─ svg // the registered SVG, 1em × 1em, filled with the current color
Getting Started
To use the Blazor Icon, follow the Ignite UI for Blazor Getting Started topic for the basic project setup, then add the component for your target platform.
Register the Ignite UI for Blazor services:
// in Program.cs file
builder.Services.AddIgniteUIBlazor();
Registering IgbIconModule, the client resource module of the IgbIcon, is optional: the component loads its resources when it first renders. Listing it in builder.Services.AddIgniteUIBlazor(typeof(IgbIconModule)) can be used to preload resources or to support elements created only on the client (see Component Modules).
Then link the theme CSS file in the app’s host page (wwwroot/index.html in a standalone Blazor WebAssembly or Blazor Hybrid app, Pages/_Host.cshtml in a Blazor Server app, Components/App.razor in a Blazor Web App):
<link href="_content/IgniteUI.Blazor/themes/light/bootstrap.css" rel="stylesheet" />
After registration, render the IgbIcon with an icon name and a collection:
<IgbIcon IconName="search" Collection="material" />
The icon stays empty until you register an SVG icon under the same name and collection, as described in Name and Collection.
Usage
Use the Blazor Icon to add a recognizable symbol to labels, buttons, list items, and navigation, or to show a status at a glance.
Name and Collection
The Blazor Icon draws the SVG that is registered under its name in its collection. The collection defaults to default. All icons on the page share one registry, so an icon that you register once is available to every Icon, and an Icon that renders before its SVG is registered updates as soon as you register it.
Register SVG markup with the RegisterIconFromTextAsync method, or fetch an SVG file from a URL with RegisterIconAsync. Call them on a referenced IgbIcon after it’s ready, then set the same IconName and Collection on each icon:
<IgbIcon @ref="SearchIcon" IconName="search" Collection="material" />
<IgbIcon IconName="build" Collection="material" />
@code {
private IgbIcon SearchIcon { get; set; }
private const string SearchSvg = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z'/></svg>";
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender && SearchIcon != null)
{
await SearchIcon.EnsureReady();
await SearchIcon.RegisterIconFromTextAsync("search", SearchSvg, "material");
await SearchIcon.RegisterIconAsync("build", "/icons/build.svg", "material");
}
}
}
Icon References
An icon reference points an icon name to another registered icon. Components such as the Combo and the Date Picker render their built-in icons through references in the default collection, and a reference that you set takes precedence over them. For example, point the input_clear reference to your own icon to replace the clear icon of these two components:
<IgbIcon @ref="CloseIcon" IconName="close" Collection="my-icons" />
@code {
private IgbIcon CloseIcon { get; set; }
private const string CloseSvg = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z'/></svg>";
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender && CloseIcon != null)
{
await CloseIcon.EnsureReady();
await CloseIcon.RegisterIconFromTextAsync("close", CloseSvg, "my-icons");
await CloseIcon.SetIconRefAsync("input_clear", "default", new IgbIconMeta()
{
Name = "close",
Collection = "my-icons",
});
}
}
}
Size
The Blazor Icon supports the small, medium, and large size presets of the theme, and uses the large preset by default. Set the --ig-size CSS variable to --ig-size-small, --ig-size-medium, or --ig-size-large to choose a preset:
.small {
--ig-size: var(--ig-size-small);
}
.medium {
--ig-size: var(--ig-size-medium);
}
<IgbIcon Class="small" IconName="build" Collection="material" />
For a size outside the presets, set --size on the icon. To resize all icons inside a container, set --ig-icon-size on the container:
.custom-size {
--size: 56px;
}
.toolbar {
--ig-icon-size: 2rem;
}
Mirrored
Set mirrored to flip the icon horizontally. Use it for directional icons, such as arrows, in right-to-left layouts.
<IgbIcon IconName="arrow_forward" Collection="material" Mirrored="true" />
Accessible Name
Hide a decorative icon from assistive technology, and give an icon that conveys meaning on its own an accessible name that describes that meaning.
Set aria-label on the icon to name it. The <title> element of a registered SVG also names the icon. Set aria-hidden="true" on a decorative icon next to visible text:
<IgbIcon IconName="warning" Collection="material" aria-label="Warning" />
<span>
<IgbIcon IconName="search" Collection="material" aria-hidden="true" />
Search
</span>
Do/Don’t
Carefully pick the Icon color to assure good contrast with the background and definitely avoid similar shades of the same color or combinations that create chromatic aberrations.
Do
Pick icon colors with strong contrast against the background.
Don’t
Avoid shades similar to the background and color combinations that cause chromatic aberration.
Properties
The Blazor Icon exposes the following properties. See the API reference for the complete type definitions.
| Name | Type | Default | Description |
|---|---|---|---|
IconName |
string |
— | The name of the icon to draw. |
Collection |
string |
default |
The registered collection in which the icon name is looked up. |
Mirrored |
bool |
false |
Whether the icon is flipped horizontally. |
Methods
The Blazor Icon draws icons that you register with the following API.
Register icons with the following methods of an IgbIcon instance. Each method also has a synchronous version without the Async suffix.
| Method | Description |
|---|---|
RegisterIconAsync |
Fetches an SVG file from a URL and registers it under a name in a collection. |
RegisterIconFromTextAsync |
Registers SVG markup under a name in a collection. |
SetIconRefAsync |
Points an icon name in a collection to the icon that an IgbIconMeta describes. |
Styling
The Blazor Icon takes the color of the surrounding text by default. Change its color and size with the following CSS variables:
| Variable | What it changes |
|---|---|
--ig-icon-color |
The icon color. Set it on an icon, or on a container to recolor all icons inside it. |
--ig-icon-size |
The icon size. Set it on an icon, or on a container to resize all icons inside it. |
--size |
The size of the icon on which you set it. |
--ig-size |
The size preset: --ig-size-small, --ig-size-medium, or --ig-size-large. |
The igc-icon element exposes no CSS parts, so apply the styles to the element itself.
Sass Theming
Create a theme with the icon-theme function and include it with the tokens() mixin. The function accepts the $color, $size, and $disabled-color parameters, and the mixin emits the matching --ig-icon-* variables in the scope where you include it.
The Sass functions come from the igniteui-theming package. See the Theming topic for the Sass setup.
@use "igniteui-theming" as *;
$custom-icon-theme: icon-theme(
$color: #1481b8
);
:root {
@include tokens($custom-icon-theme);
}
CSS Variables
Set the color and size on an icon or a container when you need local styling without a Sass build step:
igc-icon {
--size: 28px;
color: var(--ig-primary-500);
}
.toolbar {
--ig-icon-color: var(--ig-primary-500);
}
Styling with Tailwind
You can style the Blazor Icon with the custom Tailwind utility classes from igniteui-theming. Make sure to set up Tailwind first, then import the Ignite UI utilities in your global stylesheet:
@import "tailwindcss";
@import "igniteui-theming/tailwind/utilities/material.css";
Use the light-icon or dark-icon class for the corresponding theme, and override the generated CSS variables with arbitrary properties:
<IgbIcon IconName="search" Collection="material" Class="!light-icon ![--color:#7B9E89] ![--size:48px]" />
The exclamation mark (!) gives the Tailwind utility precedence over the default theme styles of the Icon.
Accessibility
The Blazor Icon is a non-interactive visual that either decorates nearby content or conveys meaning on its own.
Keyboard Interaction
The Blazor Icon doesn’t receive focus and has no keyboard interaction: it sets no tabindex and handles no keyboard or pointer events. Use the Icon Button when users must activate an icon with the keyboard.
| Key | Action |
|---|---|
| n/a | The Icon is not keyboard interactive. |
Screen Readers / ARIA
The Blazor Icon makes no announcements of its own. What assistive technology reports depends on the accessible name of the icon:
- A
<title>element that is a direct child of the root<svg>of the registered icon becomes the accessible name of the icon. - An
aria-labelattribute on the icon names an icon whose SVG has no title, and takes precedence over the title. - An
aria-hidden="true"attribute hides a decorative icon from assistive technology.
Accessibility Compliance
Infragistics documents Ignite UI for Blazor accessibility support for Section 508 and WCAG 2.1 guideline areas in the Accessibility Compliance topic.
| Criterion | How the component complies |
|---|---|
| 1.1.1 Non-text Content | The icon uses the aria-label that you set, or the <title> of the registered SVG, as its text alternative. |
Your responsibilities:
- Give every icon that conveys meaning on its own an accessible name, and keep decorative icons hidden. See Accessible Name.
- Keep a contrast ratio of at least 3:1 between an icon that conveys information and its background, as 1.4.11 Non-text Contrast requires, especially when you override the theme color.
- Use an Icon Button, not an Icon, for actions that users activate.
- Set
mirroredon directional icons in right-to-left layouts so that their direction matches the reading order.
Troubleshooting
Use these answers when a Blazor Icon doesn’t render or doesn’t look as expected.
Why is my icon empty?
The Blazor Icon draws only an SVG that is registered under the same name and collection. Check the following causes:
- The name or the collection doesn’t match the registration exactly. The collection defaults to
default. - The SVG markup is malformed. Registration throws the
SVG element not found or malformed SVG string.error when the text has no root<svg>element or doesn’t parse. - The request for an SVG file failed. The registration from a URL fails for a non-OK response, so check the browser console and the network panel. A file on another origin also needs a server that allows cross-origin requests.
Why doesn’t my SVG icon take the theme color?
The Blazor Icon applies its color to the root <svg> element, and the child elements inherit it as their fill. A child element with a hardcoded color keeps that color:
<svg viewBox="0 0 24 24">
<path fill="#050d42" d="..." />
</svg>
Remove hardcoded colors from the child elements, or set the color on the root <svg> element, where the theme overrides it. To color part of an icon on purpose, use a theme color variable, such as fill="var(--ig-primary-500)".
Why does a tooltip appear when I hover an icon?
Browsers show the <title> element of an SVG as a native tooltip.
Remove the <title> element from the SVG markup before you register it, and set aria-label on icons that need an accessible name.
Known Limitations
The Blazor Icon has the following limitations:
- The Icon is a non-interactive visual: it doesn’t receive focus or handle keyboard input.
- Theme colors reach the root
<svg>element and the child elements that inherit its fill. Child elements with hardcoded colors keep them. - The Icon draws registered SVG icons only. Icon fonts and ligatures aren’t supported.
API References
Dependencies
The Blazor Icon requires a theme stylesheet to apply its theme-based size and color. See the framework-specific setup in Getting Started.
Additional Resources
The following resources provide more Blazor Icon guidance and project support:
Related Components
The Blazor Icon often appears inside other components. Use these components when an icon needs to do more than display a symbol:
- Icon Button - Use Icon Button when users click or press the icon to run an action.
- Avatar - Use Avatar to represent a person or entity with an icon, initials, or an image.
- Badge - Use Badge to show a short status or count next to another element.
FAQ
These questions cover common Blazor Icon setup, accessibility, and usage scenarios.
No. The Blazor Icon draws SVG icons that you register by name, so register each Material icon that you need from its SVG markup or file. The Blazor Icon doesn’t support icon fonts or ligatures.
Call the RegisterIconAsync or RegisterIconFromTextAsync method of a referenced IgbIcon, then set the same IconName and Collection on the Blazor Icon. All Blazor Icons on the page share one registry, so the registered icon is available everywhere.
Give a Blazor Icon that conveys meaning on its own an accessible name with aria-label, or register an SVG that contains a <title> element. Hide a decorative Blazor Icon next to visible text with aria-hidden="true".
The Blazor Icon doesn’t receive focus or handle keyboard input, so a click handler on it excludes keyboard users. Use the Blazor Icon Button for actions, because it places an icon inside a focusable button.
Yes. Set the mirrored property of the Blazor Icon to flip a directional icon, such as an arrow, horizontally in a right-to-left layout. Non-directional icons don’t need mirroring.