Vaadin 25: Login overlay background image

Hi,

happy new year :sparkler:

As some of my fellow forum users here, I take the time between the years and migrated the first of my apps to Vaadin 25 :smiley:
This app use an LoginView extends LoginOverlay and I usually customize the background image via CSS:

vaadin-login-overlay-wrapper::part(backdrop) {
    background-image: url('images/login-background.jpg');
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
}

During migration, I made the following changes as suggested by the official Vaadin docs:

  1. replace @Theme with @StyleSheet("css/xyz.css")
  2. move the css files to src/main/resources/META-INF/resources/css
  3. move the image to src/main/resources/META-INF/resources/images
  4. use an absolute image path in CSS, starting with slash
    background: url('/images/login-background.jpg');
  1. adjust the Spring Security configuraton for those static files.

This all doesn’t work. The background image wasn’t visible :cry:

But the browser dev tools shows
a) css file was loaded successfully
b) referenced background image was loaded successfully

After adding the Chrome Dev MCP to my Claude based setup, the following hint came up:

The image loads successfully, but the problem is that Vaadin’s internal Shadow DOM styles use the background shorthand, which overrides your background image, despite !important.

The problem: external CSS can only override Shadow DOM internals to a limited extent. The best solution is to set it directly in the Shadow DOM using JavaScript:

So I have to add the following to my LoginView.java

    @Override
    protected void onAttach(AttachEvent attachEvent) {
        super.onAttach(attachEvent);

        getElement().executeJs(
            "const wrapper = this.shadowRoot.querySelector('vaadin-login-overlay-wrapper');" +
                "if (wrapper && wrapper.shadowRoot) {" +
                "  const backdrop = wrapper.shadowRoot.querySelector('[part=\"backdrop\"]');" +
                "  if (backdrop) {" +
                "    backdrop.style.setProperty('background', " +
                "      'url(/images/login-background.jpg) center/cover no-repeat', " +
                "      'important');" +
                "  }" +
                "}"
        );
    }

Placing the css part from above to the CSS file also doesn’t work. I need this whole javascript stuff in my java class :see_no_evil:

Is there another, easier way to customize such simple thing as the background image for the login view ?

If not, please provide one in one of the next Vaadin version.

Kind regards
Dominik

I’m not sure if it will fix your issue, but the docs for Vaadin 25 mention vaadin-login-overlay::part(backdrop)

2 Likes

First thing that comes to mind vaadin-login-overlay::part(backdrop)

2 Likes

Thanks @marcoc_753 and @knoobie for your fast response:

You’re absolutely right: switching from

vaadin-login-overlay-wrapper::part(backdrop)

to

vaadin-login-overlay::part(backdrop)

did the trick :see_no_evil: