-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add content-width option to global-styles & theme.json
#25160
Comments
This would be a nice first step towards letting Gutenberg handle wide/full alignments styles on the front end. Gutenberg already makes this work in the editor, but theme authors have to write (in some cases, a lot of) styles on the front-end to implement the alignments there too. They shouldn't need to do that in the future, especially when block-based themes become the standard. |
theme.json
theme.json
Regarding the generated CSS, as I meantioned before it's a nightmare to get t right - especially on layouts with sidebars. :root {
font-size: 22px;
--contentWidth: 900px;
--wideWidth: calc(1.25 * var(--contentWidth));
}
.wp-block-post-content {
--paddingHorizontal: 1em;
padding: 0 var(--paddingHorizontal);
}
.wp-block-post-content > * {
max-width: var(--contentWidth);
margin-left: auto;
margin-right: auto;
}
.wp-block-post-content .alignwide {
width: calc(var(--wideWidth) + 2 * var(--paddingHorizontal));
max-width: 100%;
margin-left: auto;
margin-right: auto;
}
.wp-block-post-content .alignfull {
transform: translateX(calc(0px - var(--paddingHorizontal)));
width: calc(100% + 2 * var(--paddingHorizontal));
max-width: calc(100% + 2 * var(--paddingHorizontal));
margin-left: auto;
margin-right: auto;
}
.wp-block-post-content .alignleft {
float: left;
margin-left: calc(50% - var(--contentWidth) / 2);
}
.wp-block-post-content .alignright {
float: right;
margin-right: calc(50% - var(--contentWidth) / 2)
} It is something that I've been working on for a long time on various themes, and so far doesn't seem to have any issues.
In the above code I'm only doing this for |
Why can't the |
Good point, updated 👍 |
What do you think about adding (See #25115) |
I don't see why not... Feel free to post code with the Also adding a link to a codepen by @jasmussen: https://codepen.io/joen/pen/oEYVXB (see comment on an older issue) |
Thank you @scruffian for your suggestion to use The bare minimum I have to make alignments work properly regardless of the layout is now this: :root {
--contentWidth: 900px;
--wideWidth: calc(1.5 * var(--contentWidth));
}
* {
box-sizing: border-box;
}
.wp-site-blocks {
--paddingHorizontal: 1em;
padding: 0 var(--paddingHorizontal);
}
.wp-site-blocks > *:not(.wp-block-post-content),
.wp-site-blocks .wp-block-post-content > * {
max-width: var(--contentWidth);
margin-left: auto;
margin-right: auto;
}
.wp-site-blocks .alignwide {
width: var(--wideWidth);
max-width: 100%;
margin-left: auto;
margin-right: auto;
}
.wp-site-blocks .alignfull {
transform: translateX(calc(0px - var(--paddingHorizontal)));
width: 100%;
max-width: 100%;
box-sizing: content-box;
}
.aligncenter {
text-align: center;
}
.wp-site-blocks .alignleft {
float: left;
margin-right: 2em;
max-width: 360px;
}
.wp-site-blocks .alignright {
float: right;
margin-left: 2em;
max-width: 360px;
} A lot cleaner... and I think with a couple more tweaks we should be able to have a result that we can automatically add to FSE themes, taking the content-width setting into account. |
These rules should probably cover nested children too, right? |
They do, yes. I added a test layout where I have a group, inside the group a columns block, then on one of the columns a group, and inside the group I added the content block. All styles are properly applied on the global layout and the content. 😄 |
Thanks for this. I'm still not convinced that we're taking the right approach with |
Not quite... It depends on the layout and the content-width.
Where are you seeing this exactly?
That would only make sense in a layout without sidebars, it can't work like that when there is a sidebar otherwise the |
I would agree with @scruffian. I'm not sure content-width has a place in a responsive world. I understand how it could support how we currently do full-wide, but it seems like that could be refactored entirely. Specifically I've noticed as I'm increasingly using the site editor, that when I insert a block there, at the root of the document, I would expect it to not be constrained to a centered column, and that the ability for Post Content to constrain the content inside is a somewhat unique behavior which, while it should definitely be retained, should not be the default for blocks or template parts or anything else inserted into the site editor. There's some great conversation about how wide and fullwide alignments can be refactored in #20650. I can't find it now, but some of the related conversation I had with @youknowriad about it involved treating wide alignment as a property of, for example, template parts. Imagine you had a button in the block toolbar of a template part that said "constrain child blocks to a center column" (or something better), and that would enable the fullwide button for child blocks. To be clear I'm not sure that's the solution either — conversations about grid features in #16998 seem to suggest there are additional ways to align blocks. But in any case, I wouldn't want us to optimize too heavily towards how wide and fullwide alignments are handled today, as it's honestly a bit of a hack. |
Perhaps we mean different things when we talk about Take for example the Wikipedia site... Their main article content area doesn't have a max-width defined. As a result, it's impossible to read anything on a large(ish) screen (anything above 17 inches) because of the extremely big line-width (far beyond the industry-standard 80 characters/line). I have to resize & halve my browser width every time I need to read something there... With that in mind, there has to be some sort of "maximum content width" or "maximum characters per line" setting. And if there is such as setting, then the text content doesn't necessarily cover the full width of its container, it depends on what that container is, the content-width defined and the device used to consume the content. And if the content doesn't cover the full-width of its container for readability purposes, then wide/full both have a purpose. Gutenberg is evolving into a page design tool, and in that context I agree that restricting the content-width will not always make sense. |
Related to #20650 One thing to consider here is that a single content width option is not a solution here. We need a content width for wide alignments and one for regular aligned blocks. We also need a way to define what alignments are supported on each "group block" (or template part). For example if I want to build a site that is split on two columns and each column need to support full/wide and regular alignments with its own widths, how do I do that. the missing piece before diving into this work is that we need a good API and the |
Well... With the above CSS, all we have to do in order to have different width depending on the container is to add inline styles to these innerBlocks and change the css-vars using |
Do you think custom alignments are something that we want to support? |
It depends on what you mean by "custom alignments"...
but these things would be extreme edge-cases and block-styles would be more suitable to handle things like that on a per-block basis |
There's also the point to remember that most of these alignments aren't even "alignments" in the traditional sense of the word. Wide/full are widths, not true alignments. Left/right are really floats, and their exact behavior actually varies somewhat since some themes use negative margins to pull the block outside of the main column entirely. (Twenty Twenty does this.) Center is the only thing that could actually be considered a true alignment. So the inconsistent implementation of these options makes things complicated. What exactly is supposed to be the difference between "no alignment" and "center alignment"? Should pulling stuff out of the main column like the left/right options on Twenty Twenty really be considered the same thing as a standard float without any negative margins? It's also worth noting that even though 2 of the options are widths, 2 are float options, and 1 is an actual alignment, you would almost never use any of them at the same time. I can't see a reason to use wide/full with a float unless you just want to end up with the same result that the float options already do on a theme like Twenty Twenty. 😵 |
Content in columns is a good example. Inside a column the difference between If you have a
In that case I think we might need to look to different ways to solve the problem. A block should be able to prevent its children from setting |
The examples posted in the screenshot above are totally dependent on the theme's styles. |
@aristath Great job, this is the best alignment solution I have seen so far! kudos mate! Works great for theme developers as a starting point. One thing, did you test the nesting in this way, if you have a group that is set to full width and then inside that you have another group you want to be contained by a standard wrapper. In my testing this just makes the nested group go full width? You could align it wide inside because we have a class for that but not standard width. unless I approach it this way, which is not perfect because text inside an .alignfull is now contained, but if all blocks including text had align classes available without manually adding them this gets pretty close to being perfect for all scenarios I've come across. sorry example in my sass implementation
|
Do we consider this solved with the addition of the layout setting? Should this be closed? |
Sounds like it to me |
Spawned from #20771
I started building a block-based theme from scratch today, and was surprised by how hard it still is to do some things.
The hardest part when creating a theme for use with the editor is figuring out how to handle the following:
max-width
to blocks so they are limited to a pre-defined content-widthalignwide
alignfull
Writing the CSS for these things is an absolute nightmare, especially in FSE where the user can add a sidebar, 2 sidebars, have nested columns within groups that are nested in columns and so on.
Generating these styles automatically would be ideal, and if done properly would solve a lot of issues.
theme.json
In
theme.json
we could use 2 options:contentWidth
: (string) can use any CSS value (90%
,1000px
,60em
etc)alignWideWidth
: (string|number) If string then any CSS value. If number then assume proportionate to thecontentWidth
(1.25
would be 1.25 times the contentWidth).Global Styles
In global-styles we'd need a setting for the content-width.
Adding the align-wide-width option would be of secondary importance and not really necessary. Good to have, but not a requirement.
EDIT: After further experimentation I realized that what we need is not a site-width as this original post was stating, but a content-width.
Site-width would be useful when building a "boxed" layout, which is not what most people will want. Content-width on the other hand describes more accurately what this is about: Setting a max-width for the actual blocks.
The site-width can be accomplished with columns if needed (3 columns, with the middle one set to 900px and the other 2 left to auto-calc)
The text was updated successfully, but these errors were encountered: