Methods
Below are the methods available in the Font Awesome Javascript API.
counter(content, params)
Add counters to layers.
Params
Allow the following keys (see icon()):
Name |
---|
title |
classes |
attributes |
styles |
dom.css()
Stylesheet definitions required to properly display icons generated by the API in the DOM.
Generates the accompanying CSS that is necessary to correctly display icons. If
you choose to disable autoAddCss
in the configuration you’ll need to grab
these styles and insert them manually into the DOM.
This is useful and normally paired with dom.insertCss()
.
dom.i2svg(params)
Will automatically find any <i>
tags in the page and replace those with <svg>
elements.
This functionality uses
requestAnimationFrame
to batch the updates and increase performance.
To specify a different element to search:
As of version 5.7.0, this method also supports the Promise API:
You can also use callbacks that will be triggered when the icons have been rendered:
dom.insertCss()
Convenience method that will add the given CSS to the DOM <head>
.
dom.watch(params)
Calls dom.i2svg() for you and watches the DOM for any additional icons being added or modified.
This method is useful when you’re loading @fortawesome/fontawesome-svg-core
, but would still like to leverage automatic DOM watching.
Params
Calling dom.watch()
without params is equivalent to calling with the following params:
Name | Purpose |
---|---|
autoReplaceSvgRoot | limits the scope for automatic search and replacement of icons. |
observeMutationsRoot | limits the scope of the mutation observer to the DOM under this root node. |
findIconDefinition(params)
Look up an icon definition previously registered in the Library.
Basic Use
Specify the prefix as well
You can then feed this as the iconDefinition
to other functions such as icon()
.
icon(iconDefinition, params)
Renders an icon as SVG.
Basic Use
Advanced Options
Getting the HTML for the icon:
Appending nodes from an HTMLCollection
:
Abstract tree:
Using a transform:
Use the main icon as a mask for another another icon:
Specify mask ID used to generate the SVG definitions (added in 5.12.2):
This is useful for testing libraries that use snapshots to verify test results.
Add title attribute:
Explicit title ID when add title (added in 5.12.2):
This is useful for testing libraries that use snapshots to verify test results.
Additional classes:
Additional attributes:
Additional styles:
Creating a symbol (auto-generating ID):
Creating a symbol (explicit ID):
layer(assembler, params)
Allows multiple icons to be assembled together in a layer.
Convenience function for producing the HTML markup for layers.
It wraps icon()
, text()
, or counter()
inputs in <span class="fa-layers fa-fw"></span>
.
Specify additional classes like this.
Params
Allow the following keys:
Name |
---|
classes |
library.add(…iconDefinitions)
Pre-registering icon definitions so that do not have to explicitly pass them to render an icon.
Explicitly passing the icon
Using the library
Prefixes and Style Classes
Like the number of our icons, our styles have grown too! We now have icons in six styles. The chart below outlines all of our styles along with how you can use them with Font Awesome Icons:
Style | Style Class |
---|---|
Brand | fab , brands |
Duotone | fad , duotone |
Light | fal ,light |
Regular | far , regular |
Solid | fa , fas , solid |
Read more about our basic styling can be used in conjunction with the styling.
parse.transform(transformString)
Takes a Power Transform string and produces the normalized transform object used by the API.
Power Transforms are space-separated verbs and values.
Transform verbs
Verb | What it does |
---|---|
grow | Makes the icon larger |
shrink | Makes the icon smaller |
left | Move the icon to the left |
right | Move the icon to the right |
up | Move the icon up |
down | Move the icon down |
rotate | Rotate the icon left or right |
flip-v | Flips along the vertical axis |
flip-h | Flips along the horizontal axis |
Transform Values
Font Awesome 5 icons are built on a sixteen pixel grid. For size and movement values represent 1 unit out of 16.
For example, up-2
means “move the icon up 2 units”. If the icon happens to be 16px high this will result in the icon being moved up by 2 pixels.
Converting to SVG transform values
Power Transform string are easy to use and compose but must be converted into a format that is useful to perform SVG transforms.
The following data is what gets used when rendering icons.
text(content, params)
Add text to layers.
Params
Allow the following keys (see icon()):
Name |
---|
transform |
title |
classes |
attributes |
styles |
toHtml(abstractElement)
Convenience method to convert an abstract representation of a Font Awesome object into its corresponding HTML markup.
The icon()
, text()
, layer()
, and counter()
functions each return a Font Awesome object that has properties abstract
and html
. (See also the documentation for icon() more details about the shape of such objects.)
toHtml()
can be used to transform any abstract element into HTML.