-
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
Block Alignments Rethinking #20650
Comments
So this is more or less what we currently do for the image block, right? For left/right floating, a slight variation could be: markup <figure class="wp-block-something wp-align-left">
<div class="wp-aligned-content"></div>
</figure> styles .wp-align-left .wp-aligned-content {
float: left;
} The benefit of this would be that we consistently have In not sure if it would be valuable, but we could consider always adding the extra inner div wrapper. It might not be useful for basic usage, but it could potentially be used by themes if they'd like some more complex positioning. Additionally it gives more consistency across all alignments. |
Good idea especially if we decide to go with our own style and separate classNames.
Same here, I agree, though only for blocks with align support. |
Cc @jasmussen for feedback. Here's his codepen: https://codepen.io/joen/pen/zLWvrW. |
Right, only for blocks that support alignment, but the block would always have the extra div wrapper, regardless of the alignment selected. |
I absolutely have thoughts on this and thank you for the ping. I will return with a more in depth comment, but overall I definitely agree this is an area where we can make things way better. |
Another challenge: When the outer block wrapper (top level element) is not floated, but rather the extra div element within, it would mess up the selection outline and toolbar position. Currently, we're handling this by creating an exception for alignments. In this case, we'd look if there's a |
@ellatrix right I don't see any other way for that one :( |
@youknowriad Just exploring some ideas... What if we keep/extend alignment in block support, and internally wrap the block in an "alignment wrapper" element, which get automatically added around both the save and edit component?
markup <div class="wp-align-wrapper wp-align-left">
<figure class="wp-block wp-block-image">...</div>
</div> |
Sounds interesting, I believe in that case the |
@youknowriad No, the <div class="wp-align-wrapper wp-align-left">
<Block.figure class="wp-block wp-block-image">...</Block.figure>
</div> The wrapper around the block would be added internally to both edit and save output. I'll quickly explore this idea and see if it's interesting. |
This is the markup I'm proposing in the last few comments: No Alignmentmarkup <div class="wp-align-wrapper">
<figure class="wp-block-image"></figure>
</div> The wrapper could be omitted here, but personally I like the consistency, and it allow themes to create more complex layouts. styles .wrapper > * {
max-width: 600px; /* width of the regular blocks */
margin-left: auto;
margin-right: auto;
} Wide Alignmentmarkup <div class="wp-align-wrapper wp-align-wide">
<figure class="wp-block-image"></figure>
</div> styles .wrapper > *.wp-align-wide {
max-width: 800px; /* width of the wide blocks */
margin-left: auto;
margin-right: auto;
} Full Alignmentmarkup <div class="wp-align-wrapper wp-align-full">
<figure class="wp-block-image"></figure>
</div> styles .wrapper > *:not(.wp-align-full) {
max-width: 600px; /* width of the regular blocks */
margin-left: auto;
margin-right: auto;
} Left/Right Alignmentmarkup <div class="wp-align-wrapper wp-align-left">
<figure class="wp-block-image"></figure>
</div> styles .wp-align-left > * {
float: left;
} Center Alignmentmarkup <div class="wp-align-wrapper wp-align-center">
<figure class="wp-block-image"></figure>
</div> styles .wp-align-center {
display: flex;
justify-content: center;
} |
I like the consistency here. Awesome proposal @ellatrix |
Question: isn't no alignment the same as centre alignment? If not, what's the difference? |
It's not for me. no alignment means "left alignment" but not floated. while center alignment is centered. |
I'm not sure if "no alignment" being left aligned ever worked. And what about right alignment, but not floated? |
It could be a potential alignment but its need is very small. no-alignments is just any regular |
(In case anyone is confused about floats and wide/full being called alignments, see #19672.) The Themes that implement wide/full widths via negative margins don't have the centered-by-default issue. Instead, of course, they have to deal with the clunkiness of negative margins, which tend to be a pain to deal with, particularly for the full width when trying to account for the scroll bar, which is not taken into account by Another approach a theme might use is CSS Grid to handle position/alignment, but of course in that context you can't use floats. It's also worth noting that some WordPress themes use the float left and float right options for a pull-out position, where lots of negative margin is used to take the floated element completely out of the main content column. Perhaps that should be treated as its own separate option in addition to the existing positions/alignments/widths/etc? As pointed out in #19672, it's important to note that wide/full widths and left/right floats are technically not even alignments in the first place. So perhaps the technical approach for widths vs floats vs actual alignments should be different, rather than attempting to use the same approach for all of them. |
No, I don't think so. the alignment-wrapper will be centered but not its content.
Treating them differently means you can apply both at the same time, which is not the case for any of these right? |
Would blocks have an alignment wrapper even when not aligned? That seems like a lot of additional markup.
I was more thinking along the lines of not having to use a wrapper for width variants like wide/full, or something like that. I think it's fine to keep the options mutually exclusive in the UI. (Though I do think they should each have more descriptive labels like "Stretch to full width" or "Float left" rather than the misleading and generic "Align ___".) |
"No alignment" just means that no alignment is forced, that the defaults are used. |
It was mentioned a few times that there are basically two approaches for alignments:
For my themes I went with negative margins because I think the first approach is not very robust. Any custom block or custom block style from third-party plugins which accidentally overrides the margin value breaks the theme layout. For example:
Not sure if you have already plans to prevent that but I thought I mention it here. |
As a theme author who's been consistently working the block editor for a year+ now, I just want to weigh in on the primary issue I've run into with alignment classes. In this situation: <div class="wp-block-image">
<figure class="alignleft"></figure>
</div> I can't write the selector <div class="wp-block-image is-aligned-right">
<figure class="alignleft"></figure>
</div> |
@youknowriad: There is one thing I find quite confusing: In backend/editor there is also the |
right now Ideally, we'd just rely on something like If we want to solve the alignment CSS in core and apply them automatically on the frontend like this issue suggests to make it easier for theme authors to support alignments, this need to be solved somehow. |
What other actionable items is needed for this issue? |
Moving my comments from #28964 to this issue, as requested. Additional related issue: #28966. (As an aside: we've been using the parent-based rules from the original code examples of this issue since the beginning of 2019. This approach is extremely lightweight and has worked very well thus far.)
This issues come when the HTML on the frontend is markedly different to the HTML in the editor. Because of the discrepancies, we're having to write more and more fallback code for the editor after each Core update. This is becoming difficult to manage and means that older Themes need to be reworked more and more often, causing us to run into problems when the Theme is uses across various installations and various versions of Core. For example: The HTML for a separator block on the frontend of the website is a simple In the editor, the Because the wrapper isn't specific to the separator block ( I have added my own block using |
@markhowellsmead: But what if an element of normal (constrainted) width should contain a widely or fully aligned element? |
@strarsis Sure, the CSS for that is simple. I'm not sure of the design logic for this example and I haven't come across it in any of the Gutenberg projects we've worked on over the last two years, but it should be achievable. If the HTML logic is simplified and consistent across all blocks, the editor, and the frontend, then the CSS would be pretty easy to solve. The problem comes when the HTML nesting logic is completely different in the editor and in the frontend, which is currently the case. |
Example for this kind of layout: https://css-tricks.com/full-width-containers-limited-width-parents/ |
I'll be watching this issue. I have the same problems as Mark over and over again. Align classes cannot be consistent through all blocks since they're not applied to all of them (ie wp-block-group) and when I add [data-align] css styles just for the editor, I run into specificity issues and have to backtrack. I now find myself needed to code multiple separate edge cases for the editor and front-end. |
Now that we landed this #29335 I consider this issue done. The new behavior is triggered by the presence of the "experimental-theme.json" file to avoid breaking changes for existing themes. This also means classic themes can't be update yet to use it since it's still "experimental" but |
Current Issues
Alignments in Gutenberg today suffer from several issues, the most important one being the inconsistency between the different block markups.
An image block aligned left/right or center has the following markup
but when it has no alignment or wide/full, it has the following markup
Other blocks like buttons, columns, ... use the align support hook which produces the following markup no matter the alignment applied
This makes it almost impossible to support properly for theme authors without bugs.
There are more or less valid reasons for these differences but instead of going through history, let's try to focus on what we want to support and how we can fix this.
Use cases
Let's assume we want to build a theme that supports all alignments (the most complex case) and all blocks.
We'll most likely have a wrapper for blocks whether it's coming from the theme's template files
.entry-content
or a group block.Now, let's take a look at each one of these use-cases and see how to implement them in terms of markup and styles.
Regular Alignment (no alignment)
markup
Note that the class here is not always present (for valid reasons I want to leave out of the current issue)
styles
The margin technique is needed here because we want to support wide/full alignments at the same time. (see later)
Wide alignment
markup
(don't mind the different alignment classNames, I'll explain later)
styles
Full-width alignment
markup
styles
We can decide here to revert the default styles for the regular blocks or update its selector to exclude the full width blocks
Floated blocks
Now, this is probably the most complex case. What we want here is a block that is marked as a float. That way, the following blocks, will wrap it but we also want the floating to be contained in the "regular" width area.
I saw some themes achieving this by applying computed margins to the block wrapper (left or right) and floating the block itself. In fact, that's the only possible technique for blocks using the "align support hook" but this approach is buggy since you can't really float multiple blocks in a row properly.
So what we're going to do is to add an extra wrapper around the blocks (like the image block above), that wrapper will have the regular size allowing us to float its content properly. So we end up with markup like that
markup
styles
Centered blocks
Theoretically, we can center blocks without extra wrappers but we can also use the extra wrapper technique similar to float blocks to center blocks easily
markup
styles
Alternatives
I'm personally not certain if there are other alternatives. Let me know if you have any.
Consequences
What does it mean if we have to implement this proposal:
Notes
The wide/full widths used above are examples, one idea here would be to rely on CSS variables and allow InnerBlocks to disable/enable/customize the widths per container.
The text was updated successfully, but these errors were encountered: