CSS icons are small visual elements used to improve the look and usability of a web page. You can create them using pure CSS shapes or by using icon libraries like Font Awesome. They are scalable, customizable, and easy to style with regular CSS properties.
font-size or transform.color, margin, and hover work on icons.aria-label to describe the meaning of icons.There are two popular ways to use icons in CSS:
width, height, border-radius, and ::before / ::after pseudo-elements.<i> tag with proper classes like fas fa-heart. Style them using normal CSS.This example builds a heart icon using a rotated square and two circles created with pseudo-elements.
/* Heart icon using pure CSS shapes */
.heart {
width: 60px;
height: 60px;
position: relative;
transform: rotate(-45deg);
background-color: red;
margin: 0 auto;
}
.heart::before,
.heart::after {
content: '';
width: 60px;
height: 60px;
position: absolute;
border-radius: 50%;
background-color: red;
}
.heart::before {
left: 30px;
top: 0;
}
.heart::after {
top: -30px;
left: 0;
}
The base square is rotated -45deg and the circles on top form the upper curves of the heart. Because everything is CSS-based, the icon remains sharp at any size.
Font Awesome provides a huge collection of ready-made icons. You just include the CDN link and use the appropriate class names.
<head> of your HTML file.<i> elements with icon classes like fas fa-heart.font-size, color, and other CSS properties.
<!-- Font Awesome icons with basic styling -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
<i class="fas fa-heart" style="font-size: 40px; color: red;"></i>
<i class="fas fa-thumbs-up" style="font-size: 40px; color: blue; margin-left: 10px;"></i>
<i class="fas fa-camera" style="font-size: 40px; color: green; margin-left: 10px;"></i>
Each icon is just an inline element that behaves like text. You can change its size with font-size, color with color, and spacing with margin.
These are some of the most frequently used properties when styling icons:
| Property | Description | Example Value |
|---|---|---|
font-size |
Controls the size of font-based icons. | 40px, 1.8rem |
color |
Sets the icon color. | red, #ff5733 |
margin |
Adds space around icons. | 10px, 0 8px |
transition |
Animates changes like hover effects. | 0.2s ease |
transform |
Used for scaling, rotating, or moving icons. | scale(1.2), rotate(10deg) |
/* Simple hover effect for Font Awesome icons */
.icon-row i {
font-size: 40px;
margin: 0 8px;
transition: transform 0.2s ease, color 0.2s ease;
}
.icon-row i:hover {
transform: scale(1.2);
color: #2563eb;
}
font-size to scale vector icons without losing quality.transition and :hover effects to make icons feel interactive.aria-label or title attributes to describe icons for screen readers.font-size, color, and margin to style them consistently.aria-label attributes.