Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I vertically align the contents of a cell in a Vaadin Grid?

I have the following code that successfully centers the contents of a cell in my Vaadin grid.

        this.grid.setRowHeight(65);
        this.grid.setStyleGenerator(item -> "v-align-center");

It makes it looks like this.

How can I also vertically center? When I tried adding the style name v-align-middle it did nothing.

enter image description here

like image 466
benstpierre Avatar asked Oct 16 '25 13:10

benstpierre


2 Answers

I played with CSS styles and default TextRenderer but did not get it working. When you switch to HtmlRenderer then you have more styling options with additional div HTML elements. See this question for styling hints. For example, this should work when you can use flex layouting:

<div style="display:flex; height:100%; width:100%">
    <div style="margin: auto;">11 m³</div>
</div>

Another workaround is to use a ComponentRenderer column with a HorizontalLayout set to 100% height and its child component aligned to middle center.

like image 117
Steffen Harbich Avatar answered Oct 19 '25 11:10

Steffen Harbich


If you're setting a specific height on the rows then it's pretty straight forward to achieve with CSS.

Rather than using the StyleGenerator, I'd just apply a CSS Class to the Grid itself.

Java:

addStyleName("my-custom-grid");

CSS:

.my-custom-grid .v-grid-body .v-grid-row .v-grid-cell {
    height: 65px;
    line-height: 65px;
}    
like image 20
Jay Avatar answered Oct 19 '25 12:10

Jay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!