CSS网格项目溢出滚动在小屏幕

cu6pst1q  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(136)

我有一个CSS网格模板为我的后块。我想要的是,在小屏幕所有4项,除了第一个应该在x滚动.我的意思是溢出-x滚动。我看到了一个模板,但不明白他们是怎么做到的。
看看他们的例子,我在哪里看到:大屏幕x1c 0d1x

小屏:

谁来帮我做一下。我该怎么做?
我的代码:

镜像

  1. .parent {
  2. display: grid;
  3. grid-template-columns: repeat(4, 1fr);
  4. grid-template-rows: repeat(2, 1fr);
  5. grid-gap: 15px;
  6. }
  7. .div {
  8. background: red;
  9. padding: 15px;}
  10. .div:nth-child(1) { grid-area: 1 / 1 / 2 / 5; }
  11. .div:nth-child(2) { grid-area: 2 / 1 / 3 / 2; }
  12. .div:nth-child(3) { grid-area: 2 / 2 / 3 / 3; }
  13. .div:nth-child(4) { grid-area: 2 / 3 / 3 / 4; }
  14. .div:nth-child(5) { grid-area: 2 / 4 / 3 / 5; }
  1. <div class="parent">
  2. <div class="div"> </div>
  3. <div class="div"> </div>
  4. <div class="div"> </div>
  5. <div class="div"> </div>
  6. <div class="div"> </div>
  7. </div>
wa7juj8i

wa7juj8i1#

我只是放了一个div,覆盖了可以滚动的元素,并添加了一些CSS,如果你对我的答案有疑问,你可以打电话给我:

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. margin: 10px;
  7. }
  8. .scrolling,
  9. .parent {
  10. display: grid;
  11. grid-template-columns: repeat(2, 1fr);
  12. grid-template-rows: repeat(2, 150px);
  13. grid-auto-flow: dense;
  14. gap: 2px;
  15. }
  16. .div {
  17. min-height: 150px;
  18. background-color: red;
  19. }
  20. @media screen and (min-width: 729px) {
  21. .parent > .div {
  22. grid-column: 1 / 2;
  23. grid-row: 1 / 3;
  24. }
  25. }
  26. @media screen and (max-width: 728px) {
  27. .parent {
  28. grid-template-rows: repeat(3, 150px);
  29. }
  30. .scrolling {
  31. grid-template-columns: repeat(4, 50%);
  32. grid-template-rows: 150px;
  33. grid-column: 1 / 3;
  34. overflow-x: scroll;
  35. scroll-behavior: smooth;
  36. scroll-snap-type: x mandatory;
  37. }
  38. .scrolling > .div {
  39. scroll-snap-align: start;
  40. }
  41. .parent > .div {
  42. grid-column: 1 / 3;
  43. grid-row: 1 / 3;
  44. }
  45. }
  1. <div class="parent">
  2. <div class="div"></div>
  3. <div class="scrolling">
  4. <div class="div"></div>
  5. <div class="div"></div>
  6. <div class="div"></div>
  7. <div class="div"></div>
  8. </div>
  9. </div>
展开查看全部
db2dz4w8

db2dz4w82#

你也许可以用子网格来实现。在这里你可以玩img的最大大小来推或不子网格上溢出.
我在申报单上留下了“肖像”这个词。对于“桌面”,它是在景观

  1. #parent {
  2. display: grid;
  3. grid-template-columns: 1fr;
  4. grid-template-rows: 1fr auto;
  5. width: auto;
  6. }
  7. #postLevel1 img {
  8. max-width: 100%;
  9. }
  10. #postLevel2 {
  11. display: grid;
  12. grid-template-columns: repeat(4, 1fr);
  13. grid-template-rows: 1fr;
  14. overflow-x: scroll;
  15. }
  16. #postLevel2 img {
  17. max-height: 100%;
  18. }
  19. @media (orientation: portrait) {}
  20. @media (orientation: landscape) {
  21. #parent {
  22. display: grid;
  23. grid-template-columns: repeat(2, 1fr);
  24. grid-template-rows: 1fr;
  25. width: 100vw;
  26. height: 100%;
  27. }
  28. #postLevel2 {
  29. display: grid;
  30. grid-template-columns: repeat(2, 1fr);
  31. grid-template-rows: repeat(2, 1fr);
  32. overflow-x: hidden;
  33. }
  34. #parent img {
  35. max-width: 100%;
  36. }
  37. }
  1. <div id="parent">
  2. <div id="postLevel1">
  3. <img src="https://picsum.photos/id/237/1200" alt="">
  4. </div>
  5. <div id="postLevel2">
  6. <div>
  7. <img src="https://picsum.photos/id/35/600" alt="">
  8. </div>
  9. <div>
  10. <img src="https://picsum.photos/id/74/600" alt="">
  11. </div>
  12. <div>
  13. <img src="https://picsum.photos/id/192/600" alt="">
  14. </div>
  15. <div>
  16. <img src="https://picsum.photos/id/121/600" alt="">
  17. </div>
  18. </div>
  19. </div>
展开查看全部

相关问题