Custom Preview Styles
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:
CMS.registerPreviewStyle(filePath);
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 totrueif you are providing a raw CSS string. Defaults tofalse.collection(string): The name of the collection whose entries the style applies to. Use_singletonsfor singletons. 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 thecollectionoption. If omitted, the style applies to all the files in the collection. This option requirescollection.
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.
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.
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.
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 or a singleton, use the file option along with collection.
// 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. itswidgetoption, e.g.string,markdownor the name of a custom field type.data-key-path: The field’s key path. For example,titlefor a top-level field,details.authorfor a field in an Object field orsections.0.headingfor 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, the type name follows in angle brackets, e.g.blocks.*<image>.src.
[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 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.
Showcase
Real-world examples of custom preview styles can be found in our showcase.