Home / Guides / How to Use SVG Icons in HTML and CSS

How to Use SVG Icons in HTML and CSS

GUIDE · 4 MIN READ

Three ways to add an SVG icon

There are three ways to put an SVG icon on a web page. Inline SVG means pasting the icon code straight into your HTML. It gives you full control over colour, size, and animation with CSS. The img tag means saving the SVG as a file and loading it with an <img> tag that points at the saved file, just like a picture. It is simple and cacheable, but CSS cannot reach inside the file to change its colour. A CSS background sets the SVG as a background image for decoration, with no accessible label. For navigation, buttons, and anything that changes colour on hover or in dark mode, use inline SVG. Start with inline, and switch to img only to reuse the same file across many pages.

Method 1: inline SVG, step by step

Follow these five steps to place an icon inline. 1. Download the SVG from Icondusk and open it in a text editor. 2. Copy the whole file, from the opening tag to the closing tag. 3. Paste it into your HTML where the icon belongs, for example inside a button or a navigation link. 4. Give the svg a class so CSS can target it, such as <svg class="icon" ...>. 5. Add width and height in CSS, not in the tag. A good starting rule is .icon { width: 20px; height: 20px; }. Keep the viewBox attribute, because it lets the icon scale cleanly, and remove any fixed width and height attributes, so CSS owns the size.

Method 2: the img tag

When inline is more than you need, the img tag is the quickest path. Save the downloaded SVG in an icons folder and reference it with <img src="/icons/shopping-ecommerce/magnifying-glass-search.svg" alt="" width="20" height="20">. The browser caches the file, and the width and height attributes stop the page shifting while it loads. Two limits to know: CSS cannot change the colour of an icon loaded this way, so download it in the final colour from the Icondusk colour picker first. A screen reader treats the img as a picture, so leave the alt empty when the nearby text already says what the icon is. For a standalone icon, write a short alt, such as alt="Search". Our guide to free icons for websites compares inline and img in more depth.

Recolour with currentColor

The currentColor trick is the reason inline SVG is so popular. In the pasted SVG code, replace the baked-in colour value with the word currentColor, for example change stroke="#17132b" to stroke="currentColor", and do the same for any fill. From then on, the icon takes whatever colour the CSS color property sets on its parent. A button with color: #fff shows a white icon, and on hover with color: #f6a821 it shows a gold icon, with no extra files. Dark mode comes free too, because the icons follow the text colour. This works on recolourable styles such as Outline and Filled, but not on styles with designed colours, such as Flat and Neon, whose colours are the artwork, so keep those as downloaded. CSS variables can retheme the whole site from one line. Our guide to changing icon colors walks through the edit in detail.

Size and align icons

Icons look wrong most often because of size and alignment, not because of the icon itself. Set one standard size per context: 16 pixels beside body text, 20 pixels in buttons and navigation, 24 pixels in cards and section headers. Match the icon size to the text it sits beside, so a 16 pixel icon pairs with 16 pixel text. For alignment, make the button or link display: inline-flex; align-items: center; gap: 8px; and the icon centres itself against the label. Never set a width without a height, or the icon stretches, and never mix sizes in one row.

Three practical examples

First, a navigation menu: place a 20 pixel inline icon before each link label, Home, Search, Cart, and Account, using icons from the Shopping & Checkout set or the Business & Office set, all from the same style. Set the link colour in CSS and the icons follow with currentColor. Second, a button with a download icon: build the button as inline flex with a gap of 8 pixels, icon first, label second, and a hover rule that changes the colour. Third, a feature list: put a small check or star icon before each bullet, all 16 pixels, all one colour, and the list scans cleanly. In all three examples, the icons come from one set, so the stroke weight matches across the page.

Keep it fast and accessible

Two habits keep icon-heavy pages fast and usable. For speed, reuse the same inline icon with a symbol sprite: define each icon once inside a hidden <svg> with <symbol> elements, then place it with <use href="#search">. The browser downloads the shape once and draws it everywhere. For accessibility, hide decorative icons from screen readers with aria-hidden="true" on the svg tag, and give a real label to icons that work alone, such as a search button with no visible text, by adding aria-label="Search" to the button. Finally, test on a dark background and at 200 percent zoom.

Every icon mentioned in our guides is free to download from the sets page, SVG and PNG, no attribution required.

Keep reading

More practical guides from the library.