Changing the background image

Let's see, as an example, the different ways you have to change the backgrounds image of the login page using CSS only.

There is the equivalent of this guide using CSS-in-JS here.

First let's download a background image an put it in src/login/assets/background.png.

Then let's apply it as background:

src/login/main.css
body.kcBodyClass {
  background: url(./assets/background.png) no-repeat center center fixed;
}

We import the StyleSheet:

src/login/KcPage.tsx
import "./main.css";
// ...

Result:

Custom background successfully applied
Replacing the image without re-building the theme

If you want to be able to "hot swipe" the image, without rebuilding the theme you have to import the image from a different location.

Place the file into /public/background.png.

Then, in your CSS code import the image with an absolute path:

src/login/main.css
body.kcBodyClass {
  background: url(/background.png) no-repeat center center fixed;
}

Now if you want to replace the image directly in Keycloak you'll be able to find it at:

/opt/keycloak/themes/<name of your theme>/login/resources/dist/background.png

For a more advanced example, in the following video I show how to load different background for different page and how to create theme variant.

Last updated

Was this helpful?