{"id":376237,"date":"2023-01-09T05:56:19","date_gmt":"2023-01-09T13:56:19","guid":{"rendered":"https:\/\/css-tricks.com\/?p=376237"},"modified":"2023-02-06T07:09:39","modified_gmt":"2023-02-06T15:09:39","slug":"styling-buttons-in-wordpress-block-themes","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/styling-buttons-in-wordpress-block-themes\/","title":{"rendered":"Styling Buttons in WordPress Block Themes"},"content":{"rendered":"\n
A little while back, Ganesh Dahal penned a post here on CSS-Tricks<\/a> responding to a tweet that asked about adding CSS box shadows on WordPress blocks and elements. There\u2019s a lot of great stuff in there that leverages new features that shipped in WordPress 6.1 that provide controls for applying shadows to things directly in the Block Editor and Site Editor UI.<\/p>\n\n\n\n Ganesh touched briefly on button elements in that post. I want to pick that up and go deeper into approaches for styling buttons in WordPress block themes. Specifically, we\u2019re going to crack open a fresh Why buttons, you ask? That\u2019s a good question, so let\u2019s start with that.<\/p>\n\n\n\n\n\n\n When we\u2019re talking about buttons in the context of the WordPress Block Editor, we have to distinguish between two different types:<\/p>\n\n\n\n If we add both of these blocks to a template, they have the same look by default.<\/p>\n\n\n\n But the markup is very different:<\/p>\n\n\n\n As we can see, the HTML tag names are different. It\u2019s the common classes \u2014 If we were writing CSS, we would target these two classes. But as we know, WordPress block themes have a different way of managing styles, and that\u2019s through the So, how do we define button styles in We\u2019ll focus specifically on the So what we need to do is define a That Rather than having to style each individual block, we create shared styles. Let\u2019s go ahead and change the default background and text color for both types of buttons in our theme. There\u2019s a This changes the color of both button types:<\/p>\n\n\n\n If crack open DevTools and have a look at the CSS that WordPress generates for the buttons, we see that the Those are our default colors! Next, we want to give users visual feedback when they interact with the button.<\/p>\n\n\n Since this is a site all about CSS, I\u2019d bet many of you are already familiar with the interactive states of links and buttons. We can Those are CSS pseudo-classes<\/a> and we use them to target a link\u2019s or button\u2019s interactions<\/a>.<\/p>\n\n\n\n In CSS, we might style a In Notice the \u201cstructured\u201d nature of this. We\u2019re basically following an outline:<\/p>\n\n\n\n We now have a complete definition of our button\u2019s default and interactive styles. But what if we want to style certain buttons that are nested in other blocks?<\/p>\n\n\n Let\u2019s imagine that we want all buttons to have our base styles, with one exception. We want the submit button of the Post Comment Form block to be blue. How would we achieve that?<\/p>\n\n\n\n This block is more complex than the Button block because it has more moving parts: the form, inputs, instructive text, and the button. In order to target the button in this block, we have to follow the same sort of JSON structure we did for the Notice that we\u2019re no longer working in The JSON structure supports elements within elements. So, if there\u2019s a This selector means that not only are we targeting a specific block \u2014 we\u2019re targeting a specific element that is contained in that block. Now we have a default set of button styles that are applied to all buttons in the theme, and a set of styles that apply to specific buttons that are contained in the Post Comment Form block.<\/p>\n\n\n\n The CSS generated by WordPress has a more precise selector as a result:<\/p>\n\n\n\ntheme.json<\/code> file and break down various approaches to styling buttons in the schema.<\/p>\n\n\n\nThe different types of buttons<\/h3>\n\n\n
\n
<\/figure>\n\n\n\n<div class=\"wp-block-button\">\n <a class=\"wp-block-button__link wp-element-button\">Button 1<\/a>\n<\/div>\n<p class=\"form-submit wp-block-button\">\n <input name=\"submit\" type=\"submit\" id=\"submit\" class=\"wp-block-button__link wp-element-button\" value=\"Post Comment\"> \n<\/p><\/code><\/pre>\n\n\n\n.wp-block-button<\/code> and .wp-element-button<\/code> \u2014 that ensure consistent styling between the two buttons.<\/p>\n\n\n\ntheme.json<\/code> file. Ganesh also covered this in great detail<\/a>, and you\u2019d do well giving his article a read.<\/p>\n\n\n\ntheme.json<\/code> without writing actual CSS? Let\u2019s do it together.<\/p>\n\n\nCreating the base styles<\/h3>\n\n\n
theme.json<\/code> is a structured set of schema written in property:value pairs. The top level properties are called \u201csections\u201d, and we\u2019re going to work with the styles<\/code> section. This is where all the styling instructions go.<\/p>\n\n\n\nelements<\/code> in the styles<\/code>. This selector targets HTML elements that are shared between blocks. This is the basic shell we\u2019re working with:<\/p>\n\n\n\n\/\/ theme.json\n{\n \"version\": 2,\n \"styles\": {\n \"elements\": {\n \/\/ etc.\n }\n }\n}<\/code><\/pre>\n\n\n\nbutton<\/code> element.<\/p>\n\n\n\n={\n \"version\": 2,\n \"styles\": {\n \"elements\": {\n \"button\": {\n \/\/ etc.\n }\n }\n }\n}<\/code><\/pre>\n\n\n\nbutton<\/code> corresponds to HTML elements that are used to mark up button elements on the front end. These buttons contain HTML tags that could be either of our two button types: a standalone component (i.e. the Button block) or a component nested within another block (e.g. the Post Comment block).<\/p>\n\n\n\ncolor<\/code> object in there that, in turn, supports background<\/code> and text<\/code> properties where we set the values we want:<\/p>\n\n\n\n{\n \"version\": 2,\n \"styles\": {\n \"elements\": {\n \"button\": {\n \"color\": {\n \"background\": \"#17a2b8\",\n \"text\": \"#ffffff\"\n }\n }\n }\n }\n}<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n.wp-element-button<\/code> class adds the styles we defined in theme.json<\/code>:<\/p>\n\n\n\n.wp-element-button {\n background-color: #17a2b8;\n color: #ffffff;\n}<\/code><\/pre>\n\n\n\nImplementing interactive button styles<\/h3>\n\n\n
:hover<\/code> the mouse cursor over them, tab them into :focus<\/code>, click on them to make them :active<\/code>. Heck, there\u2019s even a :visited<\/code> state to give users a visual indication that they\u2019ve clicked this before.<\/p>\n\n\n\n:hover<\/code> state like this:<\/p>\n\n\n\na:hover {\n \/* Styles *\/\n}<\/code><\/pre>\n\n\n\ntheme.json<\/code>, we\u2019re going to extend our existing button declaration with these pseudo-classes.<\/p>\n\n\n\n{\n \"version\": 2,\n \"styles\": {\n \"elements\": {\n \"button\": {\n \"color\": {\n \"background\": \"#17a2b8\",\n \"text\": \"#ffffff\"\n },\n \":hover\": {\n \"color\": {\n \"background\": \"#138496\"\n }\n },\n \":focus\": {\n \"color\": {\n \"background\": \"#138496\"\n }\n },\n \":active\": {\n \"color\": {\n \"background\": \"#138496\"\n }\n }\n }\n }\n }\n}<\/code><\/pre>\n\n\n\n\n
\n
\n
\n
\n
Styling buttons nested in individual blocks<\/h3>\n\n\n
button<\/code> element, but applied to the Post Comment Form block, which is mapped to the core\/post-comments-form<\/code> element:<\/p>\n\n\n\n{\n \"version\": 2,\n \"styles\": {\n \"elements\" {\n \"button\": {\n \/\/ Default button styles\n }\n }\n \"blocks\": {\n \"core\/post-comments-form\": {\n \/\/ etc.\n }\n }\n }\n}<\/code><\/pre>\n\n\n\nelements<\/code> anymore. Instead, we\u2019re working inside blocks<\/code> which is reserved for configuring actual blocks. Buttons, by contrast, are considered a global element since they can be nested in blocks, even though they are available as a standalone block too.<\/p>\n\n\n\nbutton<\/code> element in the Post Comment Form block, we can target it in the core\/post-comments-form<\/code> block:<\/p>\n\n\n\n{\n \"version\": 2,\n \"styles\": {\n \"elements\" {\n \"button\": {\n \/\/ Default button styles\n }\n }\n \"blocks\": {\n \"core\/post-comments-form\": {\n \"elements\": {\n \"button\": {\n \"color\": {\n \"background\": \"#007bff\"\n }\n }\n }\n }\n }\n }\n}<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n.wp-block-post-comments-form .wp-element-button,\n.wp-block-post-comments-form .wp-block-button__link {\n background-color: #007bff;\n}<\/code><\/pre>\n\n\n\n