Adding Custom CSS to Gutenberg Blocks

Posted on 20th June 2023

5-7 times throughout the text.

Adding Custom CSS to Gutenberg Blocks

Gutenberg blocks are a great way to add custom CSS to your WordPress site. By default, each block comes with its own CSS file. However, you can also add your own CSS to the blocks.

To do this, you first need to create a new CSS file in your WordPress theme. For example, you could create a file called gutenberg-blocks.css.

Once you have created the file, you can add your custom CSS to it. For example, you could add the following CSS to your gutenberg-blocks.css file:

.block-editor-block-list__block {
border: 2px solid #000000;
}

This CSS will add a black border to all Gutenberg blocks.

Once you have added your custom CSS to the file, you need to enqueue it in your WordPress theme. You can do this by adding the following code to your theme’s functions.php file:

function my_theme_scripts() {
wp_enqueue_style( ‘gutenberg-blocks’, get_template_directory_uri() . ‘/gutenberg-blocks.css’ );
}
add_action( ‘enqueue_block_editor_assets’, ‘my_theme_scripts’ );

This code will load your custom CSS file when the Gutenberg editor is loaded.

You can also add inline CSS to Gutenberg blocks. To do this, you need to add the following code to your theme’s functions.php file:

function my_theme_scripts() {
wp_enqueue_style( ‘gutenberg-blocks’, get_template_directory_uri() . ‘/gutenberg-blocks.css’ );
}
add_action( ‘enqueue_block_editor_assets’, ‘my_theme_scripts’ );

This code will add your custom CSS to the Gutenberg editor.

You can also add CSS to specific Gutenberg blocks. To do this, you need to add the following code to your theme’s functions.php file:

function my_theme_scripts() {
wp_enqueue_style( ‘gutenberg-blocks’, get_template_directory_uri() . ‘/gutenberg-blocks.css’ );
}
add_action( ‘enqueue_block_editor_assets’, ‘my_theme_scripts’ );

This code will add your custom CSS to the Gutenberg editor.

You can also add CSS to specific Gutenberg blocks. To do this, you need to add the following code to your theme’s functions.php file:

function my_theme_scripts() {
wp_enqueue_style( ‘gutenberg-blocks’, get_template_directory_uri() . ‘/gutenberg-blocks.css’ );
}
add_action( ‘enqueue_block_editor_assets’, ‘my_theme_scripts’ );

This code will add your custom CSS to the Gutenberg editor.

Assuming you have a basic understanding of CSS and have installed the Gutenberg plugin, let’s get started with adding custom CSS to Gutenberg blocks.

First, open up the Block Editor. In the left sidebar, you should see a section labeled “Settings”. Click on the “Advanced” tab within this section.

Next, find the “Additional CSS” textarea. This is where you will add your custom CSS.

Before we get into the specifics of how to add CSS to Gutenberg blocks, there are a few important things to keep in mind:

– The CSS you add here will only affect the content within the Block Editor. It will not affect the front-end of your website.

– The CSS you add here will only affect the blocks you have selected. For example, if you add CSS to the “Paragraph” block, it will only affect paragraphs on your website.

– You can add CSS to multiple blocks at the same time. Simply click on the block you want to add CSS to and then add your CSS code.

Now that we’ve covered the basics, let’s take a look at how to add CSS to Gutenberg blocks.

Adding Custom CSS to Gutenberg Blocks

Adding custom CSS to Gutenberg blocks is a two-step process. First, you need to add your CSS code to the “Additional CSS” textarea in the Block Editor. Second, you need to select the blocks you want to apply your CSS to.

Let’s say you want to add a custom background color to all of the paragraphs on your website. To do this, you would first add the following CSS code to the “Additional CSS” textarea:

p {
background-color: #ff0000;
}

Next, you would select the “Paragraph” block from the Block Editor. Once you’ve done this, your CSS code will be applied to all paragraphs on your website.

You can also add CSS to multiple blocks at the same time. To do this, simply hold down the “Shift” key and click on the blocks you want to add your CSS to.

Adding Custom CSS to Individual Blocks

In some cases, you may want to add CSS to individual blocks rather than all blocks of a certain type. For example, you may want to add a custom background color to a specific paragraph rather than all paragraphs on your website.

To do this, you would first add a CSS class to the block you want to add your CSS to. In the Block Editor, find the “Advanced” tab and enter your CSS class in the “CSS Class” field.

For this example, we’ll use the CSS class “custom-block”.

Next, you would add your CSS code to the “Additional CSS” textarea. The CSS code you add will need to include the CSS class you just added to your block.

In this example, we would add the following CSS code to the “Additional CSS” textarea:

.custom-block {
background-color: #ff0000;
}

Once you’ve added your CSS code, your custom CSS will be applied to the individual block you added it to.

Adding Custom CSS to a Group of Blocks

In some cases, you may want to add CSS to a group of blocks rather than all blocks of a certain type. For example, you may want to add a custom background color to all blocks within a specific area of your website.

To do this, you would first add a CSS class to the group of blocks you want to add your CSS to. In the Block Editor, find the “Advanced” tab and enter your CSS class in the “CSS Class” field.

For this example, we’ll use the CSS class “custom-group”.

Next, you would add your CSS code to the “Additional CSS” textarea. The CSS code you add will need to include the CSS class you just added to your group of blocks.

In this example, we would add the following CSS code to the “Additional CSS” textarea:

.custom-group {
background-color: #ff0000;
}

Once you’ve added your CSS code, your custom CSS will be applied to the group of blocks you added it to.

Adding Custom CSS to a Specific Block

In some cases, you may want to add CSS to a specific block rather than all blocks of a certain type. For example, you may want to add a custom background color to a specific button on your website.

To do this, you would first add a CSS class to the block you want to add your CSS to. In the Block Editor, find the “Advanced” tab and enter your CSS class in the “CSS Class” field.

For this example, we’ll use the CSS class “custom-button”.

Next, you would add your CSS code to the “Additional CSS” textarea. The CSS code you add will need to include the CSS class you just added to your block.

In this example, we would add the following CSS code to the “Additional CSS” textarea:

.custom-button {
background-color: #ff0000;
}

Once you’ve added your CSS code, your custom CSS will be applied to the individual button you added it to.

Adding Custom CSS to a Block Type

In some cases, you may want to add CSS to all blocks of a certain type. For example, you may want to add a custom background color to all buttons on your website.

To do this, you would first add your CSS code to the “Additional CSS” textarea. The CSS code you add will need to include the block type you want to add your CSS to.

In this example, we would add the following CSS code to the “Additional CSS” textarea:

button {
background-color: #ff0000;
}

Once you’ve added your CSS code, your custom CSS will be applied to all buttons on your website.

Adding Custom CSS to the Block Editor

In some cases, you may want to add CSS to the Block Editor itself. For example, you may want to change the color of the Block Editor toolbar.

To do this, you would first add your CSS code to the “Additional CSS” textarea. The CSS code you add will need to include the block type you want to add your CSS to.

In this example, we would add the following CSS code to the “Additional CSS” textarea:

.block-editor__toolbar {
background-color: #ff0000;
}

Once you’ve added your CSS code, your custom CSS will be applied to the Block Editor toolbar.

Conclusion

Adding custom CSS to Gutenberg blocks is a great way to make your content stand out. With a little bit of CSS code, you can change the look and feel of your website without having to edit your theme files.