{"id":374910,"date":"2022-11-07T06:05:11","date_gmt":"2022-11-07T14:05:11","guid":{"rendered":"https:\/\/css-tricks.com\/?p=374910"},"modified":"2024-05-01T10:38:17","modified_gmt":"2024-05-01T17:38:17","slug":"managing-css-styles-in-a-wordpress-block-theme","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/managing-css-styles-in-a-wordpress-block-theme\/","title":{"rendered":"Managing CSS Styles in a WordPress Block Theme"},"content":{"rendered":"\n
The way we write CSS for WordPress themes is in the midst of sweeping changes. I recently shared a technique for adding fluid type support in WordPress<\/a> by way of Wait, no So, what does styling actually look like in these WordPress FSE days? That\u2019s what I want to cover in this article. There\u2019s a lack of documentation for styling block themes in the WordPress Theme Developer Handbook<\/a>, so everything we\u2019re covering here is what I\u2019ve gathered about where things currently are as well as discussions about the future of WordPress theming.<\/p>\n\n\n\n\n\n\n The new developmental features that are included in WordPress 6.1<\/a> get us closer to a system of styles that are completely defined in Another way we can get a feel for where we are is by looking at the evolution of default WordPress themes<\/a>. To date, there are three default themes that support full-site editing:<\/p>\n\n\n\n But don\u2019t start trading the CSS in That leaves us in a relatively tough spot. Not only is there no clear path for overriding theme styles<\/a>, but it\u2019s unclear where the source of those styles even come from \u2014 is it from different layers of You might be wondering why WordPress is moving toward a JSON-based definition of styles instead of a traditional CSS file. Ben Dwyer from the Gutenberg development team eloquently articulates why the It\u2019s worth reading Ben\u2019s post, but the meat is in this quote:<\/p>\n\n\n\n Overriding CSS, whether layout, preset, or block styles, presents an obstacle to integration and interoperability: visual parity between the frontend and editor becomes more difficult to maintain, upgrades to block internals may conflict with overrides. Custom CSS is, furthermore, less portable across other block themes.<\/p>\n\n\n\n By encouraging theme authors to use One of the major benefits of moving CSS to JSON is that JSON is a machine-readable format, which means it can be exposed in the WordPress Site Editor UI by fetching an API, thus allowing users to modify default values and customize a site\u2019s appearance without writing any CSS at all. It also provides a way to style blocks consistently, while providing a structure that creates layers of specificity such that the user settings take the highest priority over those defined in Developers maintain consistency in JSON, and users gain flexibility with code-less customizations. That\u2019s a win-win.<\/p>\n\n\n\n There are other benefits, for sure, and Mike McAlister from WP Engine lists several in this Twitter thread<\/a>. You can find even more benefits in this in-depth discussion<\/a> over at the Make WordPress Core blog. And once you\u2019ve given that a read, compare the benefits of the JSON approach with the available ways to define and override styles in classic themes<\/a>.<\/p>\n\n\n We\u2019ve already seen a lot of progress as far as what parts of a theme And we do that by defining JSON elements<\/strong><\/a>. Think of elements as individual components that live in a WordPress block. Say we have a block that contains a heading, a paragraph, and a button. Those individual pieces are elements, and there\u2019s an A better way to describe JSON elements is as low-level components for themes and blocks that do not need the complexity of blocks. They are representations of HTML primitives<\/a> that are not defined in a block but can be used across blocks. How they are supported in WordPress (and the Gutenberg plugin) is described in the Block Editor Handbook<\/a> and this Full Site Editing tutorial<\/a> by Carolina Nymark.<\/p>\n\n\n\n For example, links are styled in the Here is a table of the elements that are currently available to style in theme.json<\/code>, a new file that WordPress has been pushing hard<\/a> to become a central source of truth for defining styles in WordPress themes that support full-site editing (FSE) features.<\/p>\n\n\n\n
style.css<\/code> file? We still have that. In fact,
style.css<\/code> is still a required file<\/a> in block themes, though its role is greatly reduced to meta information used for registering the theme. That said, the fact is that
theme.json<\/code> is still in active development, meaning we\u2019re in a transitional period where you might find styles defined there, in
styles.css<\/code> or even at the block level.<\/p>\n\n\n\n
The evolution of WordPress styles<\/h3>\n\n\n
theme.json<\/code>, but there is still be plenty of work to do before we can fully lean on it. One way we can get an idea of what\u2019s coming in future releases is by using the Gutenberg plugin<\/a>. This is where experimental features are often given a dry run.<\/p>\n\n\n\n
\n
style.css<\/code>. There is no
theme.json<\/code> file.<\/a> TT1 Blocks is the first look we got at styling in the Block Editor era, and we can consider it a teaser more than a model.<\/li>\n\n\n\n
theme.json<\/code>. The file contains only 373 lines of code<\/a>. Its lead developers had made concerted efforts to make this a CSS-less theme; however,
style.css<\/code> still shipped with just under 150 lines of code<\/a> since not all of the issues with
theme.json<\/code> were resolved in the experimental Gutenberg plugin ahead of release.<\/li>\n\n\n\n
style.css<\/code> file<\/a>.<\/li>\n<\/ul>\n\n\n\n
style.css<\/code> for JSON property-value pairs in
theme.json<\/code> just yet. There are still CSS styling rules that need to be supported in
theme.json<\/code> before we think about doing that. The remaining significant issues are currently being discussed with an aim to fully move all the CSS style rules into
theme.json<\/code> and consolidate different sources of
theme.json<\/code> into a UI for for setting global styles<\/a> directly in the WordPress Site Editor<\/a>.<\/p>\n\n\n\n
theme.json<\/code> files,
style.css<\/code>, the Gutenberg plugin, or somewhere else?<\/p>\n\n\n
Why
theme.json<\/code> instead of
style.css<\/code>?<\/h3>\n\n\n
theme.json<\/code> approach is a benefit for theme developers<\/a>.<\/p>\n\n\n\n
\n
theme.json<\/code> API where possible, the hierarchy of \u201cbase > theme > user\u201d defined styles can be resolved correctly.<\/p>\n<\/blockquote>\n\n\n\n
theme.json<\/code>. That interplay between theme-level styles in
theme.json<\/code> and the user-defined styles in the Global Styles UI is what makes the JSON approach so appealing.<\/p>\n\n\n\n
Defining styles with JSON elements<\/h3>\n\n\n
theme.json<\/code> is capable of styling. Prior to WordPress 6.1, all we could really do was style headings and links. Now, with WordPress 6.1, we can add buttons, captions, citations, and headings<\/a> to the mix.<\/p>\n\n\n\n
elements<\/code> object in
theme.json<\/code> where we define their styles:<\/p>\n\n\n\n
{\n \"version\": 2,\n \"settings\": {},\n \/\/ etc.\n \"styles\": {\n \/\/ etc.\n \"elements\": {\n \"button\": { ... },\n \"h1\": { ... },\n \"heading\": { ... },\n },\n },\n \"templateParts\": {}\n}<\/code><\/pre>\n\n\n\n
elements<\/code> object but are not a block in their own right. But a link can be used in a block and it will inherit the styles defined on the
elements.link<\/code> object in
theme.json<\/code>. This doesn\u2019t fully encapsulate the definition of an element, though, as some elements are also registered as blocks, such as the Heading and Button blocks \u2014 but those blocks can still be used within other blocks.<\/p>\n\n\n\n
theme.json<\/code> prior to WordPress 6.1, courtesy of Carolina<\/a>:<\/p>\n\n\n\n