Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center a widget in the middle of the screen in vanilla GWT?

Tags:

layout

widget

gwt

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,

like image 276
Chris Avatar asked Jul 02 '10 17:07

Chris


People also ask

How do I center a widget in HTML?

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.

How do I center my screen in the middle of CSS?

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.

How do I center in stackoverflow?

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.


2 Answers

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!

like image 137
Nico Adams Avatar answered Sep 28 '22 13:09

Nico Adams


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.

like image 28
Jeremiah Avatar answered Sep 28 '22 11:09

Jeremiah