使CSS网格占据另一个(单独的)重叠CSS网格的1行。

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

Make CSS grid take up 1 row of another (separate) overlapping CSS grid

问题

I have a layout where a text introduction is inline with the first product grid item. Then rest of the product grid should then wrap below.

然后,其余的产品网格应该在下方换行。

However, on mobile I need these items to be separate as the introduction needs to be above the product grid - which scrolls horizontally in an accordion. So the product 'cards' are wrapped in a div to contain them.

然而,在移动设备上,我需要将这些项目分开,因为介绍需要位于产品网格之上 - 该产品网格在手风琴中水平滚动。因此,产品 '卡片' 被包裹在一个 div 中以包含它们。

I have the majority of this working in my example, however the intro spans 2 rows of the grid (and more when more product rows are added), so if the text is longer it would go behind the project cards.

在我的示例中,我已经完成了大部分工作,但是介绍跨越了网格的2行(当添加更多产品行时会更多),因此如果文本较长,它会进入项目卡片的后面。

So how do I limit that grid to just taking up 1 row of the product grid? Is it possible with only CSS?

那么,如何将该网格限制为仅占据产品网格的1行?只使用CSS是否可能?

You'll need to view the example on a wider (800px+) viewport to see the desktop layout.

您需要在更宽的(800px+)视口上查看示例以查看桌面布局。

英文:

I have a layout where a text introduction is inline with the first product grid item. Then rest of the product grid should then wrap below.

However, on mobile I need these items to be separate as the introduction needs to be above the product grid - which scrolls horizontally in an accordion. So the product 'cards' are wrapped in a div to contain them.

I have the majority of this working in my example, however the intro spans 2 rows of the grid (and more when more product rows are added), so if the text is longer it would go behind the project cards.

So how do I limit that grid to just taking up 1 row of the product grid? Is it possible with only CSS?

You'll need to view the example on a wider (800px+) viewport to see the desktop layout.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  background: #eee;
}

.products {
  margin: 64px 32px;
}

