英文:
Having Trouble Setting Background Image on Vaadin Login Form
问题
我试图弄清楚如何在我正在使用 Vaadin 制作的应用程序的登录页面中放置一个背景图像:
我对 CSS 不太熟悉,这就是我遇到问题的原因之一。
我尝试使用 Vaadin 主题助手、在 StackOverflow 和 Vaadin 论坛上查找这个问题,但都没有找到解决方法。当我运行程序时,登录屏幕上没有加载背景图像。
我的程序布局:
我想要用作背景的图像是 prepbooks.jpg。
Styles.CSS 类:
@import url('./views/list-view.css');
@import url('lumo-css-framework/all-classes.css');
a[highlight] {
    font-weight: bold;
    text-decoration: underline;
}
.backgroundImage {
    background: url("images/prepbooks.jpg");
}
LoginView 类(我想要在其中添加背景图像的页面):
package com.example.application.views;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.html.Image;
import com.vaadin.flow.component.login.LoginForm;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
@Route("login")
@PageTitle("Login | Sample App")
public class LoginView extends VerticalLayout implements BeforeEnterObserver {
   private final LoginForm login = new LoginForm();
   public LoginView(){
      addClassName("login-view");
      setSizeFull();
      setAlignItems(Alignment.CENTER);
      setJustifyContentMode(JustifyContentMode.CENTER);
      login.setAction("login");
      VerticalLayout header = new VerticalLayout();
      header.add(new H1("Sample App"));
      header.setAlignItems(Alignment.CENTER);
      add(header, login);
   }
   @Override
   public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
      // inform the user about an authentication error
      if(beforeEnterEvent.getLocation()
            .getQueryParameters()
            .getParameters()
            .containsKey("error")) {
         login.setError(true);
      }
   }
}
希望这对你有帮助。
英文:
I'm trying to figure out how to put a background image in my login page of an application that I am making using Vaadin:
enter image description here
I am not familiar with CSS which is a reason why I am having trouble.
I have tried using the Vaadin Theme Assistant, looking up this question on StackOverflow and the Vaadin Forum but none of those have led me to a solution. When I run my program, no background image loads on the login screen.
Layout of my program:
enter image description here
The image I can trying to use as a background is prepbooks.jpg.
Styles.CSS class:
@import url('./views/list-view.css');
@import url('lumo-css-framework/all-classes.css');
a[highlight] {
    font-weight: bold;
    text-decoration: underline;
}
 .backgroundImage{
      background: url("images/prepbooks.jpg") ;
 }
LoginView. class (the page in which I want the background image to be on):
package com.example.application.views;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.html.Image;
import com.vaadin.flow.component.login.LoginForm;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
@Route("login")
@PageTitle("Login | Sample App")
public class LoginView extends VerticalLayout implements BeforeEnterObserver {
   private final LoginForm login = new LoginForm();
   public LoginView(){
      addClassName("login-view");
      setSizeFull();
      setAlignItems(Alignment.CENTER);
      setJustifyContentMode(JustifyContentMode.CENTER);
      login.setAction("login");
      VerticalLayout header = new VerticalLayout();
      header.add(new H1("Sample App"));
      header.setAlignItems(Alignment.CENTER);
      add(header, login);
   }
   @Override
   public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
      // inform the user about an authentication error
      if(beforeEnterEvent.getLocation()
            .getQueryParameters()
            .getParameters()
            .containsKey("error")) {
         login.setError(true);
      }
   }
}
答案1
得分: 0
将 prepbooks.jpg 放置在 frontend/themes/flowcrmtutorial/images 文件夹中,然后可以从 styles.css 文件中引用它。
参考文档以获取详细信息:https://vaadin.com/docs/latest/styling/custom-theme/creating-custom-theme/#other-theme-assets
我假设您希望将背景图像添加到整个视图。您已经使用 addClassName("login-view") 在其上设置了CSS类名称,因此我们可以在我们的CSS中使用它:
styles.css:
.login-view {
  background-image: url("images/prepbooks.jpg");
}
英文:
Place the prepbooks.jpg in the frontend/themes/flowcrmtutorial/images folder. Then you can reference it from the styles.css file.
frontend
└── themes
    └── flowcrmtutorial
        ├── images
        │   └── prepbooks.jpg
        └── styles.css
See the documentation for details: https://vaadin.com/docs/latest/styling/custom-theme/creating-custom-theme/#other-theme-assets
I assume you want to add the background image to the whole view. You already set a CSS class name on it with addClassName("login-view"), so we can use that in our CSS:
styles.css:
.login-view {
  background-image: url("images/prepbooks.jpg");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论