In Sketch, a slice is a type of layer that you can draw over an area in the Canvas that will export the content within as an image.
As slices are just another layer type in Sketch, they can be selected, resized, moved, and grouped along with other layers. As well as being able to individually hide slices, all slices can be hidden and shown within a document by toggling the Slice button at the bottom of the Layer List.
To create a slice, choose Insert › Slice from the pop-up menu (or press S). With the tool selected, click-and-drag anywhere in the Canvas to define the area. In addition, when the Slice tool is enabled, you can also just click on a layer and a new slice will be placed around it.
Slices, along with any other layer can be named via the Layer List. Simply press Command-R with a layer selected. Giving your slice an appropriate name is important as this will also be the filename when the slice is exported.
A neat trick: if you include a slash (/) in the layer name, it will place that slice in a new folder. For example, if you named your Slice
foo/bar.png, it would first create a folder named
foo and then create a image named
bar.png in there.
When a slice is selected, you can change the export settings at the bottom of the Inspector. You can even export one slice into multiple images that can each be a different size or format. Just click on the add button to include a new scale.
Just like with the Make Exportable action, inserted slices will appear with all the scales and settings that have been defined in its default preset.
To learn more about this feature in greater detail, read the previous Make Exportable section.
Trim Transparent Pixels
This option in the Inspector is used to automatically remove any superfluous ‘empty’ pixels from an exported image. When you select the Trim Transparent Pixels checkbox, observe the export preview below…
Transparency is represented by a white and grey checkerboard, and when a slice is bigger than what you want to export, you will see this pattern appear around the edges on your preview. When Trim is selected, the height and width of the Export will change to fit your content. The export preview is an accurate representation of what your exported image will look like. You can even click-and-drag this outside of the Sketch window to export.
Export Group Contents Only
If your slice is in a group, the Export Group Contents Only checkbox will become active. If selected, it’ll do what it says on the tin. The slice will only export content within that group—meaning any content above or below on the Canvas will be excluded. This is perfect for exporting an icon from a design, without having to first separate it from any background layer.
Selecting the Background Color checkbox will remove any transparency from a slice, and replace it with a color of your choosing. This is ideally used when exporting to a filetype such a JPG that does not support transparency.
To see a quick tutorial on exporting slices, take a look at the video here.