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 editor styles on element selectors effect block control styles. #10178
Expected behavior
The styles above are exaggerated but I don't think styles on button or input elements are totally edge cases. #10067 (comment)
Possible solution
One fix that would need more testing is to unset styles on the Gutenberg specific class before styles are added.
So, for example, adding the following to the top of components/style.css fixed the button styles for me:
.components-button {
all: unset;
}
This could be done for each class that Gutenberg applies to an html element other than div. Mostly button and input I would guess.
Desktop (please complete the following information):
OS: Ubuntu
Browser: chome, firefox
Version: latest
Additional context
Gutenberg v3.9
Reacted by Erik Joling, Justin Tadlock, David Romsey and Keven L.
Maybe we cut it down to a reset of only the most commonly used properties of input, select, textarea and button.
By adding the following to the top of packages/block-library/src/style.scss, the editor becomes much more resistant to user styles causing deformed or broken controls.
This is indeed a good idea, but I don't like the fact that we need to polyfill it for IE11. Can we just start with the other browsers for now? I'm worried about the extra bytes in CSS this will create.
Supporting all browsers including IE11 wouldn't be a problem. unset just chooses whether inherit or initial is used.
We'd just need to be more selective and thoughtful in the properties chosen and the reset used.
For example,
color: inherit;
padding: initial;
That being said, I forgot about the editor-styles mechanism.
It actually works against this solution. 😯
This targets the block controls even more specifically. To the point where unsetting .components-button wouldn't be specific enough to protect the insertion-point-button from user's button styles.
We can't increase the specificity of the unset or it'll override core styles added to single class selectors.
I'm going to think about it some more but right now I don't know how this initial proposal could work.
To be clear, my whole point here is that I want my editor buttons and inputs to look as they do on the front-end.
With these things largely coming from plugins and shortcodes, the [class], if there is one, can't be known at the time the theme is built. Styling the elements is the only way.
One possible solution here would be to modify the way the style wrapper #10956 works for non-"root" selectors. In addition to wrapping selectors, we add a :not() pseudo-class to them, negating a class or attribute assigned to editor components and controls using elements.
An example of a modified user's editor-style would look something like this:
The [class^="components-"] in the above example seems to cover it for the buttons but it may be better to add a new class or attribute, for targeting, to all non-div editor elements.
@m-e-h I'm curious — how much of this could be solved by applying a generic reset in CSS to our components and making their selectors more specific, so our component styles are less likely to be impacted? I think I'd personally feel more comfortable with that, vs. a CSS rewriting hack which seems more fragile.
Yes @chrisvanpatten a CSS reset like I originally proposed in this issue would work perfectly for those that are enqueuing their styles without using the editor-styles theme_support method.
However when using the current CSS rewriting hack, the themes styles become more specific than our reset: .editor-styles-wrapper button>.components-button so in this case the theme's button styles will override the block button controls.
Perhaps it's worth throwing a reset at the top of the components anyway to cover those not using the editor-styles?
For those that are using editor-styles, I'm not particularly a fan of the current CSS rewriting either but I don't know that modifying it would make it any more fragile than it already is.
Since the controls on the blocks don't specify min-height all of them will now be 3rem tall and appear as ovals instead of circles.
The following reset, placed before core defines any styles will ensure any properties not specified(by at least a single class selector) later in the stylesheet will retain their initial or inherited values. (actual would be more verbose for browser compat)
.components-button {
all: unset;
}
Now we add the wrapper to our theme's button style:
#11505 is a duplicate report I made of the above issue: button styles in Twenty Seventeen's editor CSS, pulled into the editor with editor-styles, can change the font sizes in some Gutenberg buttons. Affected buttons include media upload buttons, and the edit buttons in reusable blocks:
The culprit is the .editor-styles-wrapper button selector @m-e-h mentions above, which lets the editor styles override Gutenberg's own styles, in part because they're embedded.
It makes more sense to move that discussion here, so closing #11505 in favour of this issue!
I'm going to remove the documentation label on this, as I'm not entirely sure documentation is appropriate here. I was prepared to write this off as an edge case but if it's happening in the twenty(.*) themes, it probably warrants a closer look into how we can truly fix the problem (at least for buttons and possibly for other elements as well).
@jasmussen Thanks, as always for the thorough answer! That all makes sense
I'm not necessarily opposed to a theming system that goes outside the editing canvas (though I'm struggling to understand the use cases?)
I'm not talking about doing anything that we're not already doing. Many of the UI components, that this is meant to protect, aren't limited to the editing canvas.
I suppose for the purpose of this experiment we can start out assuming that everyone is using the editor-styles rewrite feature. I'll limit the reset to UI components within .editor-styles-wrapper.
and I've bumped up the specificity evenly across 'block-editor', 'editor', 'components', and 'edit-post' stylesheets using postcss during the build process.
Changing the default themes to add_editor_style( 'style.css' ) and everything is looking pretty close to perfect.
twentyseventeen style.css without the reset
twentyseventeen style.css with reset
I'll put my rough first pass together in a PR for some feedback and testing. I'd never even looked at Gutenberg's build process till last night so I'm definitely going to need some direction on any conventions used there.
I'd never even looked at Gutenberg's build process till last night so I'm definitely going to need some direction on any conventions used there.
Please if there's ever any blockage here, you know where to find me. You can ask openly for help in the core slack, #core-editor, or you can ping me directly (@joen in core slack), or just here. Happy to help, so we can get your expert input!
I've encountered similar issues with the components buttons as well as the permalink. As noticed before, it's problematic that Gutenberg's styles for editor controls are not wrapped in .editor-styles-wrapper, while the theme's editor styles are.
I like some of the suggestions made here, including the PR by @m-e-h, which seems hard to get right though. I know it's not so great from a pure CSS standpoint, but could we not wrap Gutenberg's styles in the .editor-styles-wrapper class as well (with a build step tool)? If the built stylesheet contained rules like .components-button, .editor-styles-wrapper .components-button { ... }, I think that would resolve most of the issues where generic theme editor styles (e.g. button, input, textarea, select { ... } override Gutenberg core component styles.
Reacted by Joen A., Joy Reynolds and Marty Helmick
I'd really like to explore some options here and hope I can find some time in the near future to do so. My extra time unfortunately disappeared around the time I started experimenting with solutions for this.
An editor-styles-wrapper would help some by increasing the specificity of the already declared style properties.
But I think the more vulnerable area here is when a theme or plugin uses style properties that haven't been declared on button, input, textarea, select. It's also a harder bug to track down since it has nothing to do with specificity like most would think.
For example, I don't think a wrapper would help at all with the case used at the start of this issue thread. #10178 (comment)
Taking a look at this with slightly fresher eyes, it seems clear that while theme developers can work within the limitations currently present due to the CSS bleed, it's still onerous and they shouldn't have to. So while it may not be urgent to address this issue, addressing this issue should make virtually all developers lives easier.
In my experience, it's always useful to look at the utopian solution first, and then plot a course towards that. In my estimation, Shadow DOM was created for this exact purpose; to encapsulate an element from inheriting styles. It seems like if we were able to encapsulate the most in-editor used components, such as the Block Toolbar, the Placeholder, and the Mover control, we'd be mostly there.
I know Shadow DOM has been explored and abandoned, but I can't recall what was holding us back — was it the lack of support in IE11/Classic Edge? Given the new Chromium based Edge supports Shadow DOM, could we consider editor styles to be a progressive enhancement for modern browsers?
Similar to how Shadow DOM protects the styles of an element, the most promising approach from this thread seemed to involve strengthening the UI styles to insulate them from being overridden by editor styles. The main point of feedback against this approach, cc @youknowriad, was that ideally we should be be content agnostic: since we can't know what additional components a plugin developer might add to the UI, it seems confusing to just protect a few elements.
But would this not also be the case with Shadow DOM? And is there a way to simplify the API surface so that controls you do not want to inherit theme styles can be wrapped? I believe this is what @m-e-h is referring to here.
So it seems like defining what content agnosticism we want is key to unsticking this issue. Let's explore that a bit:
There are UI elements that should never inherit CSS. Things like the <Button /> component, and many of the other — sliders, radio buttons, toggles checkboxes. Common to most of those is that there's a finite amount, and when plugin developers use them their experiences are consistent and arguably follow best practices.
There are also UI elements where you DO want for styles to be inherited. For example, selecting the Image block pops out a caption field. This caption field should inherit the font specified by the editor style.
Based on the above two take-aways, it seems like a pragmatic appraoch would be to:
Create an allowlist of Block Editor UI components to protect
Create instructions for developers on how to protect their own custom elements
Both of these options would be made much simpler and more scalable if we can add a uniform .class or data-att** on each element we need to protect.
To throw it out there, this could be an .is-protected class you add to components, or even data-protected-styles. For now we'd use those classes or attributes to strengthen their styles as @m-e-h suggested, but long term could we not also use those to target what to Shadow DOM encapsulate?
In the end this would provide developers with a set of components they know won't inherit CSS from theme styles, and instructions for how to protect their own elements in a similar way. Would love your thoughts, and would love to unstick this.
Reacted by Marty Helmick, David Romsey, Richard Lindhout and Jerome Toole
@mtias things like the block appender button (button.block-editor-button-block-appender) are super common and are rendered within the iframe, so any button {...} styles still bleed into them.
Activity
m-e-h commentedon Sep 25, 2018
I'm pretty sure
autoprefixerdoesn't coverallorunset. So an ie11 friendly version would look more like this: 😬m-e-h commentedon Oct 31, 2018
Reckon I should go ahead and do a PR on this?
Maybe we cut it down to a reset of only the most commonly used properties of
input,select,textareaandbutton.By adding the following to the top of
packages/block-library/src/style.scss, the editor becomes much more resistant to user styles causing deformed or broken controls.I've see zero side effects in my tests.
youknowriad commentedon Nov 1, 2018
This is indeed a good idea, but I don't like the fact that we need to polyfill it for IE11. Can we just start with the other browsers for now? I'm worried about the extra bytes in CSS this will create.
That said, I'm wondering if you considered using the editor styles mechanism to provide your editor styles, it shouldn't interfere that much using this technique. https://wordpress.org/gutenberg/handbook/extensibility/theme-support/#editor-styles
(The editor styles work better in 4.2 RC1)
m-e-h commentedon Nov 1, 2018
Supporting all browsers including IE11 wouldn't be a problem.
unsetjust chooses whetherinheritorinitialis used.We'd just need to be more selective and thoughtful in the properties chosen and the reset used.
For example,
That being said, I forgot about the editor-styles mechanism.
It actually works against this solution. 😯
This targets the block controls even more specifically. To the point where unsetting
.components-buttonwouldn't be specific enough to protect the insertion-point-button from user'sbuttonstyles.We can't increase the specificity of the unset or it'll override core styles added to single class selectors.
I'm going to think about it some more but right now I don't know how this initial proposal could work.
To be clear, my whole point here is that I want my editor buttons and inputs to look as they do on the front-end.
With these things largely coming from plugins and shortcodes, the
[class], if there is one, can't be known at the time the theme is built. Styling the elements is the only way.m-e-h commentedon Nov 5, 2018
Related:
#11503
#11505
#11132
m-e-h commentedon Nov 6, 2018
One possible solution here would be to modify the way the style wrapper #10956 works for non-"root" selectors. In addition to wrapping selectors, we add a
:not()pseudo-class to them, negating a class or attribute assigned to editor components and controls using elements.An example of a modified user's editor-style would look something like this:
The
[class^="components-"]in the above example seems to cover it for the buttons but it may be better to add a new class or attribute, for targeting, to all non-div editor elements.chrisvanpatten commentedon Nov 9, 2018
@m-e-h I'm curious — how much of this could be solved by applying a generic reset in CSS to our components and making their selectors more specific, so our component styles are less likely to be impacted? I think I'd personally feel more comfortable with that, vs. a CSS rewriting hack which seems more fragile.
m-e-h commentedon Nov 9, 2018
Yes @chrisvanpatten a CSS reset like I originally proposed in this issue would work perfectly for those that are enqueuing their styles without using the
editor-stylestheme_support method.However when using the current CSS rewriting hack, the themes styles become more specific than our reset:
.editor-styles-wrapper button>.components-buttonso in this case the theme'sbuttonstyles will override the blockbuttoncontrols.Perhaps it's worth throwing a reset at the top of the components anyway to cover those not using the
editor-styles?For those that are using
editor-styles, I'm not particularly a fan of the current CSS rewriting either but I don't know that modifying it would make it any more fragile than it already is.m-e-h commentedon Nov 9, 2018
An example
Since the controls on the blocks don't specify
min-heightall of them will now be3remtall and appear as ovals instead of circles.The following reset, placed before core defines any styles will ensure any properties not specified(by at least a single class selector) later in the stylesheet will retain their initial or inherited values.
(actual would be more verbose for browser compat)
Now we add the wrapper to our theme's
buttonstyle:It overrides our reset and our button control.
We can increase our reset specificity:
But now our reset is overriding our own control styles.
Quite the can of worms.
laurelfulford commentedon Nov 12, 2018
#11505 is a duplicate report I made of the above issue: button styles in Twenty Seventeen's editor CSS, pulled into the editor with
editor-styles, can change the font sizes in some Gutenberg buttons. Affected buttons include media upload buttons, and the edit buttons in reusable blocks:The culprit is the
.editor-styles-wrapper buttonselector @m-e-h mentions above, which lets the editor styles override Gutenberg's own styles, in part because they're embedded.It makes more sense to move that discussion here, so closing #11505 in favour of this issue!
chrisvanpatten commentedon Nov 13, 2018
Hey folks -
I'm going to remove the documentation label on this, as I'm not entirely sure documentation is appropriate here. I was prepared to write this off as an edge case but if it's happening in the
twenty(.*)themes, it probably warrants a closer look into how we can truly fix the problem (at least for buttons and possibly for other elements as well).22 remaining items
m-e-h commentedon Apr 30, 2019
@jasmussen Thanks, as always for the thorough answer! That all makes sense
I'm not talking about doing anything that we're not already doing. Many of the UI components, that this is meant to protect, aren't limited to the editing canvas.
I suppose for the purpose of this experiment we can start out assuming that everyone is using the
editor-stylesrewrite feature. I'll limit the reset to UI components within.editor-styles-wrapper.Testing with just this:
and I've bumped up the specificity evenly across 'block-editor', 'editor', 'components', and 'edit-post' stylesheets using postcss during the build process.
Changing the default themes to
add_editor_style( 'style.css' )and everything is looking pretty close to perfect.twentyseventeen

style.csswithout the resettwentyseventeen

style.csswith resetI'll put my rough first pass together in a PR for some feedback and testing. I'd never even looked at Gutenberg's build process till last night so I'm definitely going to need some direction on any conventions used there.
jasmussen commentedon May 1, 2019
That's very cool!
Please if there's ever any blockage here, you know where to find me. You can ask openly for help in the core slack, #core-editor, or you can ping me directly (@joen in core slack), or just here. Happy to help, so we can get your expert input!
m-e-h commentedon May 1, 2019
Thanks @jasmussen !
PR created here #15377
Any testing anyone can do would be awesome!
It's still an early draft. I know I need to move some of the files around but it seems to work well.
felixarntz commentedon Oct 1, 2019
I've encountered similar issues with the components buttons as well as the permalink. As noticed before, it's problematic that Gutenberg's styles for editor controls are not wrapped in
.editor-styles-wrapper, while the theme's editor styles are.I like some of the suggestions made here, including the PR by @m-e-h, which seems hard to get right though. I know it's not so great from a pure CSS standpoint, but could we not wrap Gutenberg's styles in the
.editor-styles-wrapperclass as well (with a build step tool)? If the built stylesheet contained rules like.components-button, .editor-styles-wrapper .components-button { ... }, I think that would resolve most of the issues where generic theme editor styles (e.g.button, input, textarea, select { ... }override Gutenberg core component styles.jasmussen commentedon Oct 1, 2019
My plate is all kinds of full, but this is still on my radar. I hope to resurrect the ongoing efforts here that were close, if I recall correctly.
m-e-h commentedon Oct 1, 2019
I'd really like to explore some options here and hope I can find some time in the near future to do so. My extra time unfortunately disappeared around the time I started experimenting with solutions for this.
An
editor-styles-wrapperwould help some by increasing the specificity of the already declared style properties.But I think the more vulnerable area here is when a theme or plugin uses style properties that haven't been declared on
button, input, textarea, select. It's also a harder bug to track down since it has nothing to do with specificity like most would think.For example, I don't think a wrapper would help at all with the case used at the start of this issue thread. #10178 (comment)
jasmussen commentedon Oct 8, 2019
Taking a look at this with slightly fresher eyes, it seems clear that while theme developers can work within the limitations currently present due to the CSS bleed, it's still onerous and they shouldn't have to. So while it may not be urgent to address this issue, addressing this issue should make virtually all developers lives easier.
In my experience, it's always useful to look at the utopian solution first, and then plot a course towards that. In my estimation, Shadow DOM was created for this exact purpose; to encapsulate an element from inheriting styles. It seems like if we were able to encapsulate the most in-editor used components, such as the Block Toolbar, the Placeholder, and the Mover control, we'd be mostly there.
I know Shadow DOM has been explored and abandoned, but I can't recall what was holding us back — was it the lack of support in IE11/Classic Edge? Given the new Chromium based Edge supports Shadow DOM, could we consider editor styles to be a progressive enhancement for modern browsers?
Similar to how Shadow DOM protects the styles of an element, the most promising approach from this thread seemed to involve strengthening the UI styles to insulate them from being overridden by editor styles. The main point of feedback against this approach, cc @youknowriad, was that ideally we should be be content agnostic: since we can't know what additional components a plugin developer might add to the UI, it seems confusing to just protect a few elements.
But would this not also be the case with Shadow DOM? And is there a way to simplify the API surface so that controls you do not want to inherit theme styles can be wrapped? I believe this is what @m-e-h is referring to here.
So it seems like defining what content agnosticism we want is key to unsticking this issue. Let's explore that a bit:
<Button />component, and many of the other — sliders, radio buttons, toggles checkboxes. Common to most of those is that there's a finite amount, and when plugin developers use them their experiences are consistent and arguably follow best practices.Based on the above two take-aways, it seems like a pragmatic appraoch would be to:
Potentially this would solve 80% of our problems.
And it might even plot us a path towards Shadow DOM. Long term, Shadow DOM would protect elements, but near term, higher element specificity would do so. Specifically this idea seems like it could be compatible with an allowlist of elements to protect:
To throw it out there, this could be an
.is-protectedclass you add to components, or evendata-protected-styles. For now we'd use those classes or attributes to strengthen their styles as @m-e-h suggested, but long term could we not also use those to target what to Shadow DOM encapsulate?In the end this would provide developers with a set of components they know won't inherit CSS from theme styles, and instructions for how to protect their own elements in a similar way. Would love your thoughts, and would love to unstick this.
paaljoachim commentedon Aug 3, 2021
Can we get an update from someone on this very old issue?
Thank you!
fabiankaegy commentedon May 30, 2022
The current PR that seems to work in this issue is #33494
mtias commentedon Jul 2, 2024
Unless I'm missing something, the introduction of the iframe for the editor content should be preventing the most common of these issues.
tcmulder commentedon Jan 20, 2025
@mtias things like the block appender button (
button.block-editor-button-block-appender) are super common and are rendered within the iframe, so anybutton {...}styles still bleed into them.