Spacing/Margin within a box container

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By molamar
:warning: Old Version Published before Godot 3 was released.

Hi@all,

in Godot the H/VBoxContainer and ButtonGroups are nice to arrange several elements within, but is there any option to have some space between them? Expand fills up the whole Container( as expected) but all looks very dense. In this case Margin doesnt work because it refers to the Container borders, not to the neighbour element (thats what i tried)
Thx for every suggestion.

I don’t see such a thing in BoxContainer, there is surprisingly very few methods in there fro arranging children.
You could try adding margins to the child elements instead, thought it’s not as handy.

Zylann | 2016-12-08 13:48

:bust_in_silhouette: Reply From: Brice

hbox.add_constant_override("separation", 4) ?

This did the job, thx!

molamar | 2016-12-08 21:22

This is not straightforward.
I was expecting to find this property in BoxContainer section, not in Control>Custom Constants.

Victor | 2018-12-03 20:59

:bust_in_silhouette: Reply From: itsabhiaryan

I’ve tested for GridContainer, call

add_constant_override("hseparation", 0) 

Above method overrides an integer constant in the theme resource the node uses. If the constant is invalid, Godot clears the override.

Godot 3.1

:bust_in_silhouette: Reply From: Dosedmonkey

Search sep, in the properties, and there is a custom constants that appears, tick seperation and then add how much you want. No code required.

:bust_in_silhouette: Reply From: idbrii

You can use add_spacer to add a space that can expand between elements.

To force specific spacing, you can add an empty label between items and force its size:

# visible label with text
var label := Label.new()
label.text = label_text
add_child(label)
# invisible label for padding
var spacing := Label.new()
add_child(spacing)
spacing.rect_min_size.x = 10
var other := Label.new()
other.text = "other stuff goes here"

Or in my case, I wanted to add labels after the fact, so I needed to move them around:

func add_empty_label() -> Label:
    var label := Label.new()
    add_child(label)
    move_child(label, 0)
    return label

func add_label(label_text: String):
    # invisible label for padding
    var spacing := add_empty_label()
    spacing.rect_min_size.x = 10
    # visible label with text
    var label := add_empty_label()
    label.text = label_text
    move_child(label, 0)
    return self