Classy puts a margin on form items, but not when they're in table rows.

This causes problems in contrib: if forms are placed inside responsive tables, there is no spacing between fields:
#2668708: Spacing between fields on paras

This behavior dates back to Drupal 4, and the reasoning is lost in the mists of time. Probably it is that core sometimes has a single form element in a table row (like a checkbox or dropdown) and no more spacing is desired than would be needed for text alone.

The least disruptive solution is:
When a table row has a single form item prevent it from adding unnecessary extra spacing, but if it has multiple form items allow spacing between them.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jonathanjfshaw created an issue. See original summary.

darketaine’s picture

A first guess would be that in case there is a single field-item inside a td it should get its spaces from the table elements (td) and not their content.

What I see is that this comes in drupal since 4.7 so I don't know if it's a good idea to change it just because there is a case of a module where this goes a bit wrong.

I would like though to have an opinion from the Classy maintainers as they sure know more than we do :)

bojanz’s picture

Also a problem in Inline Entity Form. Our fields look terrible because of the low spacing. Would be great not to need custom CSS.

mglaman’s picture

The drag handle is positioned and all other elements just kind of float around it. Almost like it's expected to be in its own td, however inline forms are not rendering it that way.

emma.maria’s picture

Issue tags: +CSS, +frontend
mglaman’s picture

Linking to solution that bojanz committed to Drupal Commerce 2.x: https://github.com/drupalcommerce/commerce/commit/e52fbfe03e305421905243...

In short:

-        $this->t('Value'),
 +        ['data' => $this->t('Value'), 'colspan' => 2],
+      // The tabledrag element is always added to the first cell in the row,
 +      // so we add an empty cell to guide it there, for better styling.
        $value_form['#attributes']['class'][] = 'draggable';
 +      $value_form['tabledrag'] = [
 +        '#markup' => '',
 +      ];

Needed to manually make tabledrag a td manually with an empty value.

bojanz’s picture

@mglaman
This issue is about the element margins (vertical spacing between two elements), which is unaffected by #6.
The extra column trick is just for avoiding reflow problems around the tabledrag element.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

davidhernandez’s picture

Issue tags: +Needs screenshots

Can someone add a couple screenshots highlighting what the problem is before digging in?

jonathanshaw’s picture

Here's the screenshot from Paragraphs:

It may not be very helpful though, because of the additional styling that the paragraphs widget is doing is not making the context very obvious. This is actually a row in a responsive table (with the drag handle aligned to the top of the cell), and that row is rendering an edit form for an entity called "List item" that has 3 fields

This might be a problem with any screenshot in fact ... maybe the issue comes up mostly when people do unusual things with forms.

davidhernandez’s picture

Based on what the issue summary says, we should be able to add some form elements to a table and look at it directly in Classy, right? That should reveal the problem directly.

jonathanshaw’s picture

I think so, yes

Chernous_dn’s picture

Hi #jonathanjfshaw can you provide screenshots with this bug? And where I can check this bug? Thanks.

jonathanshaw’s picture

#10 is all I've got. I don't know if there's anywhere in core that is directly affected. The problem shows up when contrib modules like IEF and paragraphs want to put forms inside tables.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

pivica’s picture

Version: 8.4.x-dev » 8.5.x-dev
Status: Active » Needs review
FileSize
91.26 KB
99.85 KB
481 bytes

Investigated this a bit and manage to track original CSS addition in file /misc/drupal.css in commit http://cgit.drupalcode.org/drupal/commit/?id=ad5e3ebb75df5c861251ba3ed3d... which happened in 2004, yep long time ago:

+tr.light .form-item, tr.dark .form-item {
+  margin-top: 0em;
+  margin-bottom: 0em;
+  white-space: nowrap;
+}

This code didn't change a lot over the year, got moved a couple of time and now it is in /core/themes/classy/css/components/form.css:

tr.odd .form-item,
tr.even .form-item {
  margin-top: 0;
  margin-bottom: 0;
}

This margin reset rules are useful for tables with single table row form elements, for example, pages like

admin/modules
admin/config/regional/language
...

