9-slice (also called nine-slice or 9-patch) scales a UI image by cutting it into nine pieces, so the corners stay sharp while the edges and centre stretch. One small drawing can become every dialog box, button and tooltip in your game, at any size.
What 9-slice is
Stretch a pixel art panel the normal way and its border gets fat and uneven. 9-slice avoids that by treating each part of the image differently:
| Piece | When the panel gets bigger |
|---|---|
| Corners | Never change |
| Top and bottom edges | Stretch sideways only |
| Left and right edges | Stretch up and down only |
| Centre | Stretches both ways |
Every tool has its own name for this, which helps when you’re searching its docs: NinePatchRect and StyleBoxTexture in Godot, Sprite Border in Unity, border-image in CSS, and slices in Aseprite.
Draw a panel that slices well
A 9-slice is only as good as the art going in. Four habits make the difference:
- Put all the detail in the corners. Rivets, curls and rounded edges belong there, because corners are the only part that’s never stretched.
- Keep edges plain along their length. A straight line stretches perfectly. A dotted or patterned edge turns into uneven dashes.
- Keep the centre flat. A solid colour fills any size. A gradient across the centre breaks into wide bands.
- Draw it small. 16 × 16 to 32 × 32 is plenty for most panels. Smaller art means crisper corners and less to redraw for each button state.
Set up the slice
- Open your image in the editor and pick the Slice tool (⇧C). It’s in the selection tool group.
- Drag a rectangle around your panel.
- In the options bar at the top, give the slice a name, such as
panel. - Turn on 9-Slice. A centre area appears, starting a third of the way in from each side.
- Select Preview to open the 9-Slice preview panel.
- Set L, T, R and B, the left, top, right and bottom margins in pixels. Make each one as wide as your corner art.
If your corners are 4 pixels wide, use 4 for all four. Tighter margins cut into the corners. Wider ones work too, but they leave less room for the panel to shrink.
The same panel also has X, Y, W and H for nudging the slice, and a Pivot checkbox for marking an anchor point.
Preview the stretch
The preview shows the real stretched result, pixel for pixel, while you work:
- Drag the corner handle to resize it, up to 128 × 128.
- Use 1×, 2×W, 2×H and 2×2 to jump to common sizes.
- Keep drawing. The preview updates with every stroke, so you can fix a corner and see it at size straight away.
Try a very wide size and a very tall size. If a corner looks squashed or an edge goes lumpy, adjust the margins or simplify that edge.
Copy it to CSS
Copy CSS gives you a border-image rule for web UI. CSS measures its slices from the edges of the whole file, so when your slice covers only part of the image, the rule carries its own copy of that part and works as soon as you paste it. Copy it again after you redraw the panel.
- With the slice selected, select Copy CSS in the preview panel and paste it into your stylesheet.
- If the slice covers the whole image, export it with Export to → PNG at 1× and replace
sprite.pngwith your file’s path.
.panel {
border-style: solid;
border-width: 4px 4px 4px 4px;
border-image-source: url("sprite.png");
border-image-slice: 4 4 4 4 fill;
border-image-repeat: stretch;
image-rendering: pixelated;
}
To show your pixels larger, multiply border-width and leave border-image-slice alone. For 3× pixels, use border-width: 12px.
Copy it to Godot
Copy Godot gives you a ready StyleBoxTexture resource for Godot 4. It stores the slice’s position, so the slice doesn’t need to fill the image, and one sheet can hold several panels.
- Export the image with Export to → PNG at 1×, so the positions match, and add it to your Godot project.
- Select Copy Godot in the preview panel.
- Create a text file in your project called
panel.tresand paste the text in. - Change
res://sprite.pngto the real path of your PNG. - In Godot, select a Panel or Button, open Theme Overrides → Styles, and load
panel.tres.
If it looks blurry, set Default Texture Filter to Nearest under Rendering → Textures → Canvas Textures in Project Settings.
Other engines
- Unity. There’s no copy button, but the numbers carry over. Read L, T, R and B from the preview, enter them as the sprite’s Border in Unity’s Sprite Editor, and set your UI Image’s type to Sliced.
- Aseprite. Slices, their 9-slice centres and pivots stay in
.asepritefiles in both directions. - Anything that reads JSON. A sprite sheet’s JSON lists every slice under
meta.sliceswith itsbounds,centerandpivot, scaled to match your export.
Common problems
The corners stretch. Your margins are smaller than your corner art. Raise L, T, R and B.
The CSS version looks scrambled. The PNG was exported at a scale other than 1×. Export at 1× again.
Godot shows nothing, or a missing resource. The path in the .tres file still says res://sprite.png. Point it at your PNG.
The Godot panel is cut in the wrong place. The PNG was exported at a scale other than 1×. Export at 1× again.
The border is thinner than the art. In CSS, border-width sets the size on screen. Multiply it by the scale you display your pixels at.
Where to go next
Take the rest of your art into the engine with Export to Godot. For the frames and sheets around your UI, see sprite sheets.