Home > Article > Web Front-end > CSS Icons with examples
Icons can easily be added to our HTML page, by using an icon library.
The simplest way to add an icon to your HTML page is with an icon library, such as Font Awesome.
Add the name of the specified icon class to any inline HTML element (like or ).
CSS icons are symbols or graphical representations that are created using
CSS (Cascading Style Sheets) rather than traditional image formats like PNG or SVG.
They are often used in web design to add visual elements to a website without relying on image files.
There are a few common methods for creating CSS icons:
These are icons created from special icon fonts such as Font Awesome, Material Icons, or Ion icons. These fonts contain a set of glyphs (symbols) that can be styled with CSS.
you might use a class like .fa-heart to add a heart icon to your HTML, and then style it with CSS properties.
Icons can be created using pure CSS by styling HTML elements (like
Include Font Awesome in your project:
Add this line to the
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
To use an icon, add a or element with the appropriate classes:
<i class="fas fa-camera"></i>
Include Material Icons in your project:
Add this line to the
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
To use an icon, add a element with the class material-icons and the icon name:
<i class="material-icons">camera_alt</i>
You can also create your own icons with CSS. Here’s a simple example using pure CSS:
<div class="icon-star"></div>
.icon-star { display: inline-block; width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 100px solid gold; position: relative; transform: rotate(35deg); } .icon-star:before { content: ''; position: absolute; top: 0; left: -50px; width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 100px solid gold; transform: rotate(-70deg); }
This CSS snippet creates a simple star shape using borders and positioning.
You can also use SVGs for high-quality icons:
<svg width="24" height="24" viewBox="0 0 24 24"> <path d="M12 2L2 7v10l10 5 10-5V7z"/> </svg>
.icon { width: 24px; height: 24px; background: url('data:image/svg+xml;base64,...') no-repeat center center; background-size: contain; }
Size and Color: For font icons and inline SVGs, you can adjust the size with font-size or the width and height properties, and change the color with color or fill for SVGs.
Accessibility: Always consider accessibility by adding descriptive text or aria attributes where needed.
Feel free to experiment and mix different methods to find what works best for your project!
The above is the detailed content of CSS Icons with examples. For more information, please follow other related articles on the PHP Chinese website!