Using CSS custom properties in a block. #123
Replies: 4 comments
-
Resources: https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/ |
Beta Was this translation helpful? Give feedback.
-
@mburridge this article should probably also cover the new Selectors API. This will be included in 6.3 and is a more official way to apply block support styles to different elements of a block. |
Beta Was this translation helpful? Give feedback.
-
Conversation locked after. Continue discussion on the issue |
Beta Was this translation helpful? Give feedback.
-
Now published: Styling blocks: empowering users with CSS custom properties |
Beta Was this translation helpful? Give feedback.
-
The
Block Supports
API allows you add colour and spacing settings to your block with a simple definition inblock.json
. However, this only allows "block level" styling, and is fine for simple blocks. If your block is more complex and consists of a hierarchy of HTML elements which need to be styled using the CSS cascade, and you want to allow users to have control over that styling, then another solution needs to be sought.This solution is not immediately obvious and involves using CSS custom properties (also called CSS variables) which get their values from the block attributes and passes them to the block's wrapping element's
useBlockProps
hook. A similar task can be done withrender.php
in a dynamic block, where the CSS custom properties are instead passed to theget_block_wrapper_attributes
function.The proposed post is a tutorial that illustrates using CSS custom properties in a block project.
Beta Was this translation helpful? Give feedback.
All reactions