You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Custom block stylesheet for the core/navigation-submenu block is not enqueued #66786
It's not possible to use a custom block stylesheet for the core/navigation-submenu block. The styles are not loaded on the front-end of the website. Instead, I have to put the submenu styles in the custom block stylesheet for the core/navigation block (for which the block stylesheet does works).
Step-by-step reproduction instructions
Using a block theme, in the function.php file, add the code snippet below.
Create a file named core-navigation-submenu.css in /assets/css/blocks/ (example) and add some styles like the example below.
Screenshots, screen recording, code snippet
In functions.php:
functionthemeslug_block_stylesheets() {
// Add the block name (with namespace) for each style$blocks = array(
// 'core/navigation','core/navigation-submenu',
);
// Loop through each block and enqueue its stylesforeach ( $blocksas$block ) {
// Replace slash with hyphen for filename$slug = str_replace( '/', '-', $block );
wp_enqueue_block_style( $block, array(
'handle' => "themeslug-block-{$slug}",
'src' => get_theme_file_uri( "assets/css/blocks/{$slug}.css" ),
'path' => get_theme_file_path( "assets/css/blocks/{$slug}.css" )
) );
}
}
add_action( 'init', 'themeslug_block_stylesheets' );
changed the title [-]Custom block stylesheet for the navigation-submenu block is not recognized[/-][+]Custom block stylesheet for the `core/navigation-submenu` block is not enqueued[/+]on Nov 6, 2024
While reviewing the core functionality of enqueuing custom block stylesheets, I noticed that the stylesheets for core/navigation-submenu are successfully enqueued on the frontend. Here's the same code and CSS used for reference.
functionthemeslug_block_stylesheets() {
// Add the block name (with namespace) for each style$blocks = array(
// 'core/navigation','core/navigation-submenu',
);
// Loop through each block and enqueue its stylesforeach ( $blocksas$block ) {
// Replace slash with hyphen for filename$slug = str_replace( '/', '-', $block );
wp_enqueue_block_style( $block, array(
'handle' => "test-block-core-navigation-submenu",
'src' => get_theme_file_uri( "assets/css/blocks/core-navigation-submenu.css" ),
'path' => get_theme_file_path( "assets/css/blocks/core-navigation-submenu.css" ),
) );
}
}
add_action( 'init', 'themeslug_block_stylesheets' );
Upon inspecting the page, the styles are present in the DOM after being enqueued for core/navigation-submenu. However, the changes are not visible on the frontend, as shown below.
To apply the changes, I used !important, as demonstrated in the code below. In the preview we can see that the styles are now applied correctly.
The issue arises due to CSS specificity and how WordPress loads styles:
When Enqueuing for core/navigation:
-The styles are loaded at the navigation block level.
-They naturally cascade to submenu elements, maintaining specificity through the parent-child relationship.
When Enqueuing for core/navigation-submenu:
WordPress core loads its default submenu styles with a certain specificity.
Custom styles must compete with these default styles, which often have equal or greater specificity.
The cascade from the parent navigation block is lost, making custom styles less effective.
Behavior Breakdown
Core Navigation Enqueuing:
wp_enqueue_block_style( 'core/navigation', ... );
Styles apply at the navigation block level.
They cascade naturally to submenu elements.
Specificity is maintained through the parent-child hierarchy.
Activity
[-]Custom block stylesheet for the navigation-submenu block is not recognized[/-][+]Custom block stylesheet for the `core/navigation-submenu` block is not enqueued[/+]patil-vipul commentedon Dec 27, 2024
While reviewing the core functionality of enqueuing custom block stylesheets, I noticed that the stylesheets for core/navigation-submenu are successfully enqueued on the frontend. Here's the same code and CSS used for reference.
Upon inspecting the page, the styles are present in the DOM after being enqueued for
core/navigation-submenu. However, the changes are not visible on the frontend, as shown below.To apply the changes, I used !important, as demonstrated in the code below. In the preview we can see that the styles are now applied correctly.
Reason for the Issue
The issue arises due to CSS specificity and how WordPress loads styles:
When Enqueuing for
core/navigation:-The styles are loaded at the navigation block level.
-They naturally cascade to submenu elements, maintaining specificity through the parent-child relationship.
When Enqueuing for
core/navigation-submenu:Behavior Breakdown
Core Navigation Enqueuing:
Submenu Enqueuing:
In case where not to rely on
!important, we can increase the selector specificity of your custom styles.cc: @germanfrelo
patil-vipul commentedon Dec 27, 2024
@germanfrelo Let me know your thoughts on the above. As I think we should continue with the default behaviour here.
germanfrelo commentedon Jan 9, 2025
Sorry for the delay. I will test it again in a few days.
germanfrelo commentedon Feb 8, 2025
@patil-vipul I confirm that it works. I don't know why it didn't work for me before.
I'm closing this issue.