files/en-us/web/api/fontface/index.md
{{APIRef("CSS Font Loading API")}}{{AvailableInWorkers}}
The FontFace interface of the CSS Font Loading API represents a single usable font face.
This interface defines the source of a font face, either a URL to an external resource or a buffer, and font properties such as style, weight, and so on.
For URL font sources it allows authors to trigger when the remote font is fetched and loaded, and to track loading status.
FontFace object, built from an external resource described by a URL or from an {{jsxref("ArrayBuffer")}}.FontFace object when the font specified in the object's constructor is done loading or rejects with a SyntaxError {{domxref("DOMException")}}."unloaded", "loading", "loaded", or "error".The code below defines a font face using data at the URL "my-font.woff" with a few font descriptors.
Just to show how it works, we then define the stretch descriptor using a property.
// Define a FontFace
const font = new FontFace("my-font", 'url("my-font.woff")', {
style: "italic",
weight: "400",
});
font.stretch = "condensed";
Next we load the font using {{domxref("FontFace.load()")}} and use the returned promise to track completion or report an error.
// Load the font
font.load().then(
() => {
// Resolved - add font to document.fonts
},
(err) => {
console.error(err);
},
);
To actually use the font we will need to add it to a {{domxref("FontFaceSet")}}. We could do that before or after loading the font.
For additional examples see CSS Font Loading API > Examples.
{{Specifications}}
{{Compat}}