Description
Preconditions
- Magento version EE 2.2.2, PHP version 7.0.22
- Have 6 product items in stock
Steps to reproduce
- Log in to the store admin dashboard, go to Content->Pages
- Edit the default home page template, insert a widget with type = Catalog Products List.
- Save the page and refresh the cache.
Expected result
- Showing 5 product items each row with the same width on large screen, 4 items each row on middle screen, 3 items each row on small screen, 2 items each row on mobile.
Actual result
- Showing 4 product items each row on large screen, 3 items each row on middle screen, 2 items each row on small screen, only 1 item each row on mobile.
- A blank area showing on the right side of each row
- A margin on the left side of the 1st item of the 2nd row.
- No margin on the left side of the 2nd item of the 2nd row.
- While inspecting the code using Chrome, Firefox or Safari, we see 24 space characters between HTML tags
</li>
and the<li>
- Still showing some space characters between
</li>
and<li>
tags even if HTML minified
I have checked the template file located in
[Magento]/vendor/magento/module-catalog-widget/view/frontend/templates/product/widget/content/grid.phtml
This is the correct old version from Magento EE 2.1.0, line 45:
<?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '<li class="product-item">' : '</li><li class="product-item">' ?>
This is the new version from Magento EE 2.2, line 38~39
<?php if ($iterator++ != 1): ?></li><?php endif ?>
[24 space characters here]<li class="product-item">
Also, there are some space characters after <\li>
on line 108:
[21 space characters here]<?php endforeach ?>
In browser, space characters between <li/>
and <li>
" will be rendered as a short blank area, about 3.1 pixel width depends on your theme font size and agent style. That's why the older store is showing items correctly but the upgraded is not.
In the magento default blank theme, the product items in the widget grid has this style:
@media (min-width: 1024px), print 7a6df6497c1fd2f…a138.min.css:1 .page-layout-1column .block.widget .products-grid .product-item:nth-child(5n+1) { margin-left: 0; } @media (min-width: 1024px), print 7a6df6497c1fd2f…a138.min.css:1 .page-layout-1column .block.widget .products-grid .product-item { margin-left: calc((100% - 5*(100%/6))/4); width: 16.66666667%; }
Which means on 1024px and wider screen, the 5n+1 items has no left margin, others have padding on the left. Each of them has width = 16.666667%.
With those space characters after each product item, there is not enough space for the last item to be rendered in the same row, so it will be moved to the next row.
To fix this problem, you have to remove all space characters in the html template file between </li>
and <li>
tags.
I removed the space characters before <li class="product-item">
on line 39 and before <?php endforeach ?>
on line 108, save and refresh the page, it works!
To ugly fix this problem without touching the backend, the admin user can add custom style to the "block.widget .products-grid .product-item" class:
.block.widget .products-grid .product-item { margin-right: -3.1px }
If -3.1 px is not enough, add more.
However, I do not know if other Magento template files have the same issue. I am not an front-end expert but I know that there may be some other HTML tags have the same problem, like <ul>
, <ol>
.