How to Use ASP.NET Icons in Your Razor Views (with Examples)Icons improve usability, communicate actions quickly, and make interfaces feel polished. In ASP.NET (Core or Framework) Razor Views you can include icons in several ways—SVGs, icon fonts (Font Awesome, Bootstrap Icons), inline images, or CSS background images. This article covers strategies, accessibility, performance, and practical examples to help you choose and implement icons in Razor Views cleanly and maintainably.
Why icons matter in web apps
Icons:
- Enhance visual hierarchy and make primary actions more discoverable.
- Save space compared to text labels when used correctly.
- Improve scanability for users who skim pages.
- When implemented with accessibility in mind, icons can make interfaces clearer for assistive technologies.
Icon options and trade-offs
Approach | Pros | Cons |
---|---|---|
SVG (inline) | Sharp at all sizes, styleable with CSS, accessible, smaller for single icons | Can bloat HTML if many inline SVGs |
SVG (external sprite) | Single HTTP request for many icons, cacheable | Requires build step or sprite generator |
Icon font (e.g., Font Awesome) | Easy to size and color with CSS, many icons available | Accessibility pitfalls, hinting issues, not as crisp as SVG on some displays |
PNG/WebP/JPEG images | Simple, supported everywhere | Not scalable, needs multiple resolutions for clarity |
CSS background images / data-URI | Keeps markup clean | Harder to handle accessibility and inline styling |
Best practices
- Prefer SVG for crispness, accessibility, and CSS styling.
- Use inline SVG when you need to change colors or animate icons per instance.
- Use an SVG sprite or external file for many repeated icons to reduce HTML size.
- Ensure accessible names via aria-label, aria-hidden, or visually hidden text.
- Keep icon decorative only when they add no semantic meaning; mark them aria-hidden=“true”.
- Use semantic HTML: pair icons with buttons, links, or labels as needed.
- Optimize SVGs (SVGO) and serve compressed assets to save bandwidth.
Using SVGs in Razor Views
1) Inline SVG example (simple button)
Inline SVGs are great when you want full control over the icon’s appearance per instance.
Example Razor snippet:
<button type="button" class="btn btn-primary" aria-label="Save"> <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false"> <path fill="currentColor" d="M17 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V7z"/> <path fill="currentColor" d="M7 3v4h10V3z"/> </svg> <span class="sr-only">Save</span> </button>
Notes:
- Use fill=“currentColor” so the SVG inherits CSS color.
- Include an accessible label: either aria-label on the button or visually hidden text (.sr-only).
2) SVG partial view for reuse
Create a partial view for each icon to avoid repeating SVG markup.
Views/Shared/Icons/_save.cshtml:
@* _save.cshtml *@ @{ string title = ViewData["title"] as string; } <svg class="icon icon-save" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img" aria-hidden="@(string.IsNullOrEmpty(title) ? "true" : "false")" focusable="false"> <title>@title</title> <path fill="currentColor" d="M17 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V7z"/> <path fill="currentColor" d="M7 3v4h10V3z"/> </svg>
Use in a view:
@{ ViewData["title"] = "Save"; } @await Html.PartialAsync("Shared/Icons/_save")
Using SVG sprite sheets
SVG sprites reduce repetition by referencing symbols from a single file.
-
Create icons.svg with symbol definitions:
<svg xmlns="http://www.w3.org/2000/svg" style="display:none"> <symbol id="icon-save" viewBox="0 0 24 24"> <path d="M17 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V7z"/> <path d="M7 3v4h10V3z"/> </symbol> </svg>
-
Include the sprite file once (e.g., _Layout.cshtml) or serve externally.
-
Reference an icon in a Razor View:
<button class="btn" aria-label="Save"> <svg class="icon"> <use href="/images/icons.svg#icon-save" /> </svg> <span class="sr-only">Save</span> </button>
Notes:
- For older browsers, use xlink:href or ensure server serves proper MIME types.
- When the sprite is external, styling with currentColor still works if the symbol paths use currentColor.
Using icon fonts (Font Awesome / Bootstrap Icons)
Icon fonts are simple to integrate via CDN or npm packages.
-
Add stylesheet in _Layout.cshtml:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />
-
Use in Razor views:
<button class="btn btn-primary" aria-label="Edit"> <i class="fa-solid fa-pen" aria-hidden="true"></i> <span class="sr-only">Edit</span> </button>
Accessibility tips:
- Mark the (or ) as aria-hidden=“true” if the button has an accessible label.
- Avoid relying solely on icon fonts for semantic meaning.
Server-side helper: tag helper or HtmlHelper
Create a Tag Helper (ASP.NET Core) to inject icons consistently.
IconTagHelper.cs:
using Microsoft.AspNetCore.Razor.TagHelpers; [HtmlTargetElement("icon", Attributes = "name")] public class IconTagHelper : TagHelper { public string Name { get; set; } public string CssClass { get; set; } public override void Process(TagHelperContext context, TagHelperOutput output) { output.TagName = "svg"; output.Attributes.SetAttribute("class", $"icon icon-{Name} {CssClass}".Trim()); output.Attributes.SetAttribute("role", "img"); output.Attributes.SetAttribute("aria-hidden", "true"); output.Content.SetHtml($@"<use href=""/images/icons.svg#{Name}"" />"); } }
Use:
<icon name="save" css-class="text-primary"></icon>
Accessibility checklist
- Decorative icon only? Add aria-hidden=“true”.
- Icon conveys meaning? Provide accessible text via aria-label, visible label, or
inside SVG. - Ensure focusable elements (buttons/links) have clear labels.
- Test with screen readers and keyboard navigation.
Performance tips
- Combine icons into a sprite when many are used.
- Inline critical icons (above-the-fold) and lazy-load less important assets.
- Minify/optimize SVGs (SVGO) and compress responses (gzip/brotli).
- Cache external sprite or icon font via proper headers.
Examples: small gallery
-
Icon with label:
<a href="/profile" class="link"> <svg class="icon"><use href="/images/icons.svg#user" /></svg> Profile </a>
-
Icon button with tooltip:
<button class="btn" aria-label="Delete" title="Delete"> <svg class="icon"><use href="/images/icons.svg#trash" /></svg> </button>
-
Colored SVG via CSS:
.icon { width: 1em; height: 1em; vertical-align: -0.125em; } .icon.icon-alert { color: #d9534f; }
<svg class="icon icon-alert" aria-hidden="true"><use href="/images/icons.svg#alert" /></svg>
Tooling and workflows
- Use an icon management tool (IcoMoon, SvgSprite, Figma export) to build sprites.
- Automate optimization with SVGO in build pipelines.
- If using npm, install packages like @fortawesome/react-fontawesome (or CSS for Razor) or bootstrap-icons and serve only required assets.
Conclusion
Use SVGs by default for crisp, accessible icons. Choose inline SVGs for per-instance control and sprites or Tag Helpers for maintainability. Always include accessible text or mark decorative icons hidden from assistive tech, optimize assets, and test with real devices and screen readers.
Leave a Reply