Overview
Containers are specialized Control nodes that automatically arrange their children. They handle positioning, sizing, and spacing without manual intervention, making it easier to create responsive, adaptive UIs.All containers inherit from the
Container base class and only work with children that inherit from Control.Container Base Class
TheContainer class provides the foundation for all container types:
- Automatically arranges child controls
- Emits signals when children need to be sorted
- Provides accessibility region support
- Mouse filter set to
MOUSE_FILTER_PASSby default
Important Signals
pre_sort_children- Emitted before sorting childrensort_children- Emitted when children need to be sorted
Box Containers
VBoxContainer
Arranges children vertically from top to bottom:HBoxContainer
Arranges children horizontally from left to right:Grid Container
Arranges children in a grid layout with a specified number of columns:columns- Number of columns in the grid
h_separation- Horizontal spacing between children (default: 4)v_separation- Vertical spacing between children (default: 4)
Size Flags
Control how children behave within containers using size flags:| Flag | Description |
|---|---|
SIZE_SHRINK_BEGIN | Shrink to minimum size, aligned to start |
SIZE_SHRINK_CENTER | Shrink to minimum size, centered |
SIZE_SHRINK_END | Shrink to minimum size, aligned to end |
SIZE_FILL | Fill available space |
SIZE_EXPAND | Request extra space |
SIZE_EXPAND_FILL | Expand and fill (common combination) |
Other Container Types
MarginContainer
Adds margins around its single child:CenterContainer
Centers its single child:PanelContainer
Provides a themed panel background for its children:ScrollContainer
Provides scrolling for content larger than its visible area:Best Practices
Use nested containers
Use nested containers
Combine different container types to achieve complex layouts. For example, use VBoxContainer for vertical sections and HBoxContainer within each section for horizontal arrangements.
Set minimum sizes
Set minimum sizes
Define
custom_minimum_size on controls to ensure proper layout, especially in containers that expand children.Understand size flags
Understand size flags
Master the size flags system to control how elements behave in different screen sizes and orientations.
Test different resolutions
Test different resolutions
Always test your UI at different resolutions to ensure containers adapt properly.
See Also
Control Nodes
Learn about the Control base class and common UI elements
Themes
Customize container appearance and spacing