---
title: Custom Preview Styles
description: Customize preview pane styles in Sveltia CMS with CSS to match your site’s design.
---

Sveltia CMS comes with built-in styles for the entry preview pane. However, you can also register your own custom preview styles to make the preview look like your site.

## Overview

To register a custom preview style, use the `registerPreviewStyle` method on the [`CMS` object](/docs/api#accessing-the-cms-object):

```js
CMS.registerPreviewStyle(filePath);
```

```js
CMS.registerPreviewStyle(cssString, { raw: true });
```

There are two ways to register custom preview styles in Sveltia CMS: by providing a file path to a CSS file or by providing a raw CSS string. If you provide a raw CSS string, you need to set the `raw` option to `true`.

### Parameters

- `filePath` (string): The path to the CSS file containing the custom styles. This file should be accessible from the CMS admin interface. It can be a relative path, which is resolved against the current page URL, or an absolute URL.
- `cssString` (string): A string containing the raw CSS styles to be applied to the preview pane.
- `options` (object, optional): An options object that can contain the following properties:
  - `raw` (boolean): Set this to `true` if you are providing a raw CSS string. Defaults to `false`.
  - `collection` (string): The name of the collection whose entries the style applies to. Use `_singletons` for [singletons](/docs/collections/singletons#referencing-singleton-files). If omitted, the style applies to all collections.
  - `file` (string): The name of the file the style applies to, in the file collection or singleton collection given with the `collection` option. If omitted, the style applies to all the files in the collection. This option requires `collection`.

The function throws a `TypeError` if a parameter is invalid, e.g. if `filePath` is not a valid URL or file path, or if `file` is given without `collection`.

## Examples

### Registering a Preview Style from a File

To register a preview style from a CSS file, simply provide the file path as an argument to the `registerPreviewStyle` function.

```js
CMS.registerPreviewStyle('/path/to/your/custom-style.css');
```

### Registering a Preview Style from a Raw CSS String

You can also register a preview style by providing a raw CSS string. Make sure to set the `raw` option to `true` in the options object.

```js JavaScript
const customCSS = `
  body {
    background-color: lightgoldenrodyellow;
  }
`;

CMS.registerPreviewStyle(customCSS, { raw: true });
```

### Registering Multiple Preview Styles

You can register multiple preview styles by calling the `registerPreviewStyle` function multiple times. The styles will be applied in the order they were registered.

```js
CMS.registerPreviewStyle('/path/to/first-style.css');
CMS.registerPreviewStyle('/path/to/second-style.css');
```

This allows you to layer styles and create complex customizations for the entry preview.

### Applying Preview Styles to Specific Collections or Files

By default, a preview style applies to the entries of all collections. To apply it to the entries of a specific collection only, use the `collection` option. To apply it to a specific file in a [file collection](/docs/collections/files) or a [singleton](/docs/collections/singletons), use the `file` option along with `collection`.

```js
// All entries
CMS.registerPreviewStyle('/path/to/common-style.css');
// Entries in the `posts` entry collection
CMS.registerPreviewStyle('/path/to/post-style.css', { collection: 'posts' });
// The `about` file in the `pages` file collection
CMS.registerPreviewStyle('/path/to/about-style.css', { collection: 'pages', file: 'about' });
// The `home` singleton
CMS.registerPreviewStyle('/path/to/home-style.css', { collection: '_singletons', file: 'home' });
```

The styles that apply to an entry are combined in the order they were registered. In this example, the preview of the `about` file uses both `common-style.css` and `about-style.css`. An entry that no preview style applies to is previewed with the built-in styles only.

## Styling Specific Fields

The default preview marks each field with three attributes that identify it, which your preview styles can use to target specific fields:

- `data-field-type`: The field’s type, i.e. its `widget` option, e.g. `string`, `markdown` or the name of a [custom field type](/docs/api/field-types).
- `data-key-path`: The field’s key path. For example, `title` for a top-level field, `details.author` for a field in an Object field or `sections.0.heading` for a subfield of the first item in a List field.
- `data-typed-key-path`: The same path with every List item index replaced with an asterisk, e.g. `sections.*.heading`. One selector then matches the subfield in all the items. For a List or Object field with [variable types](/docs/fields/list#variable-type), the type name follows in angle brackets, e.g. `blocks.*<image>.src`.

```css
[data-key-path='title'] p {
  font-size: 2em;
}

[data-typed-key-path='sections.*.body'] p {
  font-family: serif;
}

[data-field-type='markdown'] {
  line-height: 1.8;
}
```

A [custom preview template](/docs/api/preview-templates#linking-to-the-edit-pane) has these attributes only where it adds them itself, apart from the field previews that `widgetFor` and `widgetsFor` return.

The fields in the Edit Pane have the same attributes, so you can also style them with CSS on your admin page. Once a preview style applies to the entry, the preview is rendered in an iframe. Your admin page styles then don’t affect the preview, and your preview styles don’t affect the Edit Pane.

The attributes are primarily for internal use. The rest of the markup, such as class names and element structure, may change in any release, so keep your selectors as simple as possible.

:::tip[Why “key path”?]

The term comes from the [IndexedDB API](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Basic_Terminology#key_path), where a key path is a dot-separated path to a value in an object. Sveltia CMS handles entry data as a flattened object keyed by these paths, so the term is used throughout the app and its API, e.g. in the `widgetFor` prop of a [custom preview template](/docs/api/preview-templates#component-props).

:::

## Showcase

Real-world examples of custom preview styles can be found in our [showcase](/showcase?feature=preview-styles).
