• Icons
  • Docs
  • Lordicon
  • Lordicon
  • Pricing
  • More
  • Log in Sign up
Web
  • Foundations
  • Icon styles
  • Animation
  • Formats
  • Best practices
  • Integrations
  • Web
  • React
  • Flutter
  • WordPress
  • Webflow
  • Other
  • Tools
  • Figma
  • Canva
  • Adobe
  • Presentations
  • Support
  • Licenses
  • Account and Billing
  • Library
  • Troubleshooting

Web

In this guide, we demonstrate how to use the Lordicons in a web page project. To make the entire process easier, we have prepared a player (a custom element) that can be integrated into any web project.

We love open-source, and whenever possible, we strive to give back to the community. The player we've prepared is available for free as part of the NPM package. Additionally, we provide a repository with the complete source code.

<script src="https://cdn.lordicon.com/lordicon.js"></script>

<lord-icon
    src="https://cdn.lordicon.com/lewtedlh.json"
    trigger="hover"
    stroke="light"
    state="hover-pinch"
    colors="primary:#3080e8,secondary:#b4b4b4"
    style="width:250px;height:250px">
</lord-icon>

Simple usage

The simplest way to integrate the Lordicon player into a website is by loading a JavaScript script, which will automatically display icons wherever you've embedded the HTML <lord-icon> tag.

You can obtain sample code for a specific icon by using the embed HTML function on our website. This method is ideal for rapid prototyping, where performance and control over the loading process are not critical.

In the <head> section, you should load the lordicon.js script. This script should only be loaded once and is independent of the selected icon. The script link may change with the release of a new player version, and using new icons may require its updated version.

<script src="https://cdn.lordicon.com/lordicon.js"></script>

You should place the <lord-icon> tag where you want the icon to appear.

<lord-icon
    src="https://cdn.lordicon.com/lbjtvqiv.json"
    trigger="hover">
</lord-icon>

Simple usage

Click to see a straightforward integration of the player with a web page directly in your browser.

As you can see, in the simplest integration, adding animated, custom icons created with a graphical editor is as easy as copying and pasting a few lines of code.

Recommended usage

The optimal way to integrate the player with a web application is to use the @lordicon/element package we've prepared. This package should be used in pair with lottie-web. When integrating our player, we recommend using a module bundler like Webpack, Rollup or another similar tool.

Installation:

npm install @lordicon/element lottie-web

Code:

import lottie from "lottie-web";
import { defineElement } from "@lordicon/element";

// define "lord-icon" custom element with default properties
defineElement(lottie.loadAnimation);

HTML:

<lord-icon trigger="hover" src="/my-icon.json"></lord-icon>

Recommended usage

Click to view a live integration of the player with a website using the @lordicon/element package in your browser.

API

Custom Element

Our custom element <lord-icon> not only allows you to easily embed an icon on a web page but also dynamically customize it using HTML.

We support the following attributes here (all values are optional):

Attribute

Description

src

Link to the icon.

colors

Assign colors in text notation, where the first part is the color name and the second part is its value. For example: "outline:#121331,primary:#3a3347".

stroke

Thickness for supported icons, for example: "light", "regular", "bold".

trigger

The trigger name to be assigned to the icon. By default, we support: "in," "click", "hover", "loop", "loop-on-hover", "morph", "boomerang", "sequence."

target

Query selector for the element on which events will be listened.

state

Choose an animation for the icon (a single icon in Lordicon can have multiple built-in animations - you can see which animations an icon supports in our editor).

loading

The method by which the icon will be loaded. It allows for delayed loading of the icon. Acceptable values: "lazy", "interaction."

icon

Icon name to load. This applies to those who want to load icons in conjunction with the API.

delay

It allows you to introduce a pause between animation plays. Available for the following triggers: "loop", "loop-on-hover", "in".

In addition to managing attributes, users can customize icons (to a limited extent) using CSS variables. Variables allow for dynamic color swapping of icons, opening the door to easily implement features like dark mode/light mode switching or icon color changes through interaction without the need to write JavaScript code.

--lord-icon-primary: red;
--lord-icon-secondary: blue;

Note: 'primary' and 'secondary' above are example color names. Different icons may support different colors.

If you want the icon to take on a single color based on the text of the parent element, you can assign it a dedicated class, 'current-color':

<lord-icon class="current-color" trigger="hover" src="/my-icon.json"></lord-icon>

The provided custom element comes with multiple built-in triggers. Triggers define how the icon will interact with the web page. Additionally, triggers can be instructed to follow a specified element. Here's an example of an icon that will animate when the mouse hovers over the parent button:

<div class="btn">
    <lord-icon target=".btn" trigger="hover" src="/my-icon.json"></lord-icon>
</div>

Player

