I wish to center a widget in the middle of the browser window (horizontally and vertically). I do not wish to stretch the widget to fill the root pane nor do I wish to use a popup window.
Is there a simple way this can be achieved?
Thanks,
Go to a text widget or anywhere that reads HTML and write the following code. For this, we'll use the <img tag. 5. If you'd like to center the image, just simply add a <div align=”center”> in front of the code and then </div> on the end.
You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.
The text-align: center; only centers the element's inline contents, not the element itself. If it is a block element (a div is), you need to set margin: 0 auto; , else if it is an inline element, you need to set the text-align: center; on its parent element instead.
You can do that programmatically as shown in the answer above, or you use CSS. To do it the CSS way, attach a style name to your widget first:
panel.setStyleName("style")
and in your application CSS file do the following:
.style {
margin-left: auto;
margin-right: auto;
}
That should centre the widget in your window.
Hope this helps!
Sorry to resurrect an old question, but the accepted answer does not address the entire question. The OP wanted to know how to center both horizontally and vertically.
Just apply the style:
width: 300px; //or some other width
height: 300px; //or some other height
margin: auto;
to your widget and make sure that the parent widget fills the entire window. One way to accomplish this would be to apply the following style to the parent (assuming the parent is a top level element):
height: 100%;
width: 100%;
Hope this helps someone who might stumble across this thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With