CSS Icons
CSS icons are symbols used in websites for navigation, actions, or decoration. Instead of using image files, CSS allows icons to be scalable, lightweight, and easily stylable.
Types of CSS Icons
Icon Fonts
Icon fonts are fonts that contain symbols instead of letters. Popular icon fonts include:
- Font Awesome
- Material Icons
- Ionicons
Example:
<i class="fa fa-home"></i>
.fa-home {
font-size: 30px;
color: #FF5733;
}
SVG Icons
SVG (Scalable Vector Graphics) icons are vector images that remain sharp at any size. They can be embedded inline or used as external files.
Advantages:
- Resolution independent
- Animatable with CSS (
transform
,fill
,stroke
) - Accessible (ARIA labels for screen readers)
Example:
<svg width="40" height="40" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2">
<path d="M3 12l9-9 9 9M4 10v10h16V10"/>
</svg>
svg:hover {
stroke: #FF0000;
transform: scale(1.2);
}
Image-Based Icons
These are traditional icons using PNG, GIF, or JPG images. Less common now but still useful for very detailed or complex icons.
Using Icon Fonts in CSS
How to Include Icon Fonts
- Use a CDN (like Font Awesome)
- Or include local font files in your project
Basic Example:
<link rel="stylesheet" href="<https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css>">
<i class="fa fa-user"></i>
Styling Icon Fonts
.fa-user {
font-size: 40px;
color: #3498db;
transition: transform 0.3s;
}
.fa-user:hover {
transform: scale(1.2);
color: #e74c3c;
}
Using SVG Icons in CSS
Inline vs External SVG
- Inline: Directly in HTML; fully stylable and animatable.
- External: Referenced as
<img>
orbackground-image
; limited styling.
Styling & Animating SVG
svg {
width: 50px;
height: 50px;
transition: transform 0.3s, fill 0.3s;
}
svg:hover {
transform: rotate(15deg);
fill: #ff6347;
}
Styling Icons with CSS
CSS provides many ways to style icons:
- Color:
color
for icon fonts,fill
/stroke
for SVG - Size:
font-size
for fonts,width
/height
for SVG - Shadows & Glow:
text-shadow
orfilter: drop-shadow()
- Hover Animations: Scale, rotate, bounce, or color transition
Example:
.icon {
font-size: 35px;
color: #2ecc71;
transition: transform 0.3s, color 0.3s;
}
.icon:hover {
transform: scale(1.3);
color: #27ae60;
}
Customizing Icons for UI Design
- Match Brand Colors: Ensure icons align with your website’s theme
- Consistent Size & Spacing: Maintain UI balance
- Interactive Effects: Hover, click, or animation effects
- Accessibility: Add
aria-label
ortitle
for screen readers
Example:
<i class="fa fa-bell" aria-label="Notifications"></i>
CSS Icon Effects
- Rotation & Bounce: Adds interactivity
- Neon Glow: Using multiple shadows
- Transitions: Smooth hover effects
- Layered Shadows: Creates depth
Example: Neon Glow
.fa-star {
color: yellow;
text-shadow: 0 0 5px #ff0, 0 0 10px #ff0;
}