Container in Godot – Complete Guide

Welcome to our journey through the world of containers in Godot 4, a topic that unfolds a lot of potential in the realm of game GUI development. Containers are foundational elements in creating user interfaces that are dynamic, adaptable, and visually appealing. Whether you’re taking your first steps in game development or looking to polish your UI skills, understanding how containers work in Godot 4 will equip you to craft better gaming experiences.

What Is a Container in Godot 4?

A Container is a special type of Control node in Godot 4 that serves as the backbone for building user interfaces in games. Its primary function is to automatically manage the arrangement and scaling of its child nodes, providing a structured and systematic approach to UI design.

What Are Containers Used For?

Containers are used to create complex GUI layouts without the hassle of manually positioning each element. They ensure that UI elements behave predictably during screen resizing, orientation changes, or any dynamic updates that may occur during gameplay. In Godot, several types of containers specialize in different layouts, such as grids, lists, or even responsive designs with relative sizing.

Why Should You Learn About Containers?

Understanding containers is crucial for any game developer because:

  • They save time by simplifying the UI design process with automatic layout management.
  • They elevate the quality of the game with a professional and user-friendly interface.
  • They are essential for accommodating different screen sizes and aspect ratios, making your games more accessible and enjoyable on various devices.

By mastering the use of containers, you will enhance not only the aesthetics but also the functionality of your game interfaces.

CTA Small Image

Creating a Basic Container

To start with Godot containers, we’ll first create a basic container that will automatically manage the children nodes within it.

var container =

In the above example, we create a new instance of VBoxContainer, which is a type of container that arranges its children in a vertical list. We then add it to the current node using add_child().

Adding Elements to a VBoxContainer

Now let’s add some elements to our vertical box container. These could be buttons, labels, or any other Control nodes.

var button1 =
button1.text = "Button 1"

var button2 =
button2.text = "Button 2"

var label =
label.text = "I'm a label"

This script creates two buttons and a label, then adds them to the VBoxContainer. The container will automatically position these elements vertically.

Customizing Child Elements in Containers

You can customize the elements within the container by adjusting their size, margin, or any other property that influences the layout.

var button1 =
button1.text = "Button 1"
button1.rect_min_size = Vector2(200, 40)

Here, we’re setting a minimum size for the first button. This ensures that the button has a specific size within the container, irrespective of the container’s overall size.

Working with HBoxContainer

Now let’s switch to a horizontal container. HBoxContainer works similarly to VBoxContainer, but it arranges elements horizontally.

var hbox =

var icon =
icon.texture = preload("res://icon.png")

var label =
label.text = "Icon Label"

In the code above, we created a new HBoxContainer, adding to it a TextureRect (to display an image) and a Label. They are arranged side by side within the horizontal container.

Using Containers for Responsive UIs

Containers can be nested within one another to create complex and responsive UI layouts. Here’s an example of how to nest an HBoxContainer within a VBoxContainer to create a flexible layout.

var vbox =

var hbox =

var button1 =
button1.text = "Nested Button 1"

var button2 =
button2.text = "Nested Button 2"

var label =
label.text = "I am below the HBoxContainer"

This snippet demonstrates nesting: an HBoxContainer is placed within a VBoxContainer, allowing horizontal layout within a vertical layout. This kind of nesting is great for complex user interfaces that automatically adapt to their given space.

Margins and Separation

To refine the interface, Godot containers allow you to adjust the separation and margins between elements. This can be done for each container to fine-tune the UI.

var hbox =
hbox.custom_constants_separation = 10  // Adds a 10 pixel space between elements

// Add elements to hbox as previously shown

This example sets the separation of the HBoxContainer, introducing a space between the elements to make the UI cleaner and more readable.

With these examples, you’re now equipped with the basics of creating and manipulating containers in Godot 4. By understanding these foundations, you can start designing more intricate and responsive user interfaces that will improve your game’s overall presentation.

As we delve deeper into UI design with Godot 4, it’s time to explore more advanced container types and their practical applications. With the following examples, we aim to showcase the versatility and utility of containers for creating refined and adaptable interfaces.

Let’s consider a scenario where you want elements to take up all available space within a container. We’ll use the MarginContainer to show how margins can be used to control space allocation around the container’s border.

var margin_container =
margin_container.rect_min_size = Vector2(400, 400)
margin_container.margin_top = 20
margin_container.margin_bottom = 20
margin_container.margin_left = 20
margin_container.margin_right = 20

var label =
label.text = "I'm centered with margins!"

In the snippet above, the MarginContainer ensures a 20-pixel margin on all sides. This is helpful when you want to keep elements from stretching to the edges of the screen or overlapping with other UI elements. Adding a Label within this container places the text in the center, respecting the defined margins.

Moving forward, let’s say we want to align certain elements to the center of the screen, regardless of the resizing actions. For this, we can use the CenterContainer.

var center_container =
center_container.rect_min_size = Vector2(200, 200)

var centered_label =
centered_label.text = "Always in the center"

With the code above, the CenterContainer automatically centers its child nodes, both horizontally and vertically, within its bounding rectangle.

Now, let’s tackle a grid layout by using GridContainer, which distributes elements evenly in grid fashion.

var grid_container =
grid_container.columns = 3

for i in range(9):
    var button =
    button.text = "Grid Button %d" % i

This GridContainer is specified to have 3 columns, and we populate it with 9 buttons, resulting in a 3×3 grid. This kind of container is perfect for inventory systems, toolbars, or any UI element where a structured layout is desired.

The ScrollContainer is another container that adds scrolling functionality to elements that exceed the available view.

var scroll_container =
scroll_container.rect_min_size = Vector2(300, 300)

var vbox_inside_scroll =

for i in range(20):
    var label =
    label.text = "Item %d" % i

This ScrollContainer has an inner VBoxContainer filled with numerous labels. If our labels exceed the height of the ScrollContainer, a scrollbar appears, allowing users to scroll through the content.

These various container types in Godot 4 empower you to create sophisticated UI layouts efficiently. By learning and applying these containers in Godot 4, you can address almost any UI challenge that your game design may present.

Remember, while we’ve provided examples individually, combining different containers is key for creating truly dynamic and complex interfaces. As you experiment with these containers, you’ll quickly see how they can work together to build user interfaces that look good, function well, and provide a great user experience across all devices and resolutions.

Expanding our toolbox, let’s touch upon more nuanced Godot 4 containers that cater to specific layout needs. One such container is the SplitContainer, which allows the user to adjust the division between two elements interactively.

var split_container =
split_container.rect_min_size = Vector2(300, 200)

var left_panel =

var right_panel =

The SplitContainer above divides the screen into two panels with a draggable separator. This is ideal for options menus, tool panels, or any interface that necessitates resizable sections.

Another useful container is the TabContainer, perfect for interfaces with multiple tabs.

var tab_container =
tab_container.rect_min_size = Vector2(400, 300)

for i in range(4):
    var tab_content = = "Tab %d" % i

Here, we create a TabContainer with four tabs, each represented by a Panel. The name property of each Panel dictates the title of the tab, offering a tidy method for segmenting content.

A case in which we need elements stacked on top of each other, but only showing one at a time, calls for a StackContainer.

var stack_container =
stack_container.rect_min_size = Vector2(250, 150)

for i in range(3):
    var panel = = "Panel %d" % i
    panel.visible = i == 0  # Only the first panel visible by default

The StackContainer allows us to add multiple panels, but it only shows the one that has been set as visible. This suits scenarios such as wizard interfaces or sequential forms.

When dealing with lists, the ItemList container offers a simple way to manage item selection.

var item_list =
item_list.rect_min_size = Vector2(200, 100)

for i in range(10):
    item_list.add_item("Item %d" % i)

ItemList provides a straightforward method to display a list of selectable items, which could represent inventory objects, menu options, or settings. It manages selection and highlighting, which simplifies the coding required for such functionality.

For scenarios that demand an auto-wrapping layout where the contents flow to the next row or column when space is limited, we can make use of the FlowContainer.

var flow_container =
flow_container.rect_min_size = Vector2(300, 200)
flow_container.vertical_spacing = 5
flow_container.horizontal_spacing = 5

for i in range(15):
    var button =
    button.text = "Button %d" % i

In this FlowContainer example, when the buttons run out of horizontal space, they automatically wrap to the next line. The vertical_spacing and horizontal_spacing properties help configure the gap between elements for a cleaner UI.

Lastly, when creating dialog boxes or sections where content should be framed, PanelContainer is the go-to container.

var panel_container =
panel_container.rect_min_size = Vector2(250, 150)

var dialog_label =
dialog_label.text = "Your adventure begins..."

The PanelContainer wraps a Label in this example, creating a dialog box-like appearance. This is ideal for message boxes, tooltips, or any UI element that requires emphasis.

Combining these containers to create intuitive and responsive UIs is where the true power lies in Godot 4’s UI system. By leveraging these various container nodes, developers can solve diverse layout challenges with precision and polish, enhancing the player’s interaction with the game. Each container type serves its purpose and can be further customized to fit the specific needs of your game’s UI design, making your creation process both efficient and enjoyable. Continue experimenting with these containers to develop a deep understanding of their capabilities and discover the endless possibilities for UI design in your Godot 4 projects.

Continue Your Godot Game Development Journey

Mastering containers in Godot 4 is just the beginning of your game development adventure. To further hone your skills and delve into more advanced topics, consider continuing your learning path with comprehensive resources like our Godot Game Development Mini-Degree. This extensive collection of courses is tailor-made to guide you through the process of building cross-platform games with Godot 4, covering essentials like 2D and 3D assets, gameplay mechanics, and UI systems.

Whether you’re a beginner eager to get started or an experienced developer looking to specialize in Godot 4, our Mini-Degree has something for everyone. Access the courses 24/7, learn at your own pace, and build a portfolio of impressive Godot projects. Plus, for a broader range of tutorials to explore, don’t miss our full catalog of Godot courses.

We at Zenva are committed to helping you transition from beginner to professional, offering over 250 courses that are sure to boost your career. By choosing Zenva, you’ll gain the knowledge and hands-on experience needed to confidently create your own games and carve out a successful path in the game development industry.


In your quest to become a proficient game developer with Godot 4, mastering the use of containers is a crucial step. As we’ve explored, containers can revolutionize the way you approach UI design, providing simplicity, flexibility, and responsiveness to your projects. Embrace the power of containers and the multitude of other features in Godot 4, and you’ll be well on your way to creating immersive and polished games that captivate players.

Remember, the learning doesn’t stop here. Dive deeper, challenge yourself, and continue building your expertise with Zenva’s Godot Game Development Mini-Degree. Each lesson is a step forward in your development journey, and we’re excited to be a part of that journey, providing you with the knowledge and tools to bring your creative visions to life. Start today and transform your passion for game development into a tangible and rewarding skill set!

Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.