CSS Icons are graphical symbols used in webpages to represent actions,
objects, or information visually. Icons improve user experience by making
buttons, menus, navigation links, and interfaces easier to understand.
CSS can style icons by changing their size, color, spacing, alignment,
and effects.
Why Use CSS Icons?
Icons help reduce the need for extra text and make websites look more
modern. They are commonly used in navigation menus, social media buttons,
search bars, forms, dashboards, and mobile applications.
Types of Icons Used in Websites
Icon Type
Example
Description
Font Icons
Font Awesome
Icons provided as scalable fonts.
SVG Icons
SVG Images
Vector icons that can be styled with CSS.
Image Icons
PNG/JPG
Regular image files used as icons.
CSS Created Icons
Shapes
Icons created using CSS properties.
Basic CSS Icon Syntax
What this code does: Shows a blue star symbol at 50px with a short caption under it, styled like normal text.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the basic icon syntax demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo styles a text symbol and its caption. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
A text symbol is a character, so it is sized like text. font-size: 50px makes the star 50 pixels tall instead of the normal 16px. Because it is a font glyph, it stays sharp at any size.
color: blue colors the symbol exactly like text. line-height: 1.2 keeps the line box only slightly taller than the symbol, so there is no large empty space around the star. The } closes the rule.
The caption sits below the icon. margin-top: 15px separates it from the star, and color: #333 makes the text dark gray, which is easier on the eyes than pure black.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: the icon and its caption.
★ is a numeric character reference. &# starts it, 9733 is the Unicode number of the black star, and ; ends it. The browser draws it as a character, so no image file or icon library is needed. The div with the demo-icon class applies the size and color.
A normal paragraph that explains what the symbol is. Pairing icons with text helps all users, and screen readers can read the caption.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 1: Using Text Symbols as Icons
Simple symbols available in HTML can be styled using CSS. You can change their size, color, and alignment like normal text.
What this code does: Displays a large dark-red heart symbol (♥) by changing its font-size and color.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the text symbol icon demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo is similar to the basic example but uses a named HTML entity. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
font-size: 60px makes the heart 60 pixels tall. Change this value in the editor to see the symbol grow and shrink without losing quality.
#8B0000 is the hex code for dark red. Any CSS color works on a symbol, including names, rgb() and hsl(). line-height: 1.2 keeps the box close to the symbol size.
Adds 15px of space above the caption and makes its text dark gray.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: the heart and its caption.
♥ is a named character entity, which is easier to remember than the numeric ♥. It starts with & and ends with ;. Both forms produce the same character.
A paragraph describing the icon.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 2: Adding Font Awesome Icons
Font Awesome is a popular icon library that provides thousands of ready made icons. To use it, first add the Font Awesome CDN link and then use icon classes inside HTML elements.
What this code does: Loads the Font Awesome library from a CDN, then shows three blue 40px icons (house, user and heart) using its fa-solid classes.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the Font Awesome demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
This link loads the Font Awesome stylesheet from a CDN (a public file server). The file defines classes such as fa-solid and fa-house and loads the icon font. The URL contains version 6.5.0, so the icons stay the same. The line is split over two lines for readability, which is valid in HTML. An internet connection is required.
<style> opens the internal CSS. This demo has one rule for the icon class. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
Font Awesome icons are font characters, so font-size: 40px sets their size, exactly like text.
color: #007BFF colors every icon blue. margin-right: 15px spaces the icons apart, because the i elements are inline and sit on one line.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: three icons.
The element has three classes. fa-solid selects the solid style of Font Awesome, fa-house selects the house icon, and demo-icon is our own class for size, color and spacing. The i tag is empty, because the icon is drawn by the stylesheet.
Same structure. Only fa-user changes, which picks a different icon.
Same structure with fa-heart. To use another icon, replace the name part of the class with one from the Font Awesome catalog.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 3: Styling Icons Using CSS
CSS properties like color, font-size, background, padding, border-radius, and hover effects can be applied to icons to create attractive designs.
What this code does: Turns a star symbol into a round blue badge that darkens and grows slightly when you hover over it.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the styled icon badge demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo turns a symbol into a circular badge. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
display: inline-flex makes the badge a flex container that still sits inline. align-items: center and justify-content: center put the star in the exact middle, vertically and horizontally. Centering this way is more reliable than padding, because symbols have different widths.
A circle needs a square box. With the same width and height, border-radius: 50% makes a true circle. If the box were wider than it is tall, it would become an oval.
The star is 45px tall and white, drawn on a blue background. White on blue has strong contrast, so the icon is easy to see.
border-radius: 50% rounds each corner by half of the side. transition: .3s animates the hover changes over 0.3 seconds. line-height: 1 removes extra space above and below the symbol, so it is centered visually.
:hover applies while the mouse is over the badge. The background becomes a darker blue, and transform: scale(1.1) makes it 10% larger. The transition above makes both changes smooth.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one badge.
The star character inside a div. All the badge styling comes from the demo-icon class.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 4: Creating Icon Buttons
Icons are commonly used inside buttons to make actions easier to understand. CSS can combine icons with text and create modern buttons.
What this code does: Builds a green Submit button with a check mark icon placed before the text using a small right margin.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the icon button demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo styles a button and a small icon inside it. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
A green background suggests a positive action, and white text keeps it readable.
padding: 12px 20px makes the button roomy and easy to tap. border: none removes the browser’s default gray border, and border-radius: 5px rounds the corners slightly.
font-size: 18px makes the label and the icon larger. cursor: pointer shows the hand cursor over the button, which signals that it can be clicked.
A darker green when the mouse is over the button gives feedback that it reacts to the user.
The icon is in its own span, so it can have its own style. margin-right: 8px creates a gap between the check mark and the word Submit.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one button.
A button can contain more than text. The span holds the check mark (✓) and comes first, so it appears before the word Submit. The line breaks in the HTML become a single space, and the margin adds more space.
</body> closes the body.
</html> is the final closing tag and ends the document.
Complete CSS Icons Example
What this code does: Shows three round blue icons (star, heart and check mark) in a row that lift up and darken when you hover over them.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the icon row demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets the font and 24px of padding. background: #f5f9ff gives the preview a light blue color.
The container is a flex container, so the three icons sit side by side. gap: 20px separates them evenly. The gap also leaves room for the hover lift.
Each badge is a square of 80px, which becomes a circle with the radius below.
A blue background with white symbols for strong contrast. The hover state darkens this blue.
Each badge is also a flex container, which allows its symbol to be centered both vertically and horizontally.
font-size: 35px sizes the symbol. border-radius: 50% makes the square a circle. transition: .3s makes the hover change smooth.
translateY(-10px) moves the badge 10px up, as if it was lifted, without moving the others. The background changes to a darker blue.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: a row of three badges.
The flex container that holds the three badges.
A div with the demo-icon class. ★ is the star character.
Same class with the named entity ♥, which draws the heart.
Same class with ✓, the check mark. One class styles all three.
</div> closes the row.
</body> closes the body. The page has no scripts. All effects come from CSS.
</html> is the final closing tag and ends the document.
Advantages of CSS Icons
Advantage
Benefit
Description
Better Navigation
Easy Understanding
Icons quickly communicate actions.
Modern Design
Professional Look
Improves website appearance.
Scalable
Flexible Size
Icons can be resized without losing quality.
Lightweight
Faster Pages
Font icons usually require fewer resources.
Best Practices
Use icons that clearly represent their purpose.
Keep icon sizes consistent throughout the website.