Skip to content
pinegrow.com
Docs menuWordPress

Custom block styling and scripts

How to add custom styling to our blocks?

On this page

Sometimes our custom blocks need special CSS styling that is not covered by the framework styling or by the global theme stylesheet.

The best approach is to create a special CSS stylesheet that contains the block styling and to instruct WordPress to load it only if the block is displayed on the current page.

Create a new CSS file

To do that, create a new CSS file with all the required CSS styling. First, we create the folder blocks that will contain our block assets. The name doesn’t have to be blocks, of course.

Pinegrow project panel context menu with Add new folder highlighted in the CreateYourFirstBlock project.

Next, we create a new file in the Project panel by right-clicking on the blocks folder:

Pinegrow project panel with a menu on the blocks folder and Create new file highlighted.

We will name the file fancy.css:

Create new file dialog with fancy.css entered as the name of the new file.

Then, drag fancy.css to the page.

Project panel with fancy.css being dragged into the blocks folder, with a tooltip about dropping inside collapsed folders.

Let’s go back to our About me block and add the class fancy-block to its main <section> element.

Pinegrow context menu on the About Me section with Classes highlighted.

The right-click context menu or the CMD + L open up the Assign class dialog:

Add classes dialog for section.pb-5 with fancy-block typed in, showing fancy-block assigned from fancy.css.

CSS rules use the class to scope the styling to the block. Here we will add a short decorative line below <h3>:

.fancy-block h3:after { content: ‘ ‘; width: 60px; height: 0px; border-top: 4px solid; display: block; margin-top: 10px; }

Click on fancy.css in the Project panel to open the file in the code editor:

Project panel with fancy.css open in the code editor, showing a .fancy-block h3:after rule that draws a decorative line under the heading.

We then include the CSS file with the Style setting on the Block action. The value is the relative url of the stylesheet, in relation to the project folder. In our case, blocks/fancy.css.

Block settings for aboutme with Style set to blocks/fancy.css, and a callout to include the stylesheet in the block editor and front end.

The stylesheet will be included both in the editor and on the front-end.

WordPress page I Love Surfing Huge Waves showing a purple decorative line under the heading, from the fancy block styles.

Including a stylesheet only in the editor

Use Editor style setting to add a stylesheet that should be included only in the editor.

Both Style setting and Editor style setting can be defined.

Sharing the same CSS stylesheets between more blocks

A group of blocks can share the same stylesheet. Just add it to each block with the Style setting on their Block actions.

Pinegrow generates resource handles based on the stylesheet name, so that the stylesheet will be included only once, even if more blocks are using it.

Sharing the same Javascript files between multiple blocks

A group of blocks can share the same Javascript file. Just add it to each block with the Script setting in their Block actions.

Pinegrow generates resource handles based on the Javascript file name, so that the file will be included only once, even if multiple blocks are using it.

Including the framework stylesheet in the Block editor

Our blocks usually rely on styling provided by the global theme stylesheet or framework stylesheet such as Bootstrap and Tailwind CSS. Without these styles block don’t show up correctly in the block editor. Use Theme settings to include theme stylesheets in the block editor.

Loading only resources of blocks that are used on the page

By default, WordPress loads styles and scripts of all registered blocks on the front-end, regardless if blocks are used on the page or not.

To load only assets of blocks that are used on the page, add the following code to inc/custom.php:

add_filter( 'should_load_separate_core_block_assets', '__return_true' );

Starting from Pinegrow Web Editor 7.8, WordPress plugin 1.0.15 and Theme Converter 2.2 the WordPress builder can do this for you if you enable the Load only styles and scripts of blocks that are displayed on the front-end checkbox in project settings:

WordPress Theme Settings Blocks section set to Hybrid, with Load only styles and scripts of blocks displayed on the front end checked.

Summary

If a JS script or a CSS file needs to be loaded at the template level, it will be loaded every time the template is used, unless you use conditionals. See our documentation here.

If a script or CSS needs to be loaded at the block level, when two blocks use the exact same JS or CSS, it will only be loaded once.

On top of that, from the project settings, you can setup your blocks so only styles and scripts that are displayed on the frontend are loaded.

Next steps

This completes the Create Your First Block guide.

Explore the rest of Blocks documentation and tutorials to learn more.