CSS Icons


🔹 What are CSS Icons?

Icons in CSS are small graphical elements that improve visual communication on a webpage. They can be implemented using:

  • Icon Fonts (like Font Awesome)

  • Inline SVG

  • Unicode characters

  • CSS background-images


🔸 1. Using Font Awesome (Icon Fonts)

🔹 Step 1: Include Font Awesome CDN
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
🔹 Step 2: Use the Icon
<i class="fas fa-home"></i>
<i class="fa-solid fa-envelope"></i>

🟢 These are scalable, color-changeable icons using CSS.


🔸 2. Styling Font Awesome Icons with CSS

i {
  font-size: 24px;
  color: darkblue;
  margin-right: 10px;
}

🔸 3. Using SVG Icons

Inline SVG Example:
<svg width="24" height="24" fill="green" xmlns="http://www.w3.org/2000/svg">
  <circle cx="12" cy="12" r="10"/>
</svg>

🟢 Inline SVGs can be styled with fill, stroke, width, height.


🔸 4. Using Unicode Icons

You can use symbols like:

<span class="icon">&#9733;</span> <!-- star symbol -->
.icon {
  color: gold;
  font-size: 20px;
}

🔸 5. Using Background Images as Icons

<div class="icon-user"></div>
.icon-user {
  width: 24px;
  height: 24px;
  background-image: url('user-icon.png');
  background-size: cover;
}

🟢 Useful when using custom PNG/SVG images as icons.


Practice Questions

Q1. Add a home icon using Font Awesome.

Q2. Create a delete (trash) icon and style it red.

Q3. Use an inline SVG circle and color it blue.

Q4. Add a star symbol using Unicode.

Q5. Display a user icon as a background image in a div.

Q6. Increase the size of icons on hover.

Q7. Add margin between icon and text.

Q8. Style all <i> icons to have size 20px and grey color.

Q9. Use a heart icon beside a button label.

Q10. Add an envelope icon before email text.


Go Back Top