.products__grid {
  display: flex;
  flex-wrap: nowrap;
  column-gap: 4px;
  margin: 0 -32px;
  overflow-x: scroll;
  scroll-snap-type: x mandatory;
  scroll-padding-left: 32px;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.product-card {
  background-color: white;
  box-sizing: border-box;
  display: flex;
  flex-shrink: 0;
  flex-direction: column;
  padding: 32px 32px 64px;
  width: 100%;
  max-width: 100%;
}

.product-card__title {
  color: black;
  display: flex;
  font-size: 1rem;
  padding: 0 32px;
}

.product-card--story {
  background-color: transparent;
  justify-content: center;
  padding: 0 32px;
  text-align: center;
}

@media only screen and (min-width: 800px) {

  .products {
    display: grid;
    column-gap: 4px;
    margin: 64px 32px 64px;
    grid-template-columns: repeat(12, 1fr);
  }

  .products__desc {
    background: rgba(255, 0, 0, .24);
    grid-column: 2 / span 4;
    grid-row: 1;
  }


  .products__grid {
    display: grid;
    column-gap: 4px;
    row-gap: 4px;
    grid-template-columns: repeat(12, 1fr);
    grid-row: 1;
    grid-column: 1 / span 12;
    margin: 0;
  }

  .product-card {
    grid-column: span 4;

    &amp;:first-child {
      grid-column: 9 / span 4;
    }
  }

  .product-card__title {
    color: black;
    display: flex;
    font-size: 1rem;
    padding: 0 32px;
  }

  .product-card--story {
    background-color: transparent;
    justify-content: center;
    padding: 0 32px;
    text-align: center;
  }

}

<!-- language: lang-html -->

&lt;div class=&quot;products&quot;&gt;
				
				&lt;div class=&quot;products__desc&quot;&gt;
					&lt;h2&gt;Title of the Block&lt;/h2&gt;
					&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.&lt;/p&gt;
					&lt;ul&gt;
						&lt;li&gt;Lorem ipsum dolor&lt;/li&gt;
						&lt;li&gt;Lorem ipsum dolor&lt;/li&gt;
						&lt;li&gt;Lorem ipsum dolor&lt;/li&gt;
						&lt;li&gt;Lorem ipsum dolor&lt;/li&gt;
					&lt;/ul&gt;
				&lt;/div&gt;
				&lt;div class=&quot;products__grid&quot;&gt;
					&lt;a href=&quot;#&quot; class=&quot;product-card&quot;&gt;
						&lt;h3 class=&quot;product-card__title&quot;&gt;Title of Product #1&lt;/h3&gt;
						&lt;img src=&quot;https://images.pexels.com/photos/13446290/pexels-photo-13446290.jpeg&quot; alt=&quot;ALT TEXT&quot; class=&quot;product-card__image&quot;&gt;	
					&lt;/a&gt;
					&lt;a href=&quot;#&quot; class=&quot;product-card&quot;&gt;
						&lt;h3 class=&quot;product-card__title&quot;&gt;Title of Product #2&lt;/h3&gt;	
						&lt;img src=&quot;https://images.pexels.com/photos/13446290/pexels-photo-13446290.jpeg&quot; alt=&quot;ALT TEXT&quot; class=&quot;product-card__image&quot;&gt;
					&lt;/a&gt;
					&lt;a href=&quot;#&quot; class=&quot;product-card&quot;&gt;
						&lt;h3 class=&quot;product-card__title&quot;&gt;Title of Product #3&lt;/h3&gt;
						&lt;img src=&quot;https://images.pexels.com/photos/13446290/pexels-photo-13446290.jpeg&quot; alt=&quot;ALT TEXT&quot; class=&quot;product-card__image&quot;&gt;
					&lt;/a&gt;	
					&lt;div class=&quot;product-card product-card--story&quot;&gt;
						&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.&lt;/p&gt;
					&lt;/div&gt;
				&lt;/div&gt;
				
			&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

这可能有更加优雅的方法来实现,但问题似乎是要将项目从滚动网格中移出并放入主网格中。不确定是否可能。所以我的解决方法相当简单:复制第一个产品,在移动设备上隐藏顶部的产品,并在桌面上将其隐藏在滚动区域中。

另一种选择是,在不复制内容的情况下,通过将第一个产品定位为绝对定位,将其从滚动区域中移出。不过这样做有其缺点,因为您需要根据产品的内容块大小设置描述的宽度和高度,否则您的第一个产品会与描述重叠。

英文:

There might be a more elegant way to do this, but the problem seems to be breaking out of the scroller grid and place the item into the main grid. Not sure if thats possible. So my workaround is quite simple: double the first product, hide up the top one on mobile and hide it in the scroller on desktop.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.description {
        background: red;
        padding: 24px;
        box-sizing: border-box;
      }

      .scroller {
        background: yellow;
        overflow-x: scroll;
        display: flex;
        flex-wrap: nowrap;
        gap: 12px;
        max-width: 100%;
      }

      .product {
        display: inline-block;
        width: 400px;
        height: 400px;
        box-sizing: border-box;
        padding: 24px;
        background: cyan;
        flex-shrink: 0;
      }

      .products &gt; .first {
        display: none;
      }

      .first {
        background: greenyellow;
      }

      @media screen and (min-width: 800px) {
        .products {
          display: grid;
          grid-template-areas:
            &quot;a b&quot;
            &quot;c c&quot;;
          grid-template-columns: auto 400px;
          gap: 12px;
        }

        .description {
          grid-area: a;
          height: 400px;
        }

        .products &gt; .first {
          grid-area: b;
          display: block;
          background: pink;
        }

        .scroller {
          grid-area: c;
        }
      }

<!-- language: lang-html -->

    &lt;div class=&quot;products&quot;&gt;
      &lt;div class=&quot;description&quot;&gt;Description&lt;/div&gt;
      &lt;div class=&quot;product first&quot;&gt;product 1a&lt;/div&gt;
      &lt;div class=&quot;scroller&quot;&gt;
        &lt;div class=&quot;product first&quot;&gt;product 1b&lt;/div&gt;
        &lt;div class=&quot;product&quot;&gt;product 2&lt;/div&gt;
        &lt;div class=&quot;product&quot;&gt;product 3&lt;/div&gt;
        &lt;div class=&quot;product&quot;&gt;product 4&lt;/div&gt;
        &lt;div class=&quot;product&quot;&gt;product 5&lt;/div&gt;
        &lt;div class=&quot;product&quot;&gt;product 6&lt;/div&gt;
        &lt;div class=&quot;product&quot;&gt;product 7&lt;/div&gt;
        &lt;div class=&quot;product&quot;&gt;product 8&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;

<!-- end snippet -->

Another option - without doubling content - is to pull the first product out of the scroller by positioning it absolute. This has its drawbacks though as you need to set the description width and height relative to the size of the content block of the product, otherwise your first product will overlap with the description.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  .description {
        background: red;
        padding: 24px;
        box-sizing: border-box;
      }

      .scroller {
        background: yellow;
        overflow-x: scroll;
        display: flex;
        flex-wrap: nowrap;
        gap: 12px;
      }

      .product {
        display: inline-block;
        width: 400px;
        height: 400px;
        box-sizing: border-box;
        padding: 24px;
        background: cyan;
        flex-shrink: 0;
      }

      .first {
        background: greenyellow;
      }

      @media screen and (min-width: 800px) {
        .products {
          position: relative;
        }

        .description {
          grid-area: a;
          height: 400px;
        }

        .first {
          position: absolute;
          top: 0;
          right: 0;
          background: pink;
        }
      }

<!-- language: lang-html -->

 &lt;div class=&quot;products&quot;&gt;
      &lt;div class=&quot;description&quot;&gt;Description&lt;/div&gt;
      &lt;div class=&quot;scroller&quot;&gt;
        &lt;div class=&quot;product first&quot;&gt;product 1&lt;/div&gt;
        &lt;div class=&quot;product&quot;&gt;product 2&lt;/div&gt;
        &lt;div class=&quot;product&quot;&gt;product 3&lt;/div&gt;
        &lt;div class=&quot;product&quot;&gt;product 4&lt;/div&gt;
        &lt;div class=&quot;product&quot;&gt;product 5&lt;/div&gt;
        &lt;div class=&quot;product&quot;&gt;product 6&lt;/div&gt;
        &lt;div class=&quot;product&quot;&gt;product 7&lt;/div&gt;
        &lt;div class=&quot;product&quot;&gt;product 8&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月10日 18:02:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217117.html
匿名

发表评论

匿名网友

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

确定