4

I am using a ipywidgets.Label to display a text in a notebook. Basic question, how do I change the text alignment within the Label? It seems to be left aligned and I want it to be right aligned. I really looked for it in the docs but I cannot find it.

Thanks

dj K
  • 180
  • 2
  • 11

2 Answers2

5

The answer by RZin sort-of works but, it doesn't actually align the text-- it just moves the box containing the text.

One can align the label text this way:

Label("LABEL", layout=Layout(display="flex", justify_content="flex-start")

Use "flex-start" for left-align, "center" for center-align, and "flex-end" for right-align. There is also "space-between" and "space-around".

Full example that can be copy and pasted into a notebook:

from ipywidgets import Label, Layout, HBox
from IPython.display import display

x = Label("Align Left", layout=Layout(display="flex", justify_content="flex-start", width="30%", border="solid"))
y = Label("Align Center", layout=Layout(display="flex", justify_content="center", width="30%", border="solid"))
z = Label("Align Right", layout=Layout(display="flex", justify_content="flex-end", width="30%", border="solid"))
display(HBox([x,y,z]))

jupyter notebook example output

Rick
  • 43,029
  • 15
  • 76
  • 119
4

I had similar question and wrapped my Label with VBox widget.

from ipywidgets import VBox, Label, Layout

VBox([Label('I want this to be in the center.')]
,layout=Layout(width='100%', display='flex' ,
align_items='center'))
RZin
  • 91
  • 1
  • 5