简体   繁体   中英

How can I change the background color of the navigation bar - Vaadin

I'm using the class AppLayout in Vaadin. I wonder how I can change the background color in the navigation bar.

I know how to add CSS style in Vaadin, but I have trouble to access the navigation class.

Here is my code. As you see, I'm always using the method setClassName . But where can I find that method for the navigation bar?

@Viewport("width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes, viewport-fit=cover")
@PWA(name = "Hemsida", shortName = "Hem")
@Route("")
@CssImport("./CSS/MainView.css")
public class MainView extends AppLayout {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public MainView() {

        // Image bar
        Image barImage = new Image("img/cropped-logo_liggande_rod.png", "Spektrakon Logo");
        barImage.setClassName("barImage");

        // Drawer
        DrawerToggle drawerToggle = new DrawerToggle();
        drawerToggle.setClassName("drawerToggle");


        addToNavbar(barImage, drawerToggle);


        Tabs tabs = new Tabs(new Tab("Hem"), new Tab("Produktutveckling"), new Tab("Industriell Design"), new Tab("System"), new Tab("Kvalitet"), new Tab("Om oss"), new Tab("Intrenet"));
        tabs.setOrientation(Tabs.Orientation.VERTICAL);
        addToDrawer(tabs);
    }

}

From the Styling part you can see there is a navbar part. You can use it to style the navbar of a AppLayout


if navbarStyles.css has:

[part~="navbar"]{
    background-color: red;
}

And imported to the view with : @CssImport(value= "./styles/navbarStyles.css", themeFor = "vaadin-app-layout") , background color is changed


A complete example with using theme to distinguish from other AppLayout 's

@Route("")
@CssImport(value= "./styles/navbarStyles.css", themeFor = "vaadin-app-layout")
public class AppLayoutPictures extends AppLayout {

    public AppLayoutPictures(){
        setPrimarySection(AppLayout.Section.DRAWER);
        Image img = new Image("https://i.imgur.com/GPpnszs.png", "Vaadin Logo");
        img.setHeight("44px");
        addToNavbar(new DrawerToggle(), img);
        Tabs tabs = new Tabs(new Tab("Home"), new Tab("About"));
        tabs.setOrientation(Tabs.Orientation.VERTICAL);
        addToDrawer(tabs);
       //Set to AppLayout, propageted to `parts`
        getElement().setAttribute("theme","appLayout");
    }

navbarStyles.css

:host([theme~="appLayout"]) [part~="navbar"]{
    background-color: orange;
}

Result : 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM