Editor styles in a classic WordPress theme
When the block editor does not use the theme’s fonts, widths and colours, editors write in one layout and publish in another. A classic theme fixes it by giving the editor its own stylesheet with add_editor_style(). WordPress prefixes the rules for the editor canvas by itself, so the file can use the same selectors as the front end.
Problem: In the editor, posts appear in the default system font at full width, while the site uses a serif font, a 720 px content column and custom heading sizes.
Solution: Declare add_theme_support( 'editor-styles' ) and load a stylesheet with add_editor_style() that sets the typography and content width with normal selectors; WordPress scopes it to .editor-styles-wrapper. Add block style variants with register_block_style() and per-block CSS with wp_enqueue_block_style().
Load the editor stylesheet
add_action( 'after_setup_theme', function () {
add_theme_support( 'editor-styles' );
add_editor_style( 'css/editor.css' );
} );
The path is relative to the theme folder. A Google Fonts URL can be passed too: add_editor_style( 'https://fonts.googleapis.com/css2?family=Merriweather&display=swap' ).
The stylesheet
/* css/editor.css */
body {
font-family: Merriweather, Georgia, serif;
font-size: 1.125rem;
line-height: 1.7;
color: #1a1a1a;
}
h2 { font-size: 1.75rem; margin-top: 2rem; }
h3 { font-size: 1.375rem; margin-top: 1.5rem; }
.wp-block {
max-width: 720px;
}
.wp-block[data-align="wide"] { max-width: 1100px; }
.wp-block[data-align="full"] { max-width: none; }
WordPress rewrites body to .editor-styles-wrapper and prefixes the other selectors, so the rules do not leak into the editor’s own interface. Writing .editor-styles-wrapper h2 by hand is not needed. The best approach is to share the typography part with the front end: build editor.css from the same Sass partials as style.css.
Block style variants
add_action( 'init', function () {
register_block_style( 'core/image', [
'name' => 'ha-rounded',
'label' => __( 'Rounded', 'textdomain' ),
] );
} );
.wp-block-image.is-style-ha-rounded img {
border-radius: 1rem;
}
Put the CSS of the variant into both stylesheets, the front end and the editor one. More about variants and removing core ones: register_block_style().
CSS only where a block is used
add_action( 'init', function () {
wp_enqueue_block_style( 'core/quote', [
'handle' => 'ha-quote',
'src' => get_theme_file_uri( 'css/blocks/quote.css' ),
'path' => get_theme_file_path( 'css/blocks/quote.css' ),
] );
} );
wp_enqueue_block_style() (WordPress 5.9+) loads the file in the editor and, on the front end, only on pages that contain the block, if the site loads block assets separately (block themes by default, classic themes with the should_load_separate_core_block_assets filter). With path, small files are inlined instead of being a separate request.
NOTE: Block themes do not need most of this: the styles in theme.json apply to the editor and the front end at once. In a classic theme, add_theme_support( 'editor-styles' ) is required; without it, add_editor_style() only affects the classic TinyMCE editor.