๐Ÿ–‹๏ธCustom Fonts

Using a web font service

Let's see how to use, for example, Playwrite Netherland via Google Fonts.

First we need to add the few links tag we got from from Google Fonts in our HTML <head>:

index.html (or public/index.html in Webpack)
<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />

        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />

        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Playwrite+NL:wght@100..400&display=swap" rel="stylesheet">

    </head>

    <body>
        <div id="root"></div>
        <script type="module" src="/src/main.tsx"></script>
    </body>
</html>

The fonts must also be imported in Storybook, so we add the links in the .storybook/preview-head.html as well:

.storybook/preview-head.html
<style>
    body.sb-show-main.sb-main-padded {
        padding: 0;
    }
    
    /* ... */
</style>

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Playwrite+NL:wght@100..400&display=swap" rel="stylesheet">

Then all we have to do is apply the Font font family, in this example we will use vanilla CSS but you can of course use your favorite styling solution.

src/login/main.css
.kcHeaderWrapperClass {
  /* NOTE: We would use `body {` if we'd like the font to be applied to everything. */
  font-family: "Playwrite NL", cursive;
}
src/login/KcPage.tsx
import "./main.css";
import { Suspense, lazy } from "react";
// ...

That's it!

Using self hosted fonts

Keycloak is often used in enterprise internal network with strict network traffic control. In this context, using a Font CDN isn't an option, you want the font to be bundled in your jar and served directly by the Keycloak server.

Let's see how we would use a self hosted copy Vercel's Geist font.

First let's download and extract the font files in src/login/assets/fonts/geist/:

Now let's set Geist as the default font.

src/login/main.css
@import url(./assets/fonts/geist/main.css);

body {
  font-family: Geist;
}
src/login/KcPage.tsx
import "./main.css";
import { Suspense, lazy } from "react";
// ...

Result:

Was this helpful?