# Gui adaptWidthToChildren not working with adaptive scaling?

**URL:** <https://forum.babylonjs.com/t/gui-adaptwidthtochildren-not-working-with-adaptive-scaling/28911>\
**Category:** Bugs\
**Tags:** gui\
**Created:** [March 30, 2022, 9:22pm UTC](https://forum.babylonjs.com/t/gui-adaptwidthtochildren-not-working-with-adaptive-scaling/28911 "2022-03-30T21:22:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vorion](https://avatars.discourse-cdn.com/v4/letter/v/d26b3c/32.png) [@Vorion](https://forum.babylonjs.com/u/Vorion)\
**Post date:** [March 30, 2022, 9:22pm UTC](https://forum.babylonjs.com/t/gui-adaptwidthtochildren-not-working-with-adaptive-scaling/28911/1 "2022-03-30T21:22:46Z")

</div>

Hi,  
I’m trying to setup my gui to always scale up/down correctly by using the adaptive scaling feature as described here:

> **[The Babylon GUI | Babylon.js Documentation](https://doc.babylonjs.com/divingDeeper/gui/gui#adaptive-scaling)**
>
> Learn all about the Babylon.js 2D GUI system.

Since some elements, e.g. StackPanel, require width/heigth in pixel, I’m using a default resolution where I setup everything in pixel values, and babylonjs should scale up or down all controls automatically on window resize and other resolutions.  
In this playground example I’ve got two buttons inside a stack panel with adaptWidthToChildren enabled, but the stackpanel and the buttons seem to resize independently. When scaling down, the width of the stackpanel is smaller than the button width, resulting in cutting of parts of the buttons. When scaling up, the width of the stackpanel is bigger than the width of the buttons.

> **[Babylon.js Playground](https://playground.babylonjs.com/#XCPP9Y%2311896)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

(same with adaptHeightToChildren and horizontal stackpanel [https://playground.babylonjs.com/#XCPP9Y#11897](https://playground.babylonjs.com/#XCPP9Y#11897))

I don’t know if this is intended/by design and I’m doing something wrong here, or if it’s a bug.

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [March 30, 2022, 10:17pm UTC](https://forum.babylonjs.com/t/gui-adaptwidthtochildren-not-working-with-adaptive-scaling/28911/2 "2022-03-30T22:17:43Z")

</div>

cc @DarraghBurke

---

<div class="post-metadata">

**Author:** ![DarraghBurke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/darraghburke/32/10621_2.png) [@DarraghBurke](https://forum.babylonjs.com/u/DarraghBurke)\
**Post date:** [March 31, 2022, 11:53pm UTC](https://forum.babylonjs.com/t/gui-adaptwidthtochildren-not-working-with-adaptive-scaling/28911/3 "2022-03-31T23:53:52Z")

</div>

This is definitely a bug. If we set the width of the stack panel directly, it scales correctly. Thanks for catching this! I will take a look into it and hope to have a fix in by Monday.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [April 1, 2022, 1:49am UTC](https://forum.babylonjs.com/t/gui-adaptwidthtochildren-not-working-with-adaptive-scaling/28911/4 "2022-04-01T01:49:20Z")

</div>

Assigned @DarraghBurke on April 1, 2022, 1:49am UTC



---

<div class="post-metadata">

**Author:** ![Vorion](https://avatars.discourse-cdn.com/v4/letter/v/d26b3c/32.png) [@Vorion](https://forum.babylonjs.com/u/Vorion)\
**Post date:** [April 1, 2022, 11:03am UTC](https://forum.babylonjs.com/t/gui-adaptwidthtochildren-not-working-with-adaptive-scaling/28911/5 "2022-04-01T11:03:00Z")

</div>

Thanks. This bug also seems to affect other container controls, e.g. the default container.

> **[Babylon.js Playground](https://playground.babylonjs.com/#XCPP9Y%2311936)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![DarraghBurke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/darraghburke/32/10621_2.png) [@DarraghBurke](https://forum.babylonjs.com/u/DarraghBurke)\
**Post date:** [April 5, 2022, 9:49pm UTC](https://forum.babylonjs.com/t/gui-adaptwidthtochildren-not-working-with-adaptive-scaling/28911/6 "2022-04-05T21:49:47Z")

</div>

Hey, apologies for the late update - I was actually out of office on Monday. I have identified the cause of the issue.

`adaptWidthToChildren` is implemented as follows:

1. Compute max width, in pixels, of children
2. Set our width in pixels to the max width

The issue is that the children’s widths have already had the `idealWidth`/`idealHeight` scaling applied to them, but then we re-apply it to the parent as well. Essentially, we’re accounting for the rescaling twice.

I’m thinking now about the best solution to this problem. The easiest hack would be to apply the reverse of the adaptive scaling after the width of all children is computed, but that doesn’t feel like the right approach. I’ll keep you posted on what I come up with.

---

<div class="post-metadata">

**Author:** ![DarraghBurke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/darraghburke/32/10621_2.png) [@DarraghBurke](https://forum.babylonjs.com/u/DarraghBurke)\
**Post date:** [April 5, 2022, 9:53pm UTC](https://forum.babylonjs.com/t/gui-adaptwidthtochildren-not-working-with-adaptive-scaling/28911/7 "2022-04-05T21:53:45Z")

</div>

Ah, looks like we already have a `ignoreAdaptiveScaling` property on our Unit class. We can just turn that on for controls using `adapt{_}ToChildren` 🙂 PR will be open shortly!

edit: [[GUI] use ignoreAdaptiveScaling on containers width adaptWidthToChildren or adaptHeightToChildren by darraghjburke · Pull Request #12345 · BabylonJS/Babylon.js (github.com)](https://github.com/BabylonJS/Babylon.js/pull/12345)

---

<div class="post-metadata">

**Author:** ![Vorion](https://avatars.discourse-cdn.com/v4/letter/v/d26b3c/32.png) [@Vorion](https://forum.babylonjs.com/u/Vorion)\
**Post date:** [April 6, 2022, 4:47pm UTC](https://forum.babylonjs.com/t/gui-adaptwidthtochildren-not-working-with-adaptive-scaling/28911/8 "2022-04-06T16:47:12Z")

</div>

Thanks for the fix. I will test it locally with the next npm update.

Edit: Just tested it, and the bug seems to be fixed. Thanks again.

---

<div class="post-metadata">

**Author:** ![DarraghBurke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/darraghburke/32/10621_2.png) [@DarraghBurke](https://forum.babylonjs.com/u/DarraghBurke)\
**Post date:** [April 7, 2022, 8:09pm UTC](https://forum.babylonjs.com/t/gui-adaptwidthtochildren-not-working-with-adaptive-scaling/28911/9 "2022-04-07T20:09:23Z")

</div>

Unassigned @DarraghBurke on April 7, 2022, 8:09pm UTC



---

<div class="post-metadata">

**Author:** ![DarraghBurke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/darraghburke/32/10621_2.png) [@DarraghBurke](https://forum.babylonjs.com/u/DarraghBurke)\
**Post date:** [April 7, 2022, 8:09pm UTC](https://forum.babylonjs.com/t/gui-adaptwidthtochildren-not-working-with-adaptive-scaling/28911/10 "2022-04-07T20:09:27Z")

</div>

Glad to hear it!
