Modern web interfaces use icons for navigation bars, alert banners, search inputs, and status badges. While older projects relied on icon fonts (i tags with custom font files), modern CSS pairs inline SVG icons with Flexbox alignment and currentColor for sharper edges, smaller payloads, and reliable accessibility.
Inline SVGs inherit the parent button or link's color automatically when you set stroke="currentColor" or fill="currentColor". Sizing icons with width: 1.125em; height: 1.125em; scales each icon proportionally whenever the button text size changes.
.icon-btn {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 10px 16px;
background-color: #1e293b;
color: #f8fafc;
border-radius: 6px;
border: none;
cursor: pointer;
}
.icon-btn svg {
width: 18px;
height: 18px;
flex-shrink: 0;
stroke: currentColor;
}
.icon-btn:hover {
background-color: #2563eb;
}
Every icon falls into one of two categories:
aria-hidden="true" on the svg or i so screen readers do not announce gibberish.X or menu hamburger). Add aria-label="Close modal" on the button and aria-hidden="true" on the icon inside.vertical-align hacks: Setting display: inline-flex; align-items: center; gap: 8px; on the parent button centers icons vertically every time.flex-shrink: 0 on icons inside narrow buttons: Without it, long button text squishes the SVG horizontally.Inline SVGs render crisply without font-loading flashes (FOIT), support multi-color paths, and avoid screen reader glitches when custom fonts fail to download.
Use CSS mask-image: url("icon.svg"); background-color: currentColor; so the SVG acts as a stencil filled by your active CSS text color.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.