-
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
New WP 6.3 CSS is overriding theme styles and styles from other plugins #53468
Comments
Hey there! Thanks for opening this issue and for using the latest in WordPress. Can you provide more information around how you can replicate this? For example, what plugins and themes are you using? Without replication steps, this makes it very difficult for us to address your feedback, either with more context or a fix. |
Well, there are many plugins allowing you to add some margin for blocks, because the default Gutenberg one is not very useful as you never want to have the same spacings on desktop and on mobile. But let's say we will not use any plugin, but ofc I need a bigger margin on the desktop and smaller on the mobile, so I can not use either Gutenberg controls, but I need to manually add class to the first block to have some top margin and also one class for the last block to have some bottom margin. I'm maintaining hundreds of websites, but thanks god only a few tens of them are based on theme.json I can You should never add that crazy specific CSS rules like And there are more problems in theme.json themes... Broken shortcodes from version 6+ (https://demo.kubiq.sk/sample-page/) also taken hours of my life... I really like Gutenberg and the FSE but I don't understand some decisions in the development and if this will keep happening, then I will be forced to use some other builder for development and that would be a pitty, because I already have so many custom blocks and patterns and also plugins for block editor. Thank you for your time. |
For the sites with theme.json, you can achieve responsive spacing by making sure that the spacing presets are responsive. |
@carolinan, sure in the ideal world where you will have perfectly designed everything and all the spaces will be consistent you can rely on this, but unfortunately that's mostly not the real world scenario. But the point is, that many people use 3rd party plugins to create more advanced grids, offsets, etc... or just custom classes to write their own CSS rules and updates like this in WP 6.3 will break many things. You have a margin controls in the editor, so just let the user to decide if he wants to set it for 0 or not... why to write that highly specific default CSS rule that will override existing rules on millions of existing websites? |
I imagine a selector like |
Also related to the new selector style: |
missing dashboard, missing pemerlink in the list |
There are so many css rules that have a selector's depth way too aggressive, like :
And there are some other cases. Maybe the ideal solution would be to move the editor inside an iframe and be able to unload most of the styles. It's supposed to be simple but some of these styles are breaking things inside the editor and thus we can't have the same front-end & back-end styles for both. You cannot have these loose selectors where you're targetting tags or generic classes. |
|
Same problem, different code in WordPress 6.6. Also, *why" is
Ref. #40159. |
I believe the Regarding the |
Thanks for clarifying that. This seems to be an enforcement which I believe shouldn't be imposed. If there is nothing defined in the settings or CSS, would it not be better to allow the default browser behaviour to be respected? As it is now, the user's browser setting (default or accessibility-based) is overridden and this requires another override to set things back to the default behaviour. New ref. #63647 |
I don't have the full history and context around this default but it was introduced back in 2022 via #41786. As it has been around for a long while now, I'm not sure it can be easily removed completely, as some themes and sites might actually rely on it. If your theme is using theme.json it can set this to A nuclear option (which I wouldn't really recommend) might be to dequeue all global styles if your theme isn't using them:
|
Well, I love the idea of the Gutenberg / Block Editor, but i need to do some crazy things to prevent it breaking my websites after each update... so if anyone wants to try it, here is my "cleaning" code ( only for new projects ! ): add_action( 'after_setup_theme', function(){
// remove gutenberg duotone
remove_action( 'wp_body_open', 'wp_global_styles_render_svg_filters' );
remove_action( 'in_admin_header', 'wp_global_styles_render_svg_filters' );
remove_filter( 'render_block', 'wp_render_duotone_support' );
});
add_action( 'wp_head', function(){
// remove all default styles
global $wp_styles;
wp_dequeue_style('global-styles');
wp_dequeue_style('core-block-supports');
foreach( $wp_styles->queue as $handle ){
if( strpos( $handle, 'wp-block-' ) === 0 ){
wp_dequeue_style( $handle );
}
}
}, 2 );
add_action( 'enqueue_block_editor_assets', function(){
// remove crazy selectors rules eg. :first-child:first-child or :root :where
wp_enqueue_script( 'gutenberg-fixes', get_stylesheet_directory_uri() . '/gutenberg-fixes.js', [], 1, 1 );
wp_add_inline_script( 'gutenberg-fixes', 'const gutenbergFixedCss="' . addslashes( wp_get_CLEAN_global_stylesheet() ) . '"', 'before' );
});
add_action( 'wp_enqueue_scripts', function(){
$global_stylesheet = wp_get_CLEAN_global_stylesheet();
if( ! empty( $global_stylesheet ) ){
wp_register_style( 'clean-global-styles', false );
wp_add_inline_style( 'clean-global-styles', $global_stylesheet );
wp_enqueue_style( 'clean-global-styles' );
}
});
function wp_get_CLEAN_global_stylesheet(){
$can_use_cached = ! wp_is_development_mode('theme');
$cache_group = 'theme_json';
$cache_key = 'wp_get_CLEAN_global_stylesheet';
if( $can_use_cached ){
$cached = wp_cache_get( $cache_key, $cache_group );
if( $cached ){
return $cached;
}
}
$tree = WP_Theme_JSON_Resolver::get_merged_data();
$styles_variables = $tree->get_stylesheet( [ 'variables' ], [ 'theme', 'custom' ] );
$styles_rest = $tree->get_stylesheet( [ 'presets' ], [ 'theme', 'custom' ] );
$stylesheet = $styles_variables . $styles_rest;
if( $can_use_cached ){
wp_cache_set( $cache_key, $stylesheet, $cache_group );
}
return $stylesheet;
} gutenberg-fixes.js // Block editor is loading default layout styles causing weird margins and other problems
wp.hooks.addFilter( 'blocks.getBlockDefaultClassName', 'theme/gutenber-fix', function( string, className, blockName ){
let styles;
const bad_selectors = [ ':first-child:first-child', ':root :where', '.is-layout-constrained' ];
const iframe = document.querySelector('iframe[name="editor-canvas"]');
const currentEditor = iframe ? iframe.src : 'div';
if( ! document.editorFixed || document.prevEditor != currentEditor ){
if( iframe ){
styles = iframe.contentDocument.querySelectorAll('style');
}else{
styles = document.querySelectorAll('style');
}
for( let i = 0; i < styles.length; i++ ){
if( bad_selectors.some( selector => styles[ i ].innerText.includes( selector ) ) ){
styles[ i ].innerHTML = gutenbergFixedCss + '.acf-innerblocks-container{width:100%}.editor-visual-editor [data-block] a{pointer-events:none}';
document.editorFixed = true;
document.prevEditor = currentEditor;
console.log('Bad selector style removed');
break;
}
}
}
return string;
});
// remove empty block on Backspace
document.body.addEventListener( 'keydown', e => {
if( e.key == 'Backspace' ){
const selectionStart = wp.data.select('core/block-editor').getSelectionStart();
if( 'offset' in selectionStart && selectionStart.offset === 0 ){
const selectedBlock = wp.data.select('core/block-editor').getSelectedBlock();
if( selectedBlock.attributes.content.length === 0 ){
wp.data.dispatch('core/block-editor').removeBlock( selectedBlock.clientId );
}
}
}
}, true ); and then I have my own styles for blocks ofc... so then it is loading only what I need and I can easily get 100% on pagespeed and I don't need to worry about some weird changes after each update... I know... it's crazy... but it works for my workflow... |
What is the point of WordPress calling these overly aggressive CSS rules in the website every time. We manage WordPress websites for over 300 clients and surely you can't suddenly use CSS selectors like |
I agree that it's problematic to introduce rules like this; we currently have nearly 100 sites which now have to be repaired. (Automatic updates are on, based on Core team advice, so I now have to drop everything and fix this issue across dozens of sites.) |
You can also add this to your add_action( 'wp_head', function(){
global $wp_styles;
if( ! empty( $wp_styles->registered['global-styles']->extra['after'][0] ) ){
$wp_styles->registered['global-styles']->extra['after'][0] = str_replace( ':root :where', ':where', $wp_styles->registered['global-styles']->extra['after'][0] );
}
}, 1 ); |
What about keeping the block styles, but not the specific ones with For example, if I use buttons, or columns, or a cover on my page, I only want those styles to load. Not the global ones. Is that possible? Will the below do the job?
|
@kubiqsk It'd be great if you could report any styles you see breaking. You shouldn't need to implement an action like that since the intention in 6.6 is to reduce core's css specificity. |
@talldan And yet, the specificity is now higher, breaking thousands of websites... I will implement the same code today for my websites, and I'll report my findings here. |
Do let us know which particular rules if you can find them, it's really helpful. For example, the bug you reported in #63345 was an unintentional increase in specificity, it was an important one to fix, and it's one of the reasons a 6.6.1 release for WordPress will be rolled out in the next few days. |
So, those dequeue filters remove everything, and I still need my block styles. The filter below removes all highly -specific
|
|
@talldan This is the first iteration, I might refine the strings to be replaced. But right now I'll keep it as it solves so many issues ;) |
Just to keep you all up to date, another issue was reported that we're looking to include a fix for in 6.6.1 - #63712. Hopefully that's it, but if there's anything else you spot that seems higher specificity than 6.5, keep the issues coming in and we'll do our best to fix them. |
Maybe this is known, but if not.... In 6.6, in hybrid themes, within the editor, margins specified in theme.json at the block level (paragraph, for example), or in an editor stylesheet, are overridden by this new css output:
This is applying the blockGap spacing (specified in theme.json) to everything at the root level or within a most containers – including non-flexbox groups (not a row or stack) or columns where typography styles should take precedence. This also happens with a constrained layout element, such as a group.
For example, this is Ignored in the editor:
This is very frustrating. If this specificity could be reverted to how it was previously that would be ideal. At the root level, or within non flex groups or columns (etc), I'd expect typography styles (paragraph margins, heading margins, list margins, etc) to take precedence. As they do within the media & text block, for example. And as they did previously. On the frontend, the theme.json styles are being honored properly (but not in the editor):
|
Thanks for reporting. I can reproduce it. For anyone else looking to reproduce this, it seems to only be an issue when the editor isn't iframed, which can happen when custom fields is active, or there are older blocks installed. There might also be some other cases that trigger it. I expect there's extra specificity being added to the selector to override some other styles that bleed into the editor canvas, which wouldn't normally when the iframe is being used. |
Is there a way to force the block editor to use the iframe when developing hybrid themes? |
@apmeyer Yes: ensure that all blocks are compatible with theme.json schema version 3. |
Is this an editor-only issue? 6.5
6.6.1
|
@munyagu Are you seeing any problems on the front end? I believe that one's an editor only issue when the editor is non-iframed. It happens because of the way the In all other use cases though (iframed editor, front end) that extra class name isn't added, so the specificity is the same as before. |
Just a heads up by the way, I've created a tracking issue for the specificity issue reported - #64243. |
@talldan If this is an editor-only issue, it seems that the problem I and the ticket are having is not yet included in the tracking issue. |
I am using the same hybrid theme as the ticket reporter. |
As these changes are being committed into WordPress Core, each contributor will get a credit (prop). Getting WP contributor credit requires a personal (not company) WordPress.org profile / username. Do you have a personal (not company) WordPress.org profile and username?
Thank you for your contributions! |
@hellofromtonya I am at https://profiles.wordpress.org/apmeyer/ Thanks. |
Thank you @apmeyer. I manually added you to the Core Props. |
Thank you @munyagu. I manually added you to the Core Props. |
Description
There are some new CSS rules and they break layouts, eg.:
or similar one with duplicated
:first-child
.When I remove this duplicated selector with inspect element and leave only 1 occurrence, then everything works again
Step-by-step reproduction instructions
It's hard to write step-by-step for this, as you will need to use some specific themes and plugins and setup some blocks, but it's happening on more websites with a different setup, but the point is, that this is a newly added CSS, so it was working fine until now.
Screenshots, screen recording, code snippet
No response
Environment info
No response
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
No
The text was updated successfully, but these errors were encountered: