CSS Icons
Icons make interfaces faster to scan: a magnifying glass for search, a trash can for delete. There are several ways to put icons on a page, and CSS can style all of them — size, colour, spacing and animation.
Ways to add icons
| Technique | Pros | Cons |
|---|---|---|
| Emoji | Zero setup, colourful | Look differs per OS, can't recolour |
| Inline SVG | Sharp at any size, colour with CSS | More markup |
SVG in a data URI (background-image) |
Keeps HTML clean | Colour fixed inside the SVG |
| Icon fonts (e.g. Font Awesome, Material Icons) | Easy <i class="…"> usage |
Extra download, accessibility quirks |
| Pure CSS shapes | No assets at all | Only simple shapes |
Icon libraries are loaded with a <link> to their stylesheet, then used via class names:
<!-- Example only: loads a third-party icon font -->
<link rel="stylesheet" href="https://example-cdn/icon-library.css">
<i class="icon icon-home"></i>The examples below use techniques that need no external files.
Emoji icons
Emoji are text, so font-size controls their size. Wrapping them in a span lets you add spacing and accessibility text.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.menu a { display: inline-flex; align-items: center; gap: 6px; padding: 8px 12px; margin: 4px;
background: #eef3ff; border-radius: 8px; text-decoration: none; color: #1a237e; }
.menu .ico { font-size: 22px; }
.big { font-size: 60px; }
</style>
</head>
<body>
<nav class="menu">
<a href="#"><span class="ico" aria-hidden="true">🏠</span> Home</a>
<a href="#"><span class="ico" aria-hidden="true">🔍</span> Search</a>
<a href="#"><span class="ico" aria-hidden="true">⚙️</span> Settings</a>
</nav>
<p class="big">☀️ 🌧️ ❄️</p>
</body>
</html>Inline SVG icons
SVG icons scale perfectly. If the SVG uses fill="currentColor", it automatically takes the CSS color of its parent — so one icon can be recoloured anywhere.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 20px; align-items: center; padding: 20px; }
.icon { width: 1em; height: 1em; vertical-align: -0.125em; }
.red { color: #e53935; font-size: 24px; }
.blue { color: #1e88e5; font-size: 40px; }
.green { color: #43a047; font-size: 64px; }
.red:hover, .blue:hover, .green:hover { color: #ff9800; }
</style>
</head>
<body>
<span class="red"><svg class="icon" viewBox="0 0 24 24"><path fill="currentColor" d="M12 21s-7-4.5-9.5-9A5.5 5.5 0 0 1 12 6a5.5 5.5 0 0 1 9.5 6C19 16.5 12 21 12 21z"/></svg> 24px</span>
<span class="blue"><svg class="icon" viewBox="0 0 24 24"><path fill="currentColor" d="M12 21s-7-4.5-9.5-9A5.5 5.5 0 0 1 12 6a5.5 5.5 0 0 1 9.5 6C19 16.5 12 21 12 21z"/></svg></span>
<span class="green"><svg class="icon" viewBox="0 0 24 24"><path fill="currentColor" d="M12 21s-7-4.5-9.5-9A5.5 5.5 0 0 1 12 6a5.5 5.5 0 0 1 9.5 6C19 16.5 12 21 12 21z"/></svg></span>
</body>
</html>Tip: Sizing icons with
width: 1em; height: 1emmakes them match the surrounding text automatically.
SVG as a background image
A small SVG can be embedded directly in CSS as a data URI. Encode # as %23 inside the URL.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; padding: 20px; }
.search {
font-size: 16px; padding: 10px 12px 10px 40px; width: 240px;
border: 2px solid #90a4ae; border-radius: 20px;
background: white url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Ccircle cx='10' cy='10' r='6' fill='none' stroke='%23607d8b' stroke-width='2.5'/%3E%3Cpath d='M15 15l6 6' stroke='%23607d8b' stroke-width='2.5' stroke-linecap='round'/%3E%3C/svg%3E") no-repeat 12px center / 20px;
}
.done::before {
content: ""; display: inline-block; width: 18px; height: 18px; margin-right: 8px; vertical-align: -3px;
background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Ccircle cx='12' cy='12' r='11' fill='%2343a047'/%3E%3Cpath d='M7 12l3 3 7-7' stroke='white' stroke-width='2.5' fill='none'/%3E%3C/svg%3E");
}
</style>
</head>
<body>
<input class="search" placeholder="Search…">
<p class="done">Task completed</p>
<p class="done">Another task</p>
</body>
</html>Pure CSS icons
Simple icons — a hamburger menu, a close "×", an arrow — can be drawn with borders, pseudo-elements and transforms.
<!DOCTYPE html>
<html>
<head>
<style>
body { display: flex; gap: 40px; align-items: center; padding: 30px; }
.burger { width: 36px; height: 6px; background: #333; border-radius: 3px; position: relative; }
.burger::before, .burger::after { content: ""; position: absolute; left: 0; width: 36px; height: 6px; background: #333; border-radius: 3px; }
.burger::before { top: -12px; }
.burger::after { top: 12px; }
.close { width: 36px; height: 36px; position: relative; }
.close::before, .close::after { content: ""; position: absolute; top: 15px; left: 0; width: 36px; height: 6px; background: #d32f2f; border-radius: 3px; }
.close::before { transform: rotate(45deg); }
.close::after { transform: rotate(-45deg); }
.arrow { width: 24px; height: 24px; border-right: 6px solid #1976d2; border-bottom: 6px solid #1976d2; transform: rotate(-45deg); }
</style>
</head>
<body>
<div class="burger" title="menu"></div>
<div class="close" title="close"></div>
<div class="arrow" title="next"></div>
</body>
</html>Accessibility
Decorative icons next to text should be hidden from screen readers with aria-hidden="true". Icon-only buttons need a text label, e.g. aria-label="Close", so everyone knows what they do.
Test yourself
How can an inline SVG icon follow the CSS color of its parent?
- fill="inherit-color"
- fill="currentColor"
- color: svg
- It always does automatically
What should an icon-only button include for accessibility?
- A bigger icon
- An aria-label describing the action
- A red border
- Nothing
Inside a CSS data URI, how should the # character in a colour be written?
- #
- %23
- &hash;
- \#