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
When editing the styles for a specific block, it might be nice to offer an input for custom css. This way folks don't have to worry about finding / using the best selector.
Mockup: a "Custom CSS" button at the bottom of the Advanced section of a Group block, which slides the inspector to the right just as the content-only editing system, and Global Styles, does:
This could align well with how it could look for Global Styles:
We'd like to start working on this issue, but there's a decision that needs to be made before we start.
The main problem we saw when discussing this, is how to handle the selectors in our custom-CSS field.
Assuming we want the final CSS to be like this:
.wp-block-search{background:#222;border:0;}
we have a few ways to accomplish that:
Allow users to arbitrarily add custom CSS
The problem with this approach is that each snippet of custom-CSS in this case is not actually "per-block", but global. I can easily add some CSS in the search block, and target an image block - which is something we should definitely avoid.
Use a hard-coded selector per-block
We could define in block.json that the selector for the search block is .wp-block-search. In this case, the per-block "custom-CSS" field would accept something like this:
background: #222;
border: 0;
Internally we can then build the CSS from that, by doing $selector { $customCSS }.
That's simple and can work. However, if we go with something like that, it's impossible to target pseudo-elements and states like :hover, :focus, :after and so on. So while the example CSS above can be written by omitting the selector, we wouldn't be able to write something like this:
So we need to come up with a different solution.
One option would be to use a placeholder like el (short for element). In this case, we can just replace .wp-block-search with el when writing the CSS, and internally we make the conversion.
Another option would be to omit the selector completely, so the user would write something like this:
I personally like that last one as it would provide a lot of flexibility, but in this case users wouldn't be able to just copy-paste a snippet they found online and expect it to work, not to mention the complaints we'll get like "After reinventing HTML, now you want to reinvent CSS"
We need to decide how we want the CSS to be formatted - because it definitely should not be 100% freeform, it would defeat the "per-block" purpose.
It's exciting to see this option being explored, and I see a lot of great ideas have been shared already!
I love the idea of having a variable like :self (or :block) to reference the dynamically-generated block ID for easy block-scoping specificity. This is what Elementor does with its Custom CSS section, and it's perfect for fulfilling styling edge cases that no-code tools can't cover:
With that said, I'd like to gently challenge the assumption that this custom CSS should always be wrapped in a :self selector.
Consider blocks that render child elements, such as WooCommerce's Hand-Picked Products block. In these cases, locking the custom CSS so it can only affect the block's container element via :self would remove most of the utility this section would otherwise provide.
I recognize that this section would be a tool for folks of all CSS skill levels to use, and how that requirement introduces some tricky UX challenges. If the custom CSS could be locked to :self by default, but with some sort of option to unlock it without cluttering the interface, that would be ideal in my opinion!
Looking at the options, if we actually want this CSS to be scoped to the block, I'd favor this approach (not using :self or :block but making sure everything is nested within the own block selector derived from block.json).
The rationale is that if we use :self or :block, how do we make sure this does not happen?
:block{/* some block styles go here */}.wp-element-link{/* CSS unrelated to the actual block */}
Looking at the options, if we actually want this CSS to be scoped to the block, I'd favor #44412 (comment) (not using :self or :block but making sure everything is nested within the own block selector derived from block.json).
I tend to agree with that assessment... The danger we run if CSS is unscoped, is that we're basically introducing 70+ custom-css fields that can be applied globally 😅
I also lean towards enforcing the scope selector initially and remove possible footguns. Tracing orphaned selectors across a large collection of blocks is not going to be fun. With native CSS nesting hopefully coming soon we should be able to design for this anyways:
:block{/* some block styles go here */.wp-element-link{/* CSS unrelated to the actual block */}}}
Which would handle most needs in more complex blocks. Alternatively we could allow :block .wp-element-link {}.
Activity
mtias commentedon Oct 7, 2022
Some prior art: #25413
jasmussen commentedon Oct 25, 2022
jameskoster commentedon Oct 25, 2022
I like the implementation – a dedicated panel makes sense and provides adequate space. The connection to Global Styles is nice too.
But that Advanced panel is starting to look a little chaotic. Obviously it's separate, but I wonder if we should give it the Design Tools treatment?
jasmussen commentedon Oct 25, 2022
Sounds good to me, including keeping that separate! 🚀
Alright, I'll update issues.
jasmussen commentedon Oct 25, 2022
Added "Needs Dev" and updated the main issue. Should be good to work on!
aristath commentedon Nov 9, 2022
We'd like to start working on this issue, but there's a decision that needs to be made before we start.
The main problem we saw when discussing this, is how to handle the selectors in our custom-CSS field.
Assuming we want the final CSS to be like this:
we have a few ways to accomplish that:
Allow users to arbitrarily add custom CSS
The problem with this approach is that each snippet of custom-CSS in this case is not actually "per-block", but global. I can easily add some CSS in the search block, and target an image block - which is something we should definitely avoid.
Use a hard-coded selector per-block
We could define in
block.jsonthat the selector for the search block is.wp-block-search. In this case, the per-block "custom-CSS" field would accept something like this:Internally we can then build the CSS from that, by doing
$selector { $customCSS }.That's simple and can work. However, if we go with something like that, it's impossible to target pseudo-elements and states like
:hover,:focus,:afterand so on. So while the example CSS above can be written by omitting the selector, we wouldn't be able to write something like this:So we need to come up with a different solution.
One option would be to use a placeholder like
el(short for element). In this case, we can just replace.wp-block-searchwithelwhen writing the CSS, and internally we make the conversion.Another option would be to omit the selector completely, so the user would write something like this:
Or, something like this:
I personally like that last one as it would provide a lot of flexibility, but in this case users wouldn't be able to just copy-paste a snippet they found online and expect it to work, not to mention the complaints we'll get like "After reinventing HTML, now you want to reinvent CSS"
We need to decide how we want the CSS to be formatted - because it definitely should not be 100% freeform, it would defeat the "per-block" purpose.
17 remaining items
noisysocks commentedon Nov 21, 2022
@Mamaduka: I see you're working on #30142. Do you think it also makes sense to take this one? They seem closely related 🙂
wingmatt commentedon Nov 23, 2022
It's exciting to see this option being explored, and I see a lot of great ideas have been shared already!
I love the idea of having a variable like :self (or :block) to reference the dynamically-generated block ID for easy block-scoping specificity. This is what Elementor does with its Custom CSS section, and it's perfect for fulfilling styling edge cases that no-code tools can't cover:
With that said, I'd like to gently challenge the assumption that this custom CSS should always be wrapped in a :self selector.
Consider blocks that render child elements, such as WooCommerce's Hand-Picked Products block. In these cases, locking the custom CSS so it can only affect the block's container element via :self would remove most of the utility this section would otherwise provide.
I recognize that this section would be a tool for folks of all CSS skill levels to use, and how that requirement introduces some tricky UX challenges. If the custom CSS could be locked to :self by default, but with some sort of option to unlock it without cluttering the interface, that would be ideal in my opinion!
oandregal commentedon Dec 1, 2022
Looking at the options, if we actually want this CSS to be scoped to the block, I'd favor this approach (not using
:selfor:blockbut making sure everything is nested within the own block selector derived fromblock.json).The rationale is that if we use
:selfor:block, how do we make sure this does not happen?aristath commentedon Dec 1, 2022
I tend to agree with that assessment... The danger we run if CSS is unscoped, is that we're basically introducing 70+ custom-css fields that can be applied globally 😅
oandregal commentedon Dec 1, 2022
Shared #30142 (comment) a potential first step for both this issue and the other one.
mtias commentedon Dec 1, 2022
I also lean towards enforcing the scope selector initially and remove possible footguns. Tracing orphaned selectors across a large collection of blocks is not going to be fun. With native CSS nesting hopefully coming soon we should be able to design for this anyways:
Which would handle most needs in more complex blocks. Alternatively we could allow
:block .wp-element-link {}.