So a use case for this is that we need to have .form-item in table and table rows needs to have .odd or .even classes. And then we apply this margin reset rules so we save up some vertical space.

The problem is that these reset rules are too general and they will be applied for other cases like for Inline Entity Forms or Paragraphs. In this cases, we don't want this.

We could easily fix this if we assume that this rules should be active only in the case when we have one form-item inside of td, and this was probably the case when these rules were originally added:

tr.odd td > .form-item:only-child,
tr.even td > .form-item:only-child {
  margin-top: 0;
  margin-bottom: 0;
}

Use cases where margin reset should be active should still work with this but the rule will not be active for table cell which has multiple elements inside of table cells.

Here are the screenshots before and after the patch applied:

Browser support for :only-child pseudo selector is good https://caniuse.com/#feat=css-sel3, including IE9.

Status: Needs review » Needs work

The last submitted patch, 18: margin-on-form-items-in-table-rows-2675464.patch, failed testing. View results

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

slydevil’s picture

Patch from #18 works well. Thank you!

slydevil’s picture

Status: Needs work » Needs review

I've re-run the tests and they all passed for the 8.5 branch. Setting back to Needs Review.

sgurlt’s picture

Agree #18 also works for me and makes the UX for my editors way better :)

ricksta’s picture

Our team noticed this issue when using fields with unlimited instances, and a single link field, within a Paragraphs container (similar to the screenshot in #10), and the patch in #18 resolves this.

In testing, we created pages, blocks, forms and content using paragraphs, using various types of fields, and do not see any problems anywhere. On forms using Paragraphs this patch equalizes the spacing between fields, as well as anywhere table rows contain elements with the "form-item" class, such as a contact form containing a link field. We noted no regressions.

Before (below)
Only local images are allowed.

After (below)
Only local images are allowed.

Themes tested:
• tested with Seven (subtheme of Classy)
• tested with Bratik (subtheme of Classy)
• tested with Adminimal (subtheme of Classy)

Browsers tested:
• Chrome 65.0.3325
• IE 11
• MS Edge 14, 15, 16
• FireFox 52.0
• Safari 11.0.3

Drupal versions tested:
8.5
8.6.x-dev

This very selective minimal patch (margin-on-form-items-in-table-rows-2675464.patch) does seem to work as intended. We think it would be a worthwhile improvement to backend UX where the admin theme is Seven, Bartik or Adminimal theme, or any other theme with Classy as the parent theme.

Based on #21, #22, and #23, and our own testing, I'm marking this as RTBC.

ricksta’s picture

Status: Needs review » Reviewed & tested by the community
davidhernandez’s picture

I didn't test this personally, but regarding the change itself, it is pretty small and I would classify as a minor bug fix. It shouldn't adversely affect any subthemes, so I'm ok to make the change.

lauriii’s picture

Status: Reviewed & tested by the community » Needs work

Thanks @pivica for pointing out on #18 what is the use case for having those rules set. Could we document that in the CSS as well so that when this is being looked at next time, it would be faster to get the idea why it is there?

ricksta’s picture

CSS inline comment added as per suggestion by @lauriii, along with interdiff from #18.

ricksta’s picture

Status: Needs work » Needs review
jonathanshaw’s picture

Thanks for adding the comment @ricksta. However, we probably need it to explain the "why" (per #18) more than the "what".

davidhernandez’s picture

Status: Needs review » Needs work
ricksta’s picture

Assigned: Unassigned » ricksta
Status: Needs work » Needs review
FileSize
619 bytes

Updated comment in CSS for the patch to include the "why" as requested by @jonathanshaw.

ricksta’s picture

Updating patch against latest 8.6.x commits. This includes updated comment in CSS for the patch to include the "why" as requested by @jonathanshaw.

mark_fullmer’s picture

Patch in #33 has my RTBC -- tested in multiple browsers & with nested fields.

I'll defer to the maintainers of Classy for the official issue status change.

davidhernandez’s picture

You don't need my permission to change it to RTBC. If you reviewed it and it meets the criteria, feel free.

mark_fullmer’s picture

Status: Needs review » Reviewed & tested by the community
lauriii’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/themes/classy/css/components/form.css
@@ -32,8 +32,13 @@ form .field-add-more-submit {
+ * Limit scope of margin reset rule to only-child to allow form items inside
+ * table elements to have a margin top and bottom.

The question this should answer to is: why are we modifying the .form-item elements when it is the only child directly under an odd even colored table column. For me the rule seems very strict so there must be a specific reason why it exists, and that's exactly what we want to document here.

jonathanshaw’s picture

I suspect no one knows why we only do this for odd/even colored tables. The answer to that may be lost in the sands of Drupal 4.7.

We could say "When a table row has a single form item prevent it from adding unnecessary extra spacing, but if it has multiple form items allow spacing between them."

twfahey’s picture

Status: Needs work » Needs review
FileSize
658 bytes

Updated with more descriptive comment per jonathanshaw's suggestion, which seems correct to me.

jonathanshaw’s picture

Status: Needs review » Reviewed & tested by the community
ju.vanderw’s picture

Issue tags: -Needs screenshots

Screenshots were provided two years ago in #18, and are still applicable. Removing the "Needs Screenshots" Issue tag.

jonathanshaw’s picture

Assigned: ricksta » Unassigned
jonathanshaw’s picture

Issue summary: View changes
jonathanshaw’s picture

xjm’s picture

Thanks @twfahey! In the future you can also post an interdiff so we can easily see the changes you made.

The visual fix itself looks right but as @lauriii said this is an unusually specific selector. If no one knows why we do this (as #39 suggests) then maybe we should not be doing it. :) What would admin/modules and admin/config/regional/language look like without the rule? Should those tables have their own CSS for this styling, rather than the general rule that is causing us problems here? Etc.

Leaving at RTBC, but tagging for @lauriii's review again. Thank you!

lauriii’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Needs frontend framework manager review
FileSize
165.35 KB

It seems like the original CSS rule was introduced in Drupal 4.4.0 🐘I can't find any reason why this should be only applied to tables with odd even classes (rather than all tables). For example, it seems content list styles are broken since it doesn't have odd even classes.

For that reason something like this would probably make sense:

td > .form-item:only-child {
  margin-top: 0;
  margin-bottom: 0;
}



I'm sorry to come up with this feedback this late, but after my research, I started feeling making this change in Classy is too risky since this doesn't only affect core themes but all themes built on top of Classy. I'd like to suggest that we try to first come up with a proper fix in Seven, and once we are confident it works, try to find a way to move it to Classy. Currently, it is difficult to make this type of risky bug fixes to Classy, see #2659890: [Policy] [Plan] Drupal 9 and 10 markup and CSS backwards compatibility.

jonathanshaw’s picture

Title: Margin on form-items in table rows » Margin on form-items in table rows (Classy)
Issue summary: View changes
Status: Needs work » Postponed
Related issues: +#2982097: Margin on form-items in table rows (Seven)

Created #2982097: Margin on form-items in table rows (Seven) for Seven, postponing this on that.

Anatoliy Vorobyov’s picture

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jonathanshaw’s picture

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mbovan’s picture

Status: Postponed » Active

Since #2982097: Margin on form-items in table rows (Seven) was fixed long time ago I am updating the issue status here.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

komalk’s picture

Status: Active » Needs work
FileSize
658 bytes

Rerolled patch.

komalk’s picture

Status: Needs work » Needs review
FileSize
2.56 KB
2.95 KB

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Berdir’s picture

Status: Needs review » Needs work

This will need a reroll for D10.

quietone’s picture

Project: Drupal core » Classy
Version: 10.1.x-dev » 1.0.x-dev
Component: Classy theme » Code
Issue tags: +Bug Smash Initiative

This was discussed at a bugsmash group triage, with lendude and myself. lendude pointed out that that this breaks for form elements inside a table which isn't common in a front end theme. He looked at his own projects and did not find any custom CSS for this, suggesting that it might be fixed in Claro. Further, Umami only needs to support what it is needs for demonstration so it does not need a fix. That all seems reasonable so we have agreed to move this is contrib Classy.