Skip to content

Latest commit

 

History

History
102 lines (79 loc) · 2.06 KB

align-center-middle.question.md

File metadata and controls

102 lines (79 loc) · 2.06 KB
title alt_titles
How do I center a widget in a screen?
centre a widget
centre widgets
center a control
centre a control
center controls
centre controls

!!! tip

See [*How To Center Things*](https://textual.textualize.io/how-to/center-things/) in the
Textual documentation for a more comprehensive answer to this question.

To center a widget within a container use align. But remember that align works on the children of a container, it isn't something you use on the child you want centered.

For example, here's an app that shows a Button in the middle of a Screen:

from textual.app import App, ComposeResult
from textual.widgets import Button

class ButtonApp(App):

    CSS = """
    Screen {
        align: center middle;
    }
    """

    def compose(self) -> ComposeResult:
        yield Button("PUSH ME!")

if __name__ == "__main__":
    ButtonApp().run()

If you use the above on multiple widgets, you'll find they appear to "left-align" in the center of the screen, like this:

+-----+
|     |
+-----+

+---------+
|         |
+---------+

+---------------+
|               |
+---------------+

If you want them more like this:

     +-----+
     |     |
     +-----+

   +---------+
   |         |
   +---------+

+---------------+
|               |
+---------------+

The best approach is to wrap each widget in a Center container that individually centers it. For example:

from textual.app import App, ComposeResult
from textual.containers import Center
from textual.widgets import Button

class ButtonApp(App):

    CSS = """
    Screen {
        align: center middle;
    }
    """

    def compose(self) -> ComposeResult:
        yield Center(Button("PUSH ME!"))
        yield Center(Button("AND ME!"))
        yield Center(Button("ALSO PLEASE PUSH ME!"))
        yield Center(Button("HEY ME ALSO!!"))

if __name__ == "__main__":
    ButtonApp().run()