{"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 theme.json<\/code> file and break down various approaches to styling buttons in the schema.<\/p>\n\n\n\n

Why buttons, you ask? That\u2019s a good question, so let\u2019s start with that.<\/p>\n\n\n\n\n\n\n

The different types of buttons<\/h3>\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

    \n
  1. Child blocks inside of the Buttons block<\/li>\n\n\n\n
  2. Buttons that are nested inside other block (e.g. the Post Comments Form block)<\/li>\n<\/ol>\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

    \"A<\/figure>\n\n\n\n

    But the markup is very different:<\/p>\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

    As we can see, the HTML tag names are different. It\u2019s the common classes \u2014 .wp-block-button<\/code> and .wp-element-button<\/code> \u2014 that ensure consistent styling between the two buttons.<\/p>\n\n\n\n

    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 theme.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\n

    So, how do we define button styles in theme.json<\/code> without writing actual CSS? Let\u2019s do it together.<\/p>\n\n\n

    Creating 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\n

    We\u2019ll focus specifically on the elements<\/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\n

    So what we need to do is define a button<\/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\n

    That button<\/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\n

    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 color<\/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

    This changes the color of both button types:<\/p>\n\n\n\n

    \"A<\/figure>\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 .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\n

    Those are our default colors! Next, we want to give users visual feedback when they interact with the button.<\/p>\n\n\n

    Implementing interactive button styles<\/h3>\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 :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

    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 :hover<\/code> state like this:<\/p>\n\n\n\n

    a:hover {\n  \/* Styles *\/\n}<\/code><\/pre>\n\n\n\n

    In theme.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

    Notice the \u201cstructured\u201d nature of this. We\u2019re basically following an outline:<\/p>\n\n\n\n