【问题标题】:How do I get CSS row-gap and column-gap to not show up if row or column is missing?如果缺少行或列,如何使 CSS row-gap 和 column-gap 不显示?
【发布时间】:2021-04-04 14:15:27
【问题描述】:

我有一个包含三个图像和一个文本部分的网格。我有网格的列和行间隙,如果我拥有所有三个图像,则可以正常工作。但是,如果缺少任何图像,我仍然会得到缺少的列或行的列或行间隙。如果行或列不存在,有什么办法可以让这些空白消失吗?

我所包含的示例在响应式设计中给出了 1、2 或 3 张图像的三种情况。在一个图像示例中,您可以在右侧看到额外的列间隙。当浏览器变窄时,缺失行的行间距就会变得可见。

div.thumbnail
{
    background-color: green;
    padding : 1em;
    break-inside : avoid;
    display : grid;
    column-gap : 1em;
    row-gap : 1em;
    grid-template-columns : auto 1fr auto;
    grid-template-areas : "image1 text image2"
                          "image1 text image3";
}

div.thumbnail div.image1
{
    background-color : cyan;
    text-align : right;
    grid-area : image1;
}

div.thumbnail div.image2
{
    background-color : pink;
    text-align : left;
    grid-area : image2;
}

div.thumbnail div.image3
{
    background-color : limegreen;
    text-align : left;
    grid-area : image3;
}

div.thumbnail div.text
{
    background-color : orange;
    grid-area : text;
}

/* for printing and small devices like iPhone #1 */
@media screen and (max-width: 760px), print and (max-width: 5in)
{
    div.thumbnail
    {
        grid-template-columns : 1fr !important;
        grid-template-areas : "image1"
                                "image2"
                                "image3"
                                "text";
    }

    div.thumbnail div.image1,
    div.thumbnail div.image2,
    div.thumbnail div.image3,
    div.thumbnail div.text
    {
        text-align : center;
        width : auto !important;
    }
}
<h2>One Image</h2>

<div class="thumbnail">
    <div class="image1" style="width:200px">
        image 1
    </div>
    <div class="text">
        Text
    </div>
</div>

<h2>Two Images</h2>

<div class="thumbnail">
    <div class="image1" style="width:200px">
        image 1
    </div>
    <div class="image2" style="width:200px">
        image 2
    </div>
    <div class="text">
        Text
    </div>
</div>

<h2>Three Images</h2>

<div class="thumbnail">
    <div class="image1" style="width:200px">
        image 1
    </div>
    <div class="image2" style="width:200px">
        image 2
    </div>
    <div class="image3" style="width:200px">
        image 3
    </div>
    <div class="text">
        Text
    </div>
</div>

谢谢,

迈克

【问题讨论】:

    标签: css css-grid


    【解决方案1】:

    不要使用模板,而是将项目明确放置在需要的位置。请注意我如何将媒体查询更改为 min-width 而不是 max-width 所以我们只需要在大屏幕上定义项目放置,我们在小屏幕上保留默认位置

    div.thumbnail {
      background-color: green;
      padding: 1em;
      display: grid;
      gap: 1em;
    }
    
    div.image1 {
      background-color: cyan;
    }
    div.image2 {
      background-color: pink;
    }
    div.image3 {
      background-color: limegreen;
    }
    div.text {
      background-color: orange;
    }
    
    @media screen and (min-width: 760px) {
      div.thumbnail {
        grid-template-columns: auto 1fr; /* define only 2 explicit column */
        grid-auto-flow: dense;
      }
      div.image2,
      div.image3 {
        grid-column: 3; /* add another column if (2) or (3) is present */
      }
      div.text {
        grid-column: 2;
      }
      /* (1) and text span 2 row when (3) is present */
      div.image1:nth-last-child(4),
      .image3 + div.text{
        grid-row: span 2;
      }
      [class*=image] {
        width:200px;
      }
    }
    <h2>One Image</h2>
    
    <div class="thumbnail">
      <div class="image1">
        image 1
      </div>
      <div class="text">
        Text
      </div>
    </div>
    
    <h2>Two Images</h2>
    
    <div class="thumbnail">
      <div class="image1">
        image 1
      </div>
      <div class="image2">
        image 2
      </div>
      <div class="text">
        Text
      </div>
    </div>
    
    <h2>Three Images</h2>
    
    <div class="thumbnail">
      <div class="image1">
        image 1
      </div>
      <div class="image2">
        image 2
      </div>
      <div class="image3">
        image 3
      </div>
      <div class="text">
        Text
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      相关资源
      最近更新 更多