How to Add CSS Styles to a Gutenberg Block
In this lesson, I will show you two ways of adding CSS styles to your custom Gutenberg block: using inline styles and enqueueing stylesheet files with the help of the block.json file.
In the video below, we will continue working with the "Subscription form" block from this course to make it look nice.
Sorry, but you don’t have access to this video lesson.
Sign in to your account or get the course.
This is the result of how our custom block is going to look by the end of this lesson:

You can also download source files from the video using the link below:
Using Inline CSS Styles in Custom Gutenberg Blocks
Before I show you how to include CSS files into Gutenberg blocks, I would like to show you how you can use inline CSS real quick.
By the way, we will work with inline CSS more thoroughly anyway in the lesson about block color settings. As for now, I am just showing you the basics of how it works.
For example, let’s say I’d like to set the background color for my Gutenberg block. How can I achieve that with inline CSS?
Let’s start with the edit() method:
import { useBlockProps } from '@wordpress/block-editor';
export default function Edit() {
const inlineStyles = {
backgroundColor: '#f4faff'
}
return (
<div { ...useBlockProps( { style: inlineStyles } ) }>
...Then, the save() method:
import { useBlockProps } from '@wordpress/block-editor';
export default function Save() {
const inlineStyles = {
backgroundColor: '#f4faff'
}
return (
<div {...useBlockProps.save( { style: inlineStyles } )}>
...That’s pretty much it.
How to Include CSS (and SCSS) Files to Gutenberg Blocks
Now comes the interesting part of the lesson.
Quite a long time ago, if we wanted to enqueue a CSS file to a block, we had to use a special hook – enqueue_block_editor_assets and the wp_enqueue_style() function inside it. Is this method still working? Yes, of course, but I am sure that it would be better to use the block.json file for that purpose right now.
In order to do that, we need to add paths to the CSS files to block.json:
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "rudr/subscription",
...
"editorStyle": "file:./build/index.css",
"style": "file:./build/style-index.css"
}So far, we have two more parameters in the block.json file for now:
editorStyle– The CSS rules from this file will be applied to the block in the editor part only.style– The CSS rules from this file will be added to both the Block Editor part and the site front end part. However, on the website, the rules will appear in the<style>tag, not as an included CSS file.
There is also one important moment about these files we need to keep in mind. Are we going to create them in the build folder and work with them there? Of course, not! In the build folder, they should be minified and stuff. So, we will create them in the src folder then. However, if you want the CSS files to be automatically processed by @wordpress/scripts and appear inside the build folder, you need to import them inside the JavaScript entry point file, which is, in our case, /src/index.js.
import './editor.scss'
import './style.scss'
import './somecustomstyle.scss'Have you noticed something odd here? Yes, indeed, I am using SCSS files but not regular CSS files over here. And that’s another amazing thing about @wordpress/scripts – you can feel free to write styles for your custom Gutenberg block using SCSS, and as a result, everything will be compiled into regular CSS and also minified.
Another important thing we need to discuss is the names of stylesheet files you’re using inside the src folder. Depending on the file name, it will be compiled into either index.css or style-index.css, for example:
style-index.css– This file is generated from thestyle.scss(orstyle.css) file from thesrcfolder. If there is no such file, it won’t be created.index.css– This file is combined from all the other CSS/SCSS files in thesrcfolder. For example, in our case, it will beeditor.scssandsomecustomstyle.scssfiles.
The block.json file configuration also allows us to load an already registered CSS file. In the next chapter, I will show you how you can do that.
Include an already registered stylesheet or multiple stylesheets
Both editorStyle and style parameters accept an array of values. An array means that we can include multiple CSS files with a single parameter.
{
"style": [
"file:./build/style-index.css",
"misha-style"
]
}Everything should be clear with the first array element now, but what misha-style is?
Yes, it is a handle of any pre-registered CSS file. So you need to register misha-style first.
add_action( 'enqueue_block_assets', function() {
wp_register_style(
'misha-style',
plugin_dir_url( __FILE__ ) . 'assets/misha.css',
array(),
filemtime( plugin_dir_path( __FILE__ ) . 'assets/misha.css' )
);
} );Please keep in mind that the content of the misha.css file won’t be inserted directly between the <style> tags in the page HTML; the file is going to be included as usual.
Using enqueue_block_assets instead of block.json
I think, I am certain that I do not recommend you to use this method to add CSS to your Gutenberg blocks; however, maybe in your particular case, it is the only way, so I decided to mention it here as well.
This method was used widely and was the only way before the block.json file appeared.
So, we used wp_enqueue_style() and wp_register_style() functions as usual, plus the enqueue_block_editor_assets hook if we wanted to add CSS to the block editor part, or the enqueue_block_assets hook – for both the website front-end and editor parts.
Our code could look something like this:
add_action( 'enqueue_block_editor_assets', function() {
wp_enqueue_style(
'misha-style-1',
plugin_dir_url( __FILE__ ) . 'build/index.css',
array(),
filemtime( plugin_dir_path( __FILE__ ) . 'build/index.css' )
);
} );
add_action( 'enqueue_block_assets', function() {
wp_enqueue_style(
'misha-style-2',
plugin_dir_url( __FILE__ ) . 'build/style-index.css',
array(),
filemtime( plugin_dir_path( __FILE__ ) . 'build/style-index.css' )
);
} );That’s pretty much everything I wanted to show you about including CSS styles to Gutenberg blocks. You can always download the source files of the block using the link below:
However, if you’d like me to describe how block style variations work, please let me know in the comments section below.
Misha Rudrastyh
Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Great advices!
Hi Misha,
do you have an idea, how to make the CSS for the editor post-type-aware?
For example, I’d like to style .editor-post-title__input differently for Post and for Page.
I thought about running some javascript adding dynamically the classes with the results like:
.wp-block.editor-post-title__block.editor-post-type-pageHi,
Here is how you can do that
Great introduction to Gutenberg CSS additions. Thanks Misha!
Amazing! Thank you for writing this, it has really helped me out :D
Hey Misha, Thank you for the amazing post!
Would you have an idea about how I could separate the styling of the editor and of the front end?
I am using the amazing create-block and wp-env combo, but as the style.scss file declared in the block.js file gets bundled for both the editor and the front end, and as I am trying to build a full-height sticky off-canvas drawer block, you can understand which kind of difficulties I am encountering…
Thanks in advance for your answers!! Cheers!
According to the following page, you’re supposed to import editor.scss into edit.js, but I find it compiles into index.js (as you say).
https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/applying-styles-with-stylesheets/#enqueue-stylesheets:~:text=%E2%80%93%20In%20%60edit.js%60%20you%20would%20place%20%60import%20%E2%80%98./editor.scss%E2%80%99%3B%60
My solution was to add
"editorStyle": "file:index.css",into block.json and then wrap all my code inside editor.scss inside:where(.editor-styles-wrapper){ ... }but surely there’s a better way?Ah, you don’t need
:where(.editor-styles-wrapper){ … }. WordPress knows to only load index.css for the editor (this is confusing to me, why not call it editor.css?). style-index.css is for both frontend and editor.https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/#:~:text=This%20is%20for%20styles%20used%20only%20in%20the%20editor.