csswordpresswordpress-themingwordpress-plugin-creation

How to set the width of a Custom Block in a Wordpress Plugin on the Front End


I am trying to set the width of a custom block in a Wordpress Plugin. When I look at the CSS on the front end with the developer tools I am seeing this

element {
}
:where(body .is-layout-constrained) > :last-child:last-child {
  margin-block-end: 0;
}
:where(body .is-layout-constrained) > :first-child:first-child {
  margin-block-start: 0;
}
body .is-layout-constrained > :where(:not(.alignleft):not(.alignright):not(.alignfull)) {
  max-width: var(--wp--style--global--content-size);
  margin-left: auto !important;
  margin-right: auto !important;
}
.wp-block-create-block-my-custom-block {
  ...
  max-width: 100%;
  ...
}
...

As you can see the is-layout-constrained class is taking precedent over my wp-block-create-block-my-custom-block.

I would like the block to be able to expand and fill the width of the page.

I have seen some information involving the theme.json but as far as I can tell those solutions only work for themes not for plugins.

I can set the --wp--style--global--content-size variable in my wp-block-create-block-my-custom-block class but this does not feel like the "correct" solution.

What is the correct way to set the max-width in a Wordpress Plugin?


Solution

  • In block.json you can set an "align" property under "supports":

    {
      ...
      "supports": {
        "align": ["full", "wide"]
      }
      ...
    }
    

    This will add a button to the block toolbar that allows you to set the width of the block.