How to center all widgets of different widths? #1919
-
I'm sorry for asking for some help yet again, especially when this seems so basic... I have read the FAQ to center a widget, the Layout Guide, and the Align Reference but obviously I am still missing something. The minimal code below is based on the example in the FAQ - how would you horizontally center both the I understand I could wrap the button inside another Thanks again for any advice, from textual.app import App, ComposeResult
from textual.widgets import Button, Label
class CenteredApp(App):
CSS = """
Screen {
align: center middle;
}
"""
def compose(self) -> ComposeResult:
yield Label("I would like both this text and the button to be centered")
yield Button("PUSH ME!")
if __name__ == "__main__":
CenteredApp().run() |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You my find this discussion handy here. Long story short, I'd do it like this: from textual.app import App, ComposeResult
from textual.widgets import Button, Label
from textual.containers import Horizontal
class CenteredApp(App):
CSS = """
Screen {
align: center middle;
}
Horizontal {
align: center middle;
height: auto;
}
"""
def compose(self) -> ComposeResult:
yield Horizontal(Label("I would like both this text and the button to be centered"))
yield Horizontal(Button("PUSH ME!"))
if __name__ == "__main__":
CenteredApp().run() |
Beta Was this translation helpful? Give feedback.
-
Thanks again Dave for your help. Sorry I really thought I had searched pretty thoroughly before posting, but I must have missed this discussion or not scrolled down far enough.
I thought this might be how the Could I suggest that this additional info is included somewhere in the FAQ or the docs? Unless this is on the roadmap to change in the near future? Currently the workings of center alignment feel a little misleading unless you find this discussion (which I apparently missed) |
Beta Was this translation helpful? Give feedback.
You my find this discussion handy here. Long story short, I'd do it like this: