Why is my Topbar and Sidebar anchoring incorrectly in Godot 4?

Godot Version

V4

Question

`I’m building a UI in Godot 4 where I have a Main scene with the following nodes: Background (ColorRect or TextureRect. I have tried both), Topbar (HBoxContainer), Sidebar (VBoxContainer), CanvasArea (Control), and others.

Everything other than Main is rendering in the lower right quadrant.

Everything is done with anchors and I have tried to manually set anchors, change node types, change the script to force. Nothing works.

Stretch mode viewport is probably your issue, is there any reason you want that setting?

No reason in particular. I am still getting the hang on Godot. Viewport makes sense to me logically, but there is no other reason.

Try out canvas_items instead; you haven’t posted what your anchors look like and what you want the screen to show, that might help more if it persist with different stretch mode/aspect settings.

I ended up trying to force it with the following in Main.gd:

func _ready():
    # Force Main (self) to Full Rect anchors
    anchor_left = 0.0
    anchor_top = 0.0
    anchor_right = 1.0
    anchor_bottom = 1.0
    offset_left = 0.0
    offset_top = 0.0
    offset_right = 0.0
    offset_bottom = 0.0

    # Then fix background
    background.anchor_left = 0.0
    background.anchor_top = 0.0
    background.anchor_right = 1.0
    background.anchor_bottom = 1.0
    background.offset_left = 0.0
    background.offset_top = 0.0
    background.offset_right = 0.0
    background.offset_bottom = 0.0
    background.stretch_mode = TextureRect.STRETCH_SCALE
    background.expand_mode = TextureRect.EXPAND_IGNORE_SIZE

This is what renders.

The pink background (ColorRect) should cover the whole main node, not just the bottom right quadrant.

Your camera is interfering. Maybe a better scene-structure would be your 2D nodes as the root, then child the UI it’s own CanvasLayer. Do you intend to have Node2D elements or only user interface? If only UI then remove the Camera2D entirely

1 Like

Perfect! That was it. Thank you so much!