-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option to disable default block styles #32051
Comments
I listened to the Gutenberg Changelog podcast and read these github issues: WordPress/wordpress-develop#1236 and #25220 (comment). As a theme author I'm trying to get my head around this. Should I override the core block css by adding my own theme css for specific blocks? Like this: |
As it happens I am currently writing a post for these changes, and it will be published in a few days in the make.w.org blog. Below is a neat function you can use in your theme to add styles for blocks. It works both for WP 5.8 as well as previous versions, and adds your block-styles both in the frontend and the editor. /**
* Attach extra styles to multiple blocks.
*/
function my_theme_enqueue_block_styles() {
// An array of blocks.
$styled_blocks = [ 'paragraph', 'code', 'cover', 'group', 'gallery' ];
foreach ( $styled_blocks as $block_name ) {
// Get the stylesheet handle. This is backwards-compatible and checks the
// availability of the `wp_should_load_separate_core_block_assets` function,
// and whether we want to load separate styles per-block or not.
$handle = (
function_exists( 'wp_should_load_separate_core_block_assets' ) &&
wp_should_load_separate_core_block_assets()
) ? "wp-block-$block_name" : 'wp-block-library';
// Get the styles.
$styles = file_get_contents( get_theme_file_path( "styles/blocks/$block_name.min.css" ) );
// Add frontend styles.
wp_add_inline_style( $handle, $styles );
// Add editor styles.
add_editor_style( "styles/blocks/$block_name.min.css" );
if ( file_exists( get_theme_file_path( "styles/blocks/$block_name-editor.min.css" ) ) ) {
add_editor_style( "styles/blocks/$block_name-editor.min.css" );
}
}
}
// Add frontend styles.
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_block_styles' );
// Add editor styles.
add_action( 'admin_init', 'my_theme_enqueue_block_styles' ); If you want to remove default styles for a block, you could just de-register its styles using the |
Great, thank you! I will try it out. If it works for me I'll close this issue and add a reference to your future blog post. |
@aristath @erikjoling has the post mentioned above been published on make.w.org? I've looked but can't find it. |
No, it's not published. Posts like that are usually published when WordPress reaches beta. |
I'm super curious on a write up on all the neat changes that @aristath has helped introduce here in #32051, and #31239, and even #32275. These all seem to have a great impact on how styling can be applied by theme authors. @aristath is there a post looming please? Also, huge thanks for all your work! 👏 |
Not yet... Not everything made it in 5.8 so I'm compiling a list of all the improvements that did make it. For example, splitting |
Thanks for sharing. If I opt-in loading only the styles for rendered blocks in a page; would it be possible to disable the core style (for example for the gallery block) and load my own gallery style? |
Yes. wp_deregister_style( 'wp-block-gallery' );
wp_register_style( 'wp-block-gallery', $src, ... ); |
Thank you for you work and providing extra information Ari. I will close this issue. |
Hello, i found this thread because i try to remove default styles for some blocks. I was try yours code but it didn't work for me. Still it load default gallery css in /wp-includes/css/dist/block-library/style.min.css
|
@szykuc I think you also must use this filter:
|
Thank you, that was a point. |
Is it possible remove styles for specific block also in editor? |
What problem does this address?
The CSS of blocks gets inserted inline (I'm using gutenberg 10.6). But this also adds default styles for some blocks. In the case of galleries and columns it's complex to overrule them. And redundant I would say, because one of the goals of the new style approach is to reduce bytes.
https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/gallery/style.scss
https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/columns/style.scss
I want to disable the default block styles and let my theme handle the styles.
What is your proposed solution?
A setting in theme.json would do. Or something with a filter or theme-support.
Or is it already possible to disable the default styles?
Extra info
This is what the documentation has to say about it:
https://developer.wordpress.org/block-editor/how-to-guides/themes/theme-support/#default-block-styles
The text was updated successfully, but these errors were encountered: