How to have a label auto-expand its parent, but keep its margins

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sjml

This is apparently harder than it might seem at first. This repo has my current attempt – obviously there’s something obvious I’m missing, but I can’t quite figure out how the margins interact with the various containers.

Cut and paste from the README.md:

Requirements:

  1. Box remains at fixed width
  2. Text can be arbitrarily long (within reason; not pasting the text of Moby Dick in there)
  3. InfoBox resizes itself to accommodate longer text
  4. InfoBox has a colored background
  5. Text is set away from edges of the box by margins.

1-3 are accomplished by putting the label inside a VBoxContainer. Alas, VBoxContainers cannot themselves be styled, so I put it inside of a PanelContainer to give the colored background, satisfying 4.

Number 5 is where I run into problems. I cannot figure out the logic of what margins are allowed to be set and which are fixed. If I try to change the margins on the VBoxContainer, they snap back. At one point I had the margins set up properly but they went back to zero once the text changed. (I can’t reproduce this at the moment.)

:bust_in_silhouette: Reply From: sjml

Solved this with some help from Reddit.

The answer is the MarginContainer, as I had hoped, but the trick is noting that the part of MarginContainer that works its magic is in the Custom Constants, not the regular margins.