GUIDES FREE TOOLS OPEN APP
SpriteLab › Guides › 9-slice game UI

9-slice game UI: how it works in Godot, Unity and CSS

4 October 2026 · 6 min read · All guides

Almost every panel and button in a game is a small image drawn at many sizes. 9-slicing is how that works without the corners turning to mush. Here's what it is, how to set it up in each engine, and the mistakes that make UI look cheap.

What 9-slice actually does

Draw four lines across your image, two vertical and two horizontal. That gives nine regions. When the image is resized:

A small parchment panel with four border lines, 9-sliced to a wide panel with crisp corners, compared with a plain stretch where the corners and border smear

The four numbers you set are the border widths: left, top, right, bottom, in pixels of the source image. Same idea, different names: Godot calls them patch or texture margins, Unity calls it the sprite border, CSS calls it border-image-slice.

Godot 4

Two ways, depending on what the image is for.

A one-off panel: add a NinePatchRect, set its texture and the four patch_margin_* values. Set axis_stretch_horizontal and axis_stretch_vertical to Tile if the edge has a pattern.

Buttons and panels across the whole game: make a Theme, and give each button state (normal, hover, pressed, disabled, focus) a StyleBoxTexture with the same texture margins. Every Button in the scene then picks it up.

var sb := StyleBoxTexture.new()
sb.texture = preload("res://ui/button_normal.png")
sb.texture_margin_left = 12
sb.texture_margin_top = 10
sb.texture_margin_right = 12
sb.texture_margin_bottom = 14
add_theme_stylebox_override("normal", sb)

For pixel art, set the project's default texture filter to Nearest (Project Settings, Rendering, Textures), or it blurs. More on that in using pixel art in Godot 4.

Unity

  1. Select the sprite, open the Sprite Editor, and drag the green border handles, or type the L, T, R and B values. (If you set it from a script, spriteBorder is ordered left, bottom, right, top.)
  2. On the UI Image, set Image Type to Sliced (or Tiled for patterned edges). Leave Fill Center on unless you want a hollow frame.
  3. If the borders come out too thick or thin on screen, change Pixels Per Unit Multiplier on the Image rather than redrawing.
  4. For buttons, set the Button's Transition to Sprite Swap and assign the highlighted, pressed, selected and disabled sprites.

Pixel art also wants Filter Mode Point and Compression None on the import settings. The Unity guide has the rest.

Web (CSS)

CSS has 9-slice built in as border-image. The slice values go top, right, bottom, left (the CSS order), and fill keeps the centre.

.panel {
  border: 18px solid transparent;
  border-image: url(panel.png) 18 19 22 19 fill / 18px 19px 22px 19px stretch;
  image-rendering: pixelated; /* pixel art only */
}

The mistakes that make UI look cheap

If you're making the art too: SpriteLab's game UI generator suggests borders for every panel, button and bar from the art itself, keeps them the same across a button family, has a keep-centre mode for ornaments, and writes them straight into the Godot theme, the Unity package and a JSON file.

Frequently asked questions

What is 9-slice scaling?

9-slice (also called nine-slice or nine-patch) splits a UI image into a 3x3 grid with four border lines. The corners never scale, the edges stretch in one direction, and the centre stretches in both, so one small panel can be drawn at any size without blurring its corners.

How do I 9-slice in Godot 4?

Use a NinePatchRect node and set its patch margins, or put a StyleBoxTexture in a Theme and set its texture margins. The theme route is the one to use for buttons and panels, since each button state gets its own StyleBox.

How do I 9-slice in Unity?

Set the border (left, bottom, right, top) on the sprite in the Sprite Editor, then set the UI Image's Image Type to Sliced. For buttons, use the Sprite Swap transition and give each state its own sprite with the same borders.

Should borders stretch or tile?

Stretch for flat colour and simple bevels, tile for patterned edges like rope, chains or brick, where stretching visibly distorts the pattern.

Does 9-slice work for pixel art?

Yes, as long as you scale by whole numbers and use nearest-neighbour filtering. The borders scale with the art, so a 6 px border at 3x is 18 px.

Skip the slicing

Describe a screen, get a full UI kit with the borders already set for Godot and Unity.

BUILD A UI KIT 25 free credits + 5 daily. No card needed.

More reading

PIXEL ART IN GODOT 4

Nearest filtering, integer scaling and the reimport gotcha.

PIXEL ART IN UNITY

Point filtering, no compression, PPU and slicing sheets.

GAME UI GENERATOR

Full UI kits from one description, sliced and named.