{"id":375278,"date":"2022-11-30T06:11:10","date_gmt":"2022-11-30T14:11:10","guid":{"rendered":"https:\/\/css-tricks.com\/?p=375278"},"modified":"2022-11-30T06:11:11","modified_gmt":"2022-11-30T14:11:11","slug":"using-the-new-constrained-layout-in-wordpress-block-themes","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/using-the-new-constrained-layout-in-wordpress-block-themes\/","title":{"rendered":"Using The New Constrained Layout In WordPress Block Themes"},"content":{"rendered":"\n
One of the main goals of the WordPress Site Editor (and, yes, that is now the \u201cofficial\u201d name<\/a>) is to move basic block styling from<\/a> CSS to structured JSON<\/a>. JSON files are machine-readable, which makes it consumable by the JavaScript-based Site Editor for configuring a theme\u2019s global styles directly in WordPress.<\/p>\n\n\n\n It\u2019s not all the way there yet! If we look at the Twenty Twenty-Two (TT2) default theme, there were two main unresolved issues: styling interactions<\/a> (like WordPress 6.1<\/a> fixed those issues and what I want to do is look specifically at the latter. Now that we have JSON-ified styles for the margins and padding of layout containers, that opens us up to more flexible and robust ways to define spacing in our theme layouts<\/strong>.<\/p>\n\n\n\n\n\n\n First off, we already have root-level padding<\/strong> which is a fancy way of describing padding on the But there\u2019s more to it because now we have a way for blocks to bypass that padding and align themselves full-width. That\u2019s thanks to padding-aware alignments<\/strong> which is a new opt-in feature in That gets us to another thing we get: constrained layouts<\/strong>. The idea here is that any blocks nested in the layout respect the layout\u2019s content width \u2014 which is a global setting \u2014 and do not flow outside of it. We can override that behavior on a block-by-block basis with alignments, but we\u2019ll get to that.<\/p>\n\n\n\n Let\u2019s start with\u2026<\/p>\n\n\n Again, this isn\u2019t new. We\u2019ve had the ability to set padding on the This is a global setting. So, if we were to crack open DevTools and inspect the Cool. But herein lies the issue of how in the world we can allow some blocks to break out of that spacing to fill the full screen, edge-to-edge. That\u2019s why the spacing is there, right? It helps prevent that from happening!<\/p>\n\n\n\n But there are indeed plenty of cases where you might want to break out of that spacing on a one-off instance when working in the Block Editor. Say we plop an Image block on a page and we want it to go full-width while the rest of the content respects the root-level padding?<\/p>\n\n\n\n Enter\u2026<\/p>\n\n\n While attempting to create the first default WordPress theme that defines all styles in the :hover<\/code>,
:active<\/code>,
:focus<\/code>), and the margins<\/a> and padding of layout containers<\/a>. You can see how those were temporarily fixed in the TT2
style.css<\/code> file rather than making it into the
theme.json<\/code> file.<\/p>\n\n\n\n
What kind of spacing are we talking about?<\/h3>\n\n\n
<body><\/code> element. That\u2019s nice because it ensures consistent spacing on an element that is shared on all pages and posts.<\/p>\n\n\n\n
theme.json<\/code>. So, even if you have root-level padding, you can still allow, say, an image (or some other block) to break out and go full-width.<\/p>\n\n\n\n
Root-level padding<\/h3>\n\n\n
<body><\/code> element in
theme.json<\/code> since the experimental Gutenberg plugin<\/a> introduced it in version 11.7. We set it on the
styles.spacing<\/code> object, where we have
margin<\/code> and
padding<\/code> objects to define the top, right, bottom, and left spacing on the body:<\/p>\n\n\n\n
{\n \"version\": 2,\n \"styles\": {\n \"spacing\": {\n \"margin\": {\n \"top\": \"60px\",\n \"right\": \"30px\",\n \"bottom\": \"60px\",\n \"left\": \"30px\"\n },\n \"padding\": {\n \"top\": \"30px\",\n \"right\": \"30px\",\n \"bottom\": \"30px\",\n \"left\": \"30px\"\n }\n }\n }\n}<\/code><\/pre>\n\n\n\n
<body><\/code> element, we would see these CSS styles:<\/p>\n\n\n\n
body {\n margin-top: 60px;\n margin-right: 30px;\n margin-bottom: 60px;\n margin-left: 30px;\n padding-top: 30px;\n padding-right: 30px;\n padding-bottom: 30px;\n padding-left: 30px;\n}<\/code><\/pre>\n\n\n\n
Padding-aware alignments<\/h3>\n\n\n
theme.json<\/code> file, lead designer Kjell Reigstad illustrates the challenging aspects of breaking out of root-level padding in this GitHub issue<\/a>.<\/p>\n\n\n\n