# \[UMG\] How to maintain anchoring of widget when added as child to another widget?

**URL:** <https://forums.unrealengine.com/t/umg-how-to-maintain-anchoring-of-widget-when-added-as-child-to-another-widget/460577>\
**Category:** UI\
**Tags:** overlay, screen-position, unreal-engine, Widget, question, UMG\
**Created:** [January 14, 2020, 7:25pm UTC](https://forums.unrealengine.com/t/umg-how-to-maintain-anchoring-of-widget-when-added-as-child-to-another-widget/460577 "2020-01-14T19:25:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnarlrock](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gnarlrock](https://forums.unrealengine.com/u/gnarlrock)\
**Post date:** [January 14, 2020, 7:25pm UTC](https://forums.unrealengine.com/t/umg-how-to-maintain-anchoring-of-widget-when-added-as-child-to-another-widget/460577/1 "2020-01-14T19:25:12Z")

</div>

I’ve got a UMG widget (ContainerWidget) that has a canvas panel as its root. Inside the canvas panel is another widget (CenterWidget) that is anchored to the center position. If I add the ContainerWidget to the viewport everything works as expected: The CenterWidget is anchored to the center of the screen.

I’m now trying to create a MasterWidget that has and Overlay as its root which other widgets will be added to dynamically (from code). The MasterWidget is just used to show/hide all of the HUD UI but should not change any of the positioning/anchoring.

But if I use AddChild to add the ContainerWidget to the MasterWidget, the anchoring within the MasterWidget no longer works (it sticks to the top left of the screen). I’ve tried changing the root of the MasterWidget to CanvasPanel, Overlay, etc. but nothing seems to preserve the anchoring of the children.

Is there a way to add a widget to another widget but preserve the screen anchoring or the internal one?

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [January 16, 2020, 8:14pm UTC](https://forums.unrealengine.com/t/umg-how-to-maintain-anchoring-of-widget-when-added-as-child-to-another-widget/460577/2 "2020-01-16T20:14:09Z")

</div>

I’m not a great expert on widgets, but I think you have to do the anchoring in the master widget.

Because even if you’re not nesting them like this, you have the same problem just within one widget.

For instance place a button, anchor it, then surround it with a vertical box. The widget will take the anchor from the box, not the button…

---

<div class="post-metadata">

**Author:** ![gnarlrock](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gnarlrock](https://forums.unrealengine.com/u/gnarlrock)\
**Post date:** [January 22, 2020, 7:59pm UTC](https://forums.unrealengine.com/t/umg-how-to-maintain-anchoring-of-widget-when-added-as-child-to-another-widget/460577/3 "2020-01-22T19:59:29Z")

</div>

As ClockworkOcean mentions, you do have to make changes to the slot of the child widget within the master widget for it to work. Luckily it is pretty simple. Here is the code that adds a **childWidget** to the overlay of the RootWidget. It then sets the horizontal and vertical alignment to Fill which allows the childWidget to take up the full screen area and make it’s internal anchoring work properly.

```
UOverlay* overlay = Cast<UOverlay>(_rootWidget->GetRootWidget());
UOverlaySlot* slot = overlay->AddChildToOverlay(childWidget);

// Set the child widget to fill overlay area to allow internal anchoring to work
slot->SetHorizontalAlignment(EHorizontalAlignment::HAlign_Fill);
slot->SetVerticalAlignment(EVerticalAlignment::VAlign_Fill);
```
