9 Slice in Godot 4: NinePatchRect Margins and Modes
How to 9 slice a panel in Godot 4 with NinePatchRect: the four margins, Stretch, Tile and Tile Fit, tested in 4.7.2, and a quick way to check it.

You drew a 48 x 48 panel with a nice border and corner studs. It looks right at that size. Then the game needs it 300 pixels wide for a dialogue box, and 90 wide for a tooltip. You set the panel's size in Godot and the studs stretch into ovals, the border gets thick and blurry, and the whole thing looks pulled out of shape.
The fix is 9 slice scaling, called nine patch in Godot. You tell the engine where to cut the picture into nine parts. The four corners are drawn as they are, the four edges stretch in one direction, and the middle fills the rest. Godot's node for this is NinePatchRect. Everything below was run in Godot 4.7.2 with a 16 x 16 test texture whose pixels we could read back, so the numbers are what the engine drew.
Set up NinePatchRect
Add a NinePatchRect node, drag your PNG into its Texture slot, then open the Patch Margin group in the Inspector. There are four numbers: Left, Top, Right and Bottom. Each is the distance in texture pixels from that edge to the cut line. Put 4 in all four and a 16 x 16 piece becomes four 4 x 4 corners, four edges and an 8 x 8 middle.

The same thing in code:
var panel := NinePatchRect.new()
panel.texture = load("res://panel.png")
panel.patch_margin_left = 4
panel.patch_margin_top = 4
panel.patch_margin_right = 4
panel.patch_margin_bottom = 4
panel.size = Vector2(96, 48)
add_child(panel)
Some things we checked on the way:
- All four margins start at 0, and the node starts in Stretch mode with Draw Center on.
- With margins of 4, the node reports a minimum size of 8 x 8, the corners touching.
- Pixel art needs the Nearest texture filter, or the stretched parts blur. Set Project Settings > Rendering > Textures > Canvas Textures > Default Texture Filter to Nearest once for the whole project. Our tests ran with it set that way.
Use one piece of a bigger sheet
UI art usually lives on a sheet with many pieces. Set Region Rect to the piece's rectangle (x, y, width, height) and the node only reads that part of the texture. In our test a 32 x 16 texture, red on the left half and green on the right, drew only green with a region of 16, 0, 16, 16. Count the margins from the edges of the region, not of the whole sheet.

Stretch, Tile and Tile Fit
The Axis Stretch group has two settings, Horizontal and Vertical, and each can be Stretch, Tile or Tile Fit. They change what happens to the edges and the middle. The corners never change. In code the values are NinePatchRect.AXIS_STRETCH_MODE_STRETCH (0), AXIS_STRETCH_MODE_TILE (1) and AXIS_STRETCH_MODE_TILE_FIT (2).
We gave the 4-pixel-margin piece a middle whose eight columns each had their own colour, made the node 28 pixels wide (a 20-pixel middle) and read a row of pixels back. Numbering the source columns 0 to 7, this is what the middle showed:
| Mode | The 20 middle pixels |
|---|---|
| Stretch | 0 0 1 1 1 2 2 3 3 3 4 4 5 5 5 6 6 7 7 7 |
| Tile | 0 to 7, then 0 to 7 again, then 0 to 3 cut off |
| Tile Fit | three whole copies, each squeezed to about 6.7 pixels wide |
Stretch is right for flat or smooth art, like a plain colour, a soft gradient or a simple border line. On pixel art with detail in the middle, the scale is rarely a whole number, so some source pixels come out wider than their neighbours. A 20-pixel middle from an 8-pixel one is 2.5 times, and the first row of the table shows it: columns of 2 and 3 pixels mixed.
Tile repeats the middle at its original size, starting from the left of the middle area, and cuts the last copy off wherever the area ends. Pixels stay square. If the pattern is a brick wall or a dotted fill, a cut-off brick at the edge is easy to spot.
Tile Fit works out how many copies fit, rounds to the nearest whole number and stretches them a little so none is cut. It rounds both ways. Our 8-pixel tile in a 17-pixel middle gave 2 copies of 8.5 pixels, and in 13 pixels it still gave 2. A 22-pixel middle gave 3. Because the stretch is small, Tile Fit is usually the best look for patterned fills at sizes that aren't a multiple of the tile.
The edges follow the same setting on their own axis, so a panel can tile across and stretch down.
Draw Center off for frames
Turn Draw Center off and the middle is left empty, so you see whatever is behind. This is the setting for a frame around a picture or a highlight box around an item. We checked it: with it off, the pixel in the middle of a 28 x 28 node was the background colour, and with it on it was the texture.
The same thing in a scene file or a StyleBox
If you want to write or generate the scene yourself, this loads in Godot 4 as it is (it needs a panel.png next to it):
[gd_scene load_steps=2 format=3]
[ext_resource type="Texture2D" path="res://panel.png" id="1"]
[node name="Panel" type="NinePatchRect"]
offset_right = 96.0
offset_bottom = 48.0
texture = ExtResource("1")
region_rect = Rect2(0, 0, 16, 16)
patch_margin_left = 4
patch_margin_top = 4
patch_margin_right = 4
patch_margin_bottom = 4
axis_stretch_horizontal = 2
axis_stretch_vertical = 1
Here axis_stretch_horizontal = 2 is Tile Fit and axis_stretch_vertical = 1 is Tile. A node suits a free-standing panel. For buttons and panels that follow a Theme, use a StyleBoxTexture, which is the same idea with different names: texture_margin_left, texture_margin_top, texture_margin_right, texture_margin_bottom, axis_stretch_horizontal, axis_stretch_vertical and draw_center.
[gd_resource type="StyleBoxTexture" load_steps=2 format=3]
[ext_resource type="Texture2D" path="res://panel.png" id="1"]
[resource]
texture = ExtResource("1")
texture_margin_left = 4.0
texture_margin_top = 4.0
texture_margin_right = 4.0
texture_margin_bottom = 4.0
axis_stretch_horizontal = 2
Assign it to a Button with add_theme_stylebox_override("normal", style), or put it in your Theme resource. We loaded this file and used it on a Button; its minimum size came out as 8 x 8 with margins of 4.
Pick margins that don't cut into the art
Where you cut matters more than any setting above. The cut line has to sit just inside the corner decoration, and everything in the edge and middle bands should look the same along the direction it stretches. If a stretched column contains a rivet, a gem or a gradient step, that detail is repeated into a stripe. If you cut too far in, part of the corner ends up in the edge and gets smeared.
Finding the numbers by eye is slow, and you only see a mistake after you run the game. The check you want is to resize the panel a few times and look at it.
The fast way: the 9-Slice Previewer
The 9-Slice Previewer does that check in your browser, drawing the panel the way Godot 4 does. Your art is never uploaded. Open it here: 3 days for $1 opens it on your own panels, with every other tool and up to 10 packs.
- Drop a panel, a button, or a whole UI sheet, with its JSON if you have one, and pick a piece.
- Drag the four guides over the piece, or press Find margins to get the smallest margins that stretch cleanly.
- Drag the panel's corner to resize it and choose Stretch, Tile or Tile Fit across and down. Columns or rows that would smear turn red, and a tiled middle that would end on part of a tile gets the nearest sizes that hold whole tiles. The same piece shows as a small box, a wide bar and a tall window side by side.
- Download the piece: a NinePatchRect scene, a StyleBoxTexture, CSS border-image classes, a JSON of the margins, the panel drawn at your size, and the margins written out for Unity, GameMaker, Phaser, Ren'Py, Defold and Unreal.
Every Gridwright asset pack, made to fit together, and all 44 tools for the fiddly work, with every new release, for $17 a month. Try it in your own game first: 3 days for $1 with every tool and up to 10 packs. Cancel any time.
Start: 3 days for $1 See everything it includesFAQ
What is the difference between 9 slice and NinePatchRect?
They are the same method. 9 slice is the general name and NinePatchRect is Godot's node for it. CSS calls it border-image.
Why is my NinePatchRect blurry?
Godot's default texture filter for 2D is Linear, which smooths pixel art as soon as it scales. Set the Default Texture Filter to Nearest in the Project Settings, or the node's Texture > Filter.
How do I use a piece from a sprite sheet?
Set Region Rect to the piece. Count the margins from the edges of that region.
Should I use Tile or Tile Fit?
Use Tile Fit for a patterned middle on panels of changing size. Use Tile when the pattern must keep its exact pixel size and a cut-off last copy is fine.
Get your panels right before they ship
Open the 9-Slice Previewer, drop your panel and look at it at three sizes. For pieces already cut for this, the UI Kit has pixel art panels, buttons and bars. The tool and the pack are part of Gridwright: 44 tools and 150 asset packs, 3 days for $1, then $17 a month.
Every Gridwright asset pack, made to fit together, and all 44 tools for the fiddly work, with every new release, for $17 a month. Try it in your own game first: 3 days for $1 with every tool and up to 10 packs. Cancel any time.
Start: 3 days for $1 See everything it includes