How to Modify the Default CSS Class of Your Custom Gutenberg Block
Content
When you create a custom Gutenberg block using the official create-block tool, WordPress automatically wraps your block's content in a default CSS class. This class follows the pattern .wp-block-create-block-{your-block-name}. While this is useful for basic styling, many developers want to customize this class for better semantics, consistency with their theme, or to follow a specific naming convention.
Why Change the Default Class?
The automatically generated class name is functional but not always ideal. You might want to change it to:
- Match your theme's existing class naming structure (e.g., BEM methodology).
- Create a shorter, more readable class name.
- Ensure consistency across multiple custom blocks.
The Solution: Using the useBlockProps Hook
You can easily define a custom wrapper with your own class names by modifying the save function in your block's JavaScript file. The key is to use the useBlockProps.save() function provided by the @wordpress/block-editor package.
Here is a code snippet demonstrating how to implement this:
// Import the necessary hook
import { useBlockProps } from '@wordpress/block-editor';
// Inside your save function
export default function save() {
// Use useBlockProps.save() to define a custom className
return (
<div {...useBlockProps.save({ className: 'my-custom-class' })}>
<p>Your block content goes here...</p>
</div>
);
}
How It Works
- useBlockProps.save(): This React hook is essential for handling the block's wrapper props during the save phase. It generates the necessary data attributes and classes WordPress needs to manage the block.
- className property: By passing an object with a
classNameproperty to the hook, you can append your own custom class to the wrapper element. WordPress will still add its own classes for identification, but your custom class will be included.
Important Considerations
- Preservation of Core Classes: This method adds your custom class alongside WordPress's core classes. It does not remove the default classes, which are required for the block editor to function correctly.
- Block Validation: Changing the output of the
savefunction can sometimes cause block validation errors if an existing block on a page was saved with the previous markup. Be mindful of this when updating blocks on a live site. - Additional Props: The
useBlockPropshook handles more than just classes; it also manages other attributes like alignment and typography settings defined in yourblock.json. Using this hook ensures you don't lose that functionality.
By following this approach, you gain full control over the HTML output of your custom Gutenberg blocks, allowing for cleaner code and more maintainable styling.
Related Support Threads Support
-
Modifying the output of WordPress / Gutenberghttps://wordpress.org/support/topic/modifying-the-output-of-word-press/
-
Cover Block Text isn’t Read By Google?https://wordpress.org/support/topic/cover-block-text-isnt-read-by-google/
-
How to write tests for Gutenberg via mocking useSelect or via a dummy post?https://wordpress.org/support/topic/how-to-write-tests-for-gutenberg-via-mocking-useselect-or-via-a-dummy-post/
-
is WORD PRESS automatically GUTENBERG editor now?https://wordpress.org/support/topic/is-word-press-automatically-gutenberg-editor-now/
-
Watching for changes in Gutenberg blocks?https://wordpress.org/support/topic/watching-for-changes-in-gutenberg-blocks/
-
How to expand the functionality of the default Gutenberg blocks?https://wordpress.org/support/topic/how-to-expand-the-functionality-of-the-default-gutenberg-blocks/
-
Can I use the Rest API to keep the Gutenberg intact when publishing a post?https://wordpress.org/support/topic/can-i-use-the-rest-api-to-keep-the-gutenberg-intact-when-publishing-a-post/
-
Modify Gutenberg link dialog suggestionshttps://wordpress.org/support/topic/modify-gutenberg-link-dialog-suggestions/
-
Gutenberg: Checking current template type?https://wordpress.org/support/topic/gutenberg-checking-current-template-type/
-
Implement Gutenberg publish confirmation messagehttps://wordpress.org/support/topic/implement-gutenberg-publish-confirmation-message/
-
WordPress Block Editor (Gutenberg) Issues: Custom Code Not Displaying Correctlyhttps://wordpress.org/support/topic/wordpress-block-editor-gutenberg-issues-custom-code-not-displaying-correctly/
-
Gutenberg blocks in theme files?https://wordpress.org/support/topic/gutenberg-blocks-in-theme-files/
-
Basics of making a Gutenberg block?https://wordpress.org/support/topic/basics-of-making-a-gutenberg-block/
-
Gutenberg: Custom React Components: Re-initializing?https://wordpress.org/support/topic/gutenberg-custom-react-components-re-initializing/
-
How to hook into the save function of a Gutenberg blockhttps://wordpress.org/support/topic/how-to-hook-into-the-save-function-of-a-gutenberg-block/
-
Gutenberg: Having CSS/JS apply to blocks when editing?https://wordpress.org/support/topic/gutenberg-having-css-js-apply-to-blocks-when-editing/
-
Gutenberg Block: Accessing meta field for posts?https://wordpress.org/support/topic/gutenberg-block-accessing-meta-field-for-posts/
-
Remove background image from Gutenberg editor on block themehttps://wordpress.org/support/topic/remove-background-image-from-gutenberg-editor-on-block-theme/
-
Impreza: How to upgrade WPBakery to a working page builderhttps://wordpress.org/support/topic/impreza-how-to-upgrade-wpbakery-to-a-working-page-builder/
-
Negative Margin Column Block Workaroundhttps://wordpress.org/support/topic/negative-margin-column-block-workaround/
-
Gutenberg: Modifying built-in block classes?https://wordpress.org/support/topic/gutenberg-modifying-built-in-block-classes/
-
Getting theme’s current font sizes for use in Gutenberg block?https://wordpress.org/support/topic/getting-themes-current-font-sizes-for-use-in-gutenberg-block/
-
How to select Guttenberg “code block” for CSS customisationhttps://wordpress.org/support/topic/how-to-select-guttenberg-code-block-for-css-customisation/
-
Optimal Icon Usage in Gutenberghttps://wordpress.org/support/topic/optimal-icon-usage-in-gutenberg/
-
Gutenberg Block: Media Uploader?https://wordpress.org/support/topic/gutenberg-block-media-uploader/
-
How to add Gutenberg Inspector controls to Posts (not individual blocks)https://wordpress.org/support/topic/how-to-add-gutenberg-inspector-controls-to-posts-not-individual-blocks/
-
Gutenberg Block – disable editing for client / block permissionshttps://wordpress.org/support/topic/gutenberg-block-disable-editing-for-client/
-
How to use 3rd party React Component Library for crafting new Gutenberg Blockshttps://wordpress.org/support/topic/how-to-use-3rd-party-react-component-library-for-crafting-new-gutenberg-blocks/
-
Limiting types of Gutenberg blocks for certain CPTs?https://wordpress.org/support/topic/limiting-types-of-gutenberg-blocks-for-certain-cpts/
-
Post title block in Gutenberghttps://wordpress.org/support/topic/post-title-in-gtenberg/
-
Gutenberg: get context of parent post for synced patternhttps://wordpress.org/support/topic/gutenberg-get-context-of-parent-post-for-synced-pattern/
-
Limiting number of Blocks by type?https://wordpress.org/support/topic/limiting-number-of-blocks-by-type/
-
Best way to have controlled content blockshttps://wordpress.org/support/topic/best-way-to-have-controlled-content-blocks/
-
Gutenberg blocks in theme or separate plugin?https://wordpress.org/support/topic/gutenberg-blocks-in-theme-or-separate-plugin/
-
import Gutenberg html into block pluginhttps://wordpress.org/support/topic/import-gutenberg-html-into-block-plugin/
-
Lazy Loading Gutenberg block content?https://wordpress.org/support/topic/lazy-loading-gutenberg-block-content/
-
Limiting available Gutenberg Blocks by post type?https://wordpress.org/support/topic/limiting-available-gutenberg-blocks-by-post-type/