gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Global link styles are overriding individual block styles that contain links

Open ndiego opened this issue 2 years ago • 6 comments

Description

Link styles set in Global Styles override the local block styles that contain links. Good examples are the Site Title and Post Title (Title) blocks. See the video for more details.

cc @tellthemachines and @aaronrobertshaw as I know you both have been doing specificity work.

Step-by-step reproduction instructions

  1. Open a Playground instance and navigate to the Site Editor
  2. Change the size of the Site Title block in the header to XXL
  3. Then go into Global Styles and modify the size of links to something other than XXL.
  4. Notice that the Site Title is now that size and there is no way to change it. The global link style overrides the styles applied at the block level.

Screenshots, screen recording, code snippet

https://github.com/WordPress/gutenberg/assets/4832319/517e4973-30b9-4cc3-8a13-fe24f4a85491

Environment info

  • WordPress 6.5.3
  • Gutenberg 18.3.0

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

ndiego avatar May 14 '24 13:05 ndiego

Thanks for the ping @ndiego 👍

I can replicate this issue. It appears to be less about specificity and more about the structure of the block and its block supports.

The block support styles and classes are applied to the block's wrapper e.g. has-font-size-*. When the Site Title is made a link its contents are wrapped in an <a> tag.

Global link styles by their nature target any <a> element and so match the inner <a> wrapping the site title block's inner text. In the absence of any other style directly targeting that link, the global style takes precedence over any inherited style.

One option might be to add some additional styles to Site Title's style.scss file. Ones that force the inheritance of the block support styles if present. This can get a little messy and bloated though because the block probably needs styles to ensure the inheritance of all the typography.

aaronrobertshaw avatar May 15 '24 08:05 aaronrobertshaw

I'm able to reproduce Exactly what is described in the OP when using 6.6.1. But that's the global styles overriding what's defined & saved from the editor.

In the theme.json file While using WP 6.6.1 + Gutenberg 18.8.0 (only) + TT4. If I define styles.elements.link.typography.fontSize to "1rem" And styles.blocks.core/navigation.typography.fontSize to "5rem"

The navigation looks like: image

But it's essentially the same I guess.

As @aaronrobertshaw suggested, we need to apply the styles to the inner link. That is: styles.blocks.core/navigation.elements.link.typography.fontSize to ie. "2rem" And we should match styles.blocks.core/navigation.typography.fontSize with the same value. Or remove it, and leave only the one for the inner link (the most specific). Or there could be a discrepancy between the two.

image

It feels logical to me. I mean, If the block wraps it with an a element. Makes sense to apply it in theme.json to the elements.link inside the block style.

I also feel it can be redundant. Because we can find ourselves adding the fontSize property both for the linked and not linked case. And most of the time, I guess it'll be the same size.

Global link styles by their nature target any element and so match the inner wrapping the site title block's inner text. In the absence of any other style directly targeting that link, the global style takes precedence over any inherited style.

But then, even styles.elements.link.typography.fontSize is conditioning any other fontSize property if the block has a link. Even when those blocks define a fontSize for the block. But has not defined the font size for the link of that block specifically.

Example: If syles.elements.link.typography.fontSize is set to "1rem" and styles.blocks.core/navigation.typography.fontSize to "5rem" the 1rem would still take precedence, if not using a more specific rule for those links. That is, unless you set styles.blocks.core/navigation.elements.link.typography.fontSize

All of a sudden, I'm having to go block by block on my entire theme to modify the styles to be like that in order to look as I expect. Or else, my theme feels broken.

But then, even styles.elements.link.typography.fontSize is conditioning any other fontSize property if the block has a link. Even when those blocks define a fontSize for the block. But has not defined the font size for the link of that block specifically.

Admittedly, I found this a bit hard to follow but if I understand correctly, you are describing the general goal of styling elements (including links) globally.

The whole idea is that you could define a generic style that should be applied everywhere. In specific instances where you don't want that global element style to apply it will need to be overridden.

I believe the current behaviour follows CSS inheritance i.e. styles are only inherited when there isn't something directly targeting the element. Here's an quick codepen illustrating a simple case.

When inline styles or classes are applied to a parent block, they could be hooked onto to force inheritance. If the styles are simple CSS via either global styles or an external stylesheet, I don't think it is that easy.

aaronrobertshaw avatar Jul 25 '24 02:07 aaronrobertshaw

Admittedly, I found this a bit hard to follow but if I understand correctly, you are describing the general goal of styling elements (including links) globally.

Yes. You're correct. I've basically described your CSS codepen. With the rules of theme.json. 😅 So. If you define an abstract style for the links ( that applies everywhere ), that was the styles.elements.link.typography.fontSize part. It'll be conditioning you forever. Unless you override it more specifically.

Is that bad ? No. I feel it's logical. And technically correct. I know I may have made it sound like I was against it. But no. I think that the theme developer has a responsibility here on his thought process. To previously plan their styles correctly.

I guess I was trying to see or understand if there was a reason why the global link styles could be overriding custom block styles in the editor for what it's done in the theme.json itself. But that's not the case.

The link global styles are overriding custom block styles defined in the editor. And I don't know why. I'm sorry. I now see it's far beyond my knowledge & I only scratch the surface I guess.

emdashftw avatar Jul 25 '24 07:07 emdashftw

It's a tricky area with lots of angles 🤯

FWIW I think the root of the recent discussion is captured in this comment:

The link global styles are overriding custom block styles defined in the editor

In this scenario, "overriding" isn't accurate because the custom block styles don't define any link styles. If they did, they would override the global link styles. It might be easier to think of the global link styles as "preventing inheritance" here.

When global link styles are being set, a choice is made that all links should be styled that way unless told otherwise by more specific styles. This aligns with your other point:

I think that the theme developer has a responsibility here on his thought process. To previously plan their styles correctly.

I agree but the ideal scenario is for this process to be as easy, straightforward, and free of gotchas as possible.

Depending on the design, there could well be a tipping point where for a theme it is easier to not define a global link style and only define them per block type e.g. all links within paragraphs get X styles.

aaronrobertshaw avatar Jul 25 '24 07:07 aaronrobertshaw

Following the discussion above, closing this as working as intended: global elements.link styles apply wherever a block hasn't defined its own link styles, and block-level link styles take precedence when they are defined — as @aaronrobertshaw walked through above (and the reporter confirmed).

youknowriad avatar Aug 28 '26 14:08 youknowriad