Working with Icons
1. Using IonIcon Component
| Property | Values | Description |
|---|---|---|
| name | string |
Name of the icon from the Ionicons library |
| size | small | medium | large |
Size of the icon |
| color | primary | secondary | success | warning | danger | light | medium | dark |
Color of the icon |
2. Using Ionicons Library
| Property | Values | Description |
|---|---|---|
| name | home |
The icon name to render from the Ionicons library |
| size | medium, small, large |
Adjusts the size of the icon |
| color | primary, secondary, danger |
Sets the color of the icon using Ionic's color system |
Note: Ionicons are SVG-based and support dynamic styling via CSS.
3. Using Platform-Specific Icons
Example: Using Platform-Specific Icons
<ion-icon name="logo-ionic" platform="ios"></ion-icon>
<ion-icon name="logo-ionic" platform="md"></ion-icon>
| Property | Values | Description |
|---|---|---|
| name | string |
Name of the icon to use |
| platform | ios | md | android |
Platform-specific icon variant to use |
Note: Platform-specific icons are supported in Ionic 6+.
4. Setting Icon Size
| Property | Values | Description |
|---|---|---|
| size | small, medium, large |
Defines the size of the icon |
| style | solid, outline |
Defines the style of the icon |
Example: Setting Icon Size
<ion-icon name="heart" size="large"></ion-icon>
<ion-icon name="heart" size="medium"></ion-icon>
<ion-icon name="heart" size="small"></ion-icon>
5. Setting Icon Color
Icons in Ionic can be customized with different colors using CSS classes or inline styles.
| Property | Values | Description |
|---|---|---|
| color | primary, secondary, tertiary, success, warning, danger, light, medium, dark |
Predefined color variants for Ionic icons |
| style | color: #ff0000; |
Custom color using inline CSS |
6. Using Custom SVG Icons
Example: Adding a Custom SVG Icon
<!-- src/icons/my-icon.svg -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M12 2L2 7l10 5 10-5-10-5z"></path>
</svg>
| Property | Description | Usage |
|---|---|---|
| icon | Name or path to custom SVG icon | <ion-icon name="my-icon"></ion-icon> |
Note: Custom SVG icons must be added to the
src/icons directory and registered in your Ionic project's configuration.
7. Using Icon Slots
Icon slots in Ionic allow you to customize the placement and appearance of icons within components like buttons, cards, and lists. You can use slots to inject custom icons or modify existing ones.
| Slot | Description | Usage Example |
|---|---|---|
| start | Places the icon at the beginning of the component. | |
| end | Places the icon at the end of the component. | |
| icon | Used to define an icon inside a component like ion-button or ion-card. | |
Example: Custom Icon in Button
<ion-button>
<ion-icon slot="start" name="star"></ion-icon>
<ion-label>Save</ion-label>
</ion-button>
Note: The
slot attribute is used to position the icon within the component structure. It must be placed inside the component that supports it.
8. Implementing Icon Buttons
| Property | Values | Description |
|---|---|---|
| icon | string |
Name of the icon to display (from Ionicons library) |
| size | small | medium | large |
Size of the icon button |
| color | primary | secondary | success | warning | danger | light | dark |
Color theme for the button |
Note: Icon buttons should have accessible labels for screen readers. Use
aria-label or aria-labelledby to provide context.
Example: Icon Button with Label
<ion-button aria-label="Add to favorites">
<ion-icon name="heart-outline"></ion-icon>
<span>Favorite</span>
</ion-button>
9. Using Lazy Loading Icons
Example: Lazy Load Icon in Ionic
import { IonIcon } from '@ionic/react';
import { home } from 'ionicons/icons';
// Lazy load icon
const MyComponent = () => {
return <IonIcon icon={home} lazy={true} />;
};
| Property | Values | Description |
|---|---|---|
| icon | string | IonIcon |
The icon name or IonIcon object to display |
| lazy | true | false |
Enables lazy loading of the icon |
Note: Lazy loading icons improves initial load performance by deferring icon rendering until needed.
10. Creating Icon Libraries
Example: Icon Library Structure
// Example directory structure for an icon library
// src/icons/
// ├── index.js
// ├── icon-set.js
// ├── icons/
// │ ├── home.svg
// │ ├── user.svg
// │ └── settings.svg
// └── styles.css
| Feature | Description | Usage |
|---|---|---|
| iconSetName | ionicons |
Name of the icon set to use |
| iconPath | ./src/icons |
Directory where icons are stored |
| iconFormat | svg |
File format of the icons (e.g., svg, png) |
Note: Ensure all SVG icons are optimized for web use to reduce bundle size.
Example: Registering an Icon Set
import { Ionicons } from 'ionicons';
import { addIcons } from 'ionicons/components';
// Register custom icon set
addIcons({
home: require('./icons/home.svg'),
user: require('./icons/user.svg'),
settings: require('./icons/settings.svg')
});
// Use in component
<ion-icon name="home"></ion-icon>