user2810472
user2810472

Reputation: 135

Vaadin 10, how can I add the css class to div element in Java?

I am trying to create a user list in UI cards like this,

<style type="text/css">
    .userbox {
        left: 36px;
        color: black;
        font-family: arial;
        font-size: 17px;
        font-weight: 600;
        width: 300px;
        height: 150px;
        text-indent: 30px;
        box-shadow: 0 1px 6px rgba(0, 0, 0, 0.12), 0 1px 4px rgba(0, 0, 0, 0.24);
    }

    .userbox:hover {
        background-color: #d1e7cd;
    }

    img {
        border-radius: 5%;
    }
</style>

I want to generate these HTML elements dynamically,

<div class="userbox"></div>

How can I do that? I tried to create Div objects and add the styles but I couldn't find a way in Vaadin Flow.

Upvotes: 3

Views: 1415

Answers (2)

Naranmandakh Tsogoo
Naranmandakh Tsogoo

Reputation: 241

Div implemented HasStyle interface that interface has addClassName("") method:

public class Div extends HtmlContainer
public class HtmlContainer extends HtmlComponent
public class HtmlComponent extends Component implements HasSize, HasStyle

public interface HasStyle extends HasElement {

    /**
     * Adds a CSS class name to this component.
     *
     * @param className
     *            the CSS class name to add, not <code>null</code>
     */
    default void addClassName(String className) {
        getClassNames().add(className);
    }

In my case eclipse doesn't suggest it.

You can use it: divElement.addClassName("userbox");

Upvotes: 1

ollitietavainen
ollitietavainen

Reputation: 4290

This should be enough:

Div div = new Div();
div.setClassName("userbox");

Upvotes: 3

Related Questions