如何包装物品,以确保最后一行始终有至少2个物品?

huangapple go评论92阅读模式
英文:

How to wrap items to always have min 2 items in the last row?

问题

图片问题

我的客户希望以响应式的方式在他的网站上列出车辆。但所有已知的技术(flex、grid)都会将最后一项换行到下一行。

我试图用图片做的事情

但在主视图中,它非常难看。客户希望:

  • 每行最多5个项目
  • 每行最少2个项目

棘手的部分是项目的数量必须是动态的。通常在4到6辆车之间

我知道可以像这样选择最后两个项目

  1. .item:nth-last-child(-n+2) {
  2. order: 2; /*将最后两项的顺序设置为2*/
  3. }

但我不能强制它们换行。有什么建议吗?

英文:

The problem with pictures

My client want to list vehicles in his website in a responsive way of course. But all known techniques(flex, grid) wrap the last item to the next row.

What I try to do with pictures

But in a hero it's extremely ugly. The client want:

  • Maximum 5 items per row
  • Minimum 2 items per row

The tricky part is the number of item must be dynamic. Usually between 4 to 6 vehicules

I know I can select the last two items like this

  1. .item:nth-last-child(-n+2) {
  2. order: 2; /* set order to 2 for the last two items */
  3. }

But I can't force them to wrap. Any ideas?

答案1

得分: 0

以下是翻译好的部分:

"It turns out I had to get the number of items beforehand and inject the class into the list to control the display"

"

    "

    ".SENARIO_4_ITEMS {
    .container {
    width: 268px;
    }
    @media #{$larger-down} {
    a {
    flex-basis: 50%;
    display: flex;
    justify-content: center;
    .container {
    width: 268px;
    }
    }
    }
    }"
    ".SENARIO_5_ITEMS {
    .container {
    width: 270px;
    }
    @media #{$xxlarge-down} {
    a {
    flex-basis: 33%;
    display: flex;
    justify-content: center;
    .container {
    width: 300px;
    }
    }
    }
    }"
    ".SENARIO_6_ITEMS {
    a {
    flex-basis: 33%;
    display: flex;
    justify-content: center;
    .container {
    width: 300px;
    }
    }
    @media #{$xxlarge-down} {
    .container {
    width: 300px;
    }
    }
    }"

    英文:

    It turns out I had to get the number of items beforehand and inject the class into the list to control the display

    1. <ul class="{{ 'SENARIO_' + numberOfItem + '_ITEMS' }}">

    ...

    1. .SENARIO_4_ITEMS {
    2. .container {
    3. width: 268px;
    4. }
    5. @media #{$larger-down} {
    6. a {
    7. flex-basis: 50%;
    8. display: flex;
    9. justify-content: center;
    10. .container {
    11. width: 268px;
    12. }
    13. }
    14. }
    15. }
    16. .SENARIO_5_ITEMS {
    17. .container {
    18. width: 270px;
    19. }
    20. @media #{$xxlarge-down} {
    21. a {
    22. flex-basis: 33%;
    23. display: flex;
    24. justify-content: center;
    25. .container {
    26. width: 300px;
    27. }
    28. }
    29. }
    30. }
    31. .SENARIO_6_ITEMS {
    32. a {
    33. flex-basis: 33%;
    34. display: flex;
    35. justify-content: center;
    36. .container {
    37. width: 300px;
    38. }
    39. }
    40. @media #{$xxlarge-down} {
    41. .container {
    42. width: 300px;
    43. }
    44. }
    45. }

huangapple
  • 本文由 发表于 2023年4月13日 22:05:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定