Working with the element, depending on the project's needs, can go far beyond simply embedding an icon on a web page. We provide access to an API where animation, various icon properties, and supported triggers can be fully controlled by the programmer. This allows for achieving intriguing effects where interaction with icons can add value to the project being implemented.

For the loaded icon, you gain access to the player instance:

const element = document.querySelector("lord-icon");

// the player is ready
element.addEventListener("ready", () => {
    element.playerInstance.play();
});

// the animation is complete
element.addEventListener("complete", () => {
    element.playerInstance.direction *= -1;
    element.playerInstance.play();
});

Supported methods:

Method

Description

play()

Play the animation.

Note: A finished animation can't be played again from the last frame. However you can change the direction of the animation or replay it from the beginning.

playFromBeginning()

Play the animation from the beginning.

pause()

Pause the animation.

stop()

Stop the animation.

goToFrame(frame)

Go to the exact frame.

goToFirstFrame()

Go to the first animation frame.

goToLastFrame()

Go to the last animation frame.

addEventListener(name, callback)

Start listening for an event. Supported events: "ready", "refresh", "complete", "frame".

removeEventListener(name, callback)

Stop listening for an event.

Supported properties:

Property

Description

properties

Access to read or modify multiple properties at once. Resets to default any properties that are not provided.

colors

This property allows you to discover customizable colors or update them within a processed icon.

stroke

The 'stroke' property gives you the value of the icon's stroke width.

state

This property allows you to control the state (motion type) of the icon. States enable switching between multiple animations built into a single icon file.

speed

This property allows you to control the speed of the icon animation.

frame

Access the player frame. You can manually control the animation playback by changing this frame.

direction

The 'direction' property lets you influence the direction of the animation playback, whether it plays forward (1) or in reverse (-1).

loop

This property allows you to control whether the player should loop the animation.

Supported read-only properties:

Property

Description

isReady

The player is ready.

isPlaying

The player is currently playing the animation.

states

The 'states' property provides a list of supported states by a processed icon.

frames

The 'frames' property provides the value of the animation length in terms of the number of frames.

duration

The 'duration' property provides the value of the animation length in seconds.

In the included repository and examples, we demonstrate how to use the low-level player.

Examples

Triggers

Discover all the built-in animation triggers available.

Show
Trigger target

Utilize the 'target' attribute to specify the parent element responsible for triggering the animation.

Show
Customization

Learn about all the customizable attributes supported by the element.

Show
Current color

Delve into the 'current-color' class, allowing icons to inherit their color from a parent element.

Show
CSS variables

Explore the use of CSS variables to customize colors.

Show
Background

Discover how to use the element as a background.

Show
Manual control

Gain insights into manual interaction with icons and players.

Show
Icon loader

See an example of creating an icon loader callback, enabling you to provide icon data from alternative sources.

Show
Loading (lazy)

Learn how to load icons only when needed.

Show
Loading (lazy + effect)

Load icons only when needed and create a smooth, simultaneous fade-in effect after the load completes.

Show
Loading (placeholder)

Display a small SVG placeholder until the custom element is ready.

Show
Loading (placeholder + interaction)

Display a small SVG placeholder until the first user interaction with the element.

Show
State

Explore the use of icon state animations.

Show
Custom trigger

This example demonstrates how to create a straightforward custom animation trigger.

Show
Custom trigger (scroll)

Create a custom trigger that plays an animation when a user scrolls the website.

Show
Custom trigger (in screen)

Build an animation trigger that plays when the icon appears in the browser viewport.

Show
Custom trigger (loop in screen)

Activate an already available 'loop' when the element appears fully on the user's screen.

Show
Custom trigger (states)

This example demonstrates complex usage of states with a custom trigger. See how the trash icon appears, fills, and erases as the user interacts.

Show
Upload

Get a preview of uploaded files.

Show
Angular

Lordicon in pair with Angular.

Show

Best practices

Icon hosting

While we expect minimal issues, there is no guarantee of 100% CDN uptime. If your project is of paramount importance to your clients, we suggest you consider hosting icons as Lottie JSON (Lordicon) on your own.

Minify

If you're not using multiple animations or strokes within a single icon in your project, consider using the 'minify' option when downloading its JSON. This will result in a smaller file that loads faster for users.

Optimizations

If you have the time and the technical skills, ensure that animated icons display 'instantly.' You can achieve this by:

  • Downloading the SVG for a specific icon.
  • Delaying the loading of the icon's JSON until the first interaction with it.

We have demonstrated how to achieve this in our examples and repository.

  • Intro
  • Simple usage
  • Recommended usage
  • API
  • Examples
  • Best practices

Was this article helpful?

Sorry to hear that. How can we improve this article?

Thanks for your feedback!

Best practices React