CSS Icons
1. Introduction to CSS Icons
Icons are small graphical symbols that represent actions, objects, or information. CSS allows you to style icons by changing their color, size, spacing, background, and effects.
Popular Icon Libraries
- Font Awesome
- Bootstrap Icons
- Google Material Icons
2. Font Awesome Icons
Font Awesome is one of the most popular icon libraries. It contains thousands of free and premium icons.
Step 1: Include Font Awesome
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
Step 2: Use Icons
| Icon | Purpose |
|---|---|
| fa-house | Home |
| fa-user | User Profile |
| fa-envelope | |
| fa-phone | Phone |
| fa-cart-shopping | Shopping Cart |
3. Bootstrap Icons
Bootstrap Icons are a free, open-source icon library designed especially for Bootstrap projects.
Include Bootstrap Icons
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
Examples
| Bootstrap Icon | Use |
|---|---|
| bi-house-fill | Home |
| bi-search | Search |
| bi-person-fill | User |
| bi-envelope-fill |
4. Google Material Icons
Google Material Icons follow Google's Material Design guidelines and provide clean, modern icons.
Include Material Icons
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
Examples
| Material Icon | Purpose |
|---|---|
| home | Home |
| search | Search |
| phone | Phone |
5. Changing Icon Size
Since icons are treated like text, their size can be changed using the font-size property.
i{
font-size:40px;
}
Example
.home{
font-size:24px;
}
.search{
font-size:36px;
}
.cart{
font-size:48px;
}
| Size | Example |
|---|---|
| 16px | Small Icon |
| 24px | Medium Icon |
| 36px | Large Icon |
| 48px | Extra Large Icon |
6. Changing Icon Color
Icons can be colored using the color property, just like normal text.
i{
color:red;
}
Different Colored Icons
.home{
color:blue;
}
.user{
color:green;
}
.phone{
color:orange;
}
.email{
color:purple;
}
| Color | Example Use |
|---|---|
| Blue | Information |
| Green | Success |
| Red | Error / Warning |
| Orange | Notifications |
7. Icon Hover Effects
Hover effects make icons interactive and improve the user experience.
i{
color:black;
transition:0.3s;
}
i:hover{
color:red;
}
Hover with Size Effect
.icon{
font-size:30px;
transition:0.3s;
}
.icon:hover{
color:blue;
transform:scale(1.2);
}
8. Icon Background & Border
Icons can have backgrounds, borders, padding, and rounded corners.
.icon{
background:#0d6efd;
color:white;
padding:12px;
border-radius:50%;
}
Border Example
.icon{
border:2px solid green;
padding:10px;
border-radius:8px;
}
| Property | Purpose |
|---|---|
| background | Adds background color. |
| padding | Adds spacing around the icon. |
| border | Adds a border. |
| border-radius | Makes square or circular icons. |
9. Social Media Icons
Social media icons help users quickly access your social networking profiles.
Styled Social Icons
.social{
font-size:32px;
color:#0d6efd;
margin:10px;
}
.social:hover{
color:red;
}
10. Practical Icon Examples
Example 1: Search Button
Example 2: Contact Information
+91-9876543210
info@example.com
Example 3: Navigation Menu
Home
Profile
Settings
Example 4: Shopping Cart
Cart (5)
11. CSS Icons Best Practices
Well-designed icons improve navigation, readability, and the overall user experience of a website.
- Use icons that clearly represent their purpose.
- Keep icon sizes consistent throughout the website.
- Use colors that match your website theme.
- Maintain equal spacing between icons.
- Add hover effects to improve interactivity.
- Use SVG or icon fonts for better scalability.
- Do not overuse icons where text is more appropriate.
- Always provide meaningful labels for important icons.
12. Common Icon Mistakes
| Mistake | Correct Practice |
|---|---|
| Using too many different icon styles. | Use one icon library consistently. |
| Different icon sizes. | Maintain consistent sizing. |
| Poor color contrast. | Choose colors with good visibility. |
| No hover effect. | Add hover effects for clickable icons. |
| Icons without labels. | Add text labels whenever needed. |
❌ Incorrect
.icon{
font-size:20px;
color:yellow;
}
✔ Better
.icon{
font-size:28px;
color:#0d6efd;
}
13. Browser Support
Modern icon libraries are supported by all major web browsers.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Supported |
| Mozilla Firefox | ✔ Supported |
| Microsoft Edge | ✔ Supported |
| Safari | ✔ Supported |
| Opera | ✔ Supported |
14. CSS Icons Interview Questions
- What are CSS icons?
- Name three popular icon libraries.
- How do you include Font Awesome in a webpage?
- How do you change an icon's size?
- Which CSS property changes an icon's color?
- How can hover effects be added to icons?
- Why are icon libraries preferred over image icons?
- What are Bootstrap Icons?
- What are Google Material Icons?
- Why should icon sizes remain consistent?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Icons.
- ✔ Font Awesome Icons.
- ✔ Bootstrap Icons.
- ✔ Google Material Icons.
- ✔ Changing Icon Size.
- ✔ Changing Icon Color.
- ✔ Hover Effects.
- ✔ Background & Border Styling.
- ✔ Social Media Icons.
- ✔ Practical Icon Examples.
- ✔ Best Practices and Common Mistakes.
Quick Quiz
Which CSS property is commonly used to change the size of an